Uva 10158 War

并查集的应用 直接阔成2倍。后N项为对应的敌人

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 10010
int p[MAXN * 2];
int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);}
int N,M;
int main()
{
    //freopen("sample.txt","r",stdin);
    while (scanf("%d",&N) != EOF)
    {
        for (int i = 0; i <= N * 2; i++) p[i] = i;
        int op,x,y;
        while (scanf("%d%d%d",&op,&x,&y) != EOF)
        {
            if (op == 0 && x == 0 && y == 0) break;
            int  u = Find(x), v = Find(y);
            int  eu = Find(x + N) , ev = Find(y + N);
            switch (op)
            {
                case 1 : if (u == ev) puts("-1");
                         else
                         {
                             p[u] = v;
                             p[eu] = ev;
                         }
                         break;
                case 2 : if (u == v) puts("-1");
                         else
                         {
                             p[u] = ev;
                             p[eu] = v;
                         }
                         break;
                case 3 : if (u == v) puts("1");
                         else puts("0");
                         break;
                case 4 : if (u == ev) puts("1");
                         else puts("0");
                         break;
            }
        }
    }
    return 0;
}
时间: 2024-08-22 15:56:49

Uva 10158 War的相关文章

uva 10158 War (并查集)

uva 10158 War 四个操作:1)使AB成为朋友.2)使AB成为敌人.3)询问AB是不是朋友.4)询问AB是不是敌人.PS:敌人的敌人是朋友.朋友的朋友是朋友. 开一个2 * n的数组,0~n - 1表示的是朋友,n~2 * n - 1表示的是n号国家的敌人. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib&

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

UVA 10158 War 并查集

D - War(8.4.3) Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: War A war is being lead between two countries, A and B. As a loyal citizen of C, you dec

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

UVA 之11729 - Commando War

There is a war and it doesn't look very promising for your country. Now it's time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-p