並查集+uva10158
Problem B: 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 you), but you do not know which person belongs to which country. You can see people talking to each other, and through observing their behaviour during their occasional one-to-one conversations, you can guess if they are friends or enemies. In fact what your country would need to know is whether certain pairs of people are from the same country, or they are enemies. You may receive such questions from C’s government even during the peace-talks, and you have to give replies on the basis of your observations so far. Fortunately nobody talks to you, as nobody pays attention to your humble appearance.
Abstract
Now, more formally, consider a black box with the following operations:
setFriends(x, y) shows that x and y are from the same country
setEnemies(x, y) shows that x and y are from different countries
areFriends(x, y) returns true if you are sure that x and y are friends
areEnemies(x, y) returns true if you are sure that x and y are enemies
The first two operations should signal an error if they contradict with your former knowledge. The two relations ‘friends’ (denoted by ~) and ‘enemies’ (denoted by *) have the following properties:
~ is an equivalence relation, i.e.
1. If x ~ y and y ~ z then x ~ z (The friends of my friends are my friends as well.)
2. If x ~ y then y ~ x (Friendship is mutual.)
3. x ~ x (Everyone is a friend of himself.)
* is symmetric and irreflexive
4. If x * y then y * x (Hatred is mutual.)
5. Not x * x (Nobody is an enemy of himself.)
Also
6. If x * y and y * z then x ~ z (A common enemy makes two people friends.)
7. If x ~ y and y * z then x * z (An enemy of a friend is an enemy.)
Operations setFriends(x, y) and setEnemies(x, y) must preserve these properties.
Input
The first line contains a single integer, n, the number of people.
Each of the following lines contains a triple of integers, c x y, where c is the code of the operation:
c = 1, setFriends
c = 2, setEnemies
c = 3, areFriends
c = 4, areEnemies
and x and y are its parameters, which are integers in the range [0, n), identifying two (different) people. The last line contains 0 0 0.
All integers in the input file are separated by at least one space or line break.
Output
For every ‘areFriends’ and ‘areEnemies’ operation write 0 (meaning no) or 1 (meaning yes) to the output. Also for every ‘setFriends’ or ‘setEnemies’ operation which contradicts with previous knowledge, output a –1 to the output ; note that such an operation should produce no other effect and execution should continue. A successful ‘setFriends’ or ‘setEnemies’ gives no output.
All integers in the output file must be separated by at least one space or line break.
Constraints
n < 10000, the number of operations is unconstrained.
Sample Input
10
1 0 1
1 1 2
2 0 5
3 0 2
3 8 9
4 1 5
4 1 2
4 8 9
1 8 9
1 5 2
3 5 2
0 0 0
Sample Output
1
0
1
0
0
-1
0
思路:x和x+n是敵人,如果x和y是敵人,則x和y+n是朋友,y和x+n是朋友,如果x和y是朋友,x+n和y+n是朋友,以此類推
下面是程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=40010;
int n,rank1[MAXN],pra[MAXN];
void init()
{
for(int i=0;i<=2*n;i++)
{
rank1[i]=0;
pra[i]=i;
}
}
int find1(int x)
{
if(pra[x]==x)return x;
return pra[x]=find1(pra[x]);
}
void unite(int x,int y)
{
if(rank1[x]<rank1[y])
pra[x]=y;
else
{
pra[y]=x;
if(rank1[x]==rank1[y])
rank1[x]++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
int c,x,y;
scanf("%d",&n);
init();
while(cin>>c>>x>>y,c||x||y)
{
int a=find1(x),b=find1(y),e=find1(x+n),d=find1(y+n);
if(c==1)
{
if((a==d)||(b==e))cout<<-1<<endl;
else{unite(a,b);unite(e,d);}
}
else if(c==2)
{
if(a==b)cout<<-1<<endl;
else{unite(a,d);unite(b,e);}
}
else if(c==3)
{
if((a==b)&&(a!=d)&&(b!=e))cout<<1<<endl;
else cout<<0<<endl;
}
else if(c==4)
{
if((a!=b)&&((a==d)||(b==e)))cout<<1<<endl;
else cout<<0<<endl;
}
}
return 0;
}
相關文章
- 並查集到帶權並查集並查集
- 查並集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- 並查集(一)並查集的幾種實現並查集
- 並查集(小白)並查集
- [leetcode] 並查集(Ⅱ)LeetCode並查集
- [leetcode] 並查集(Ⅲ)LeetCode並查集
- [leetcode] 並查集(Ⅰ)LeetCode並查集
- 3.1並查集並查集
- 並查集應用並查集
- 寫模板, 並查集。並查集
- 並查集的使用並查集
- 並查集跳躍並查集
- 各種並查集並查集
- 淺談並查集並查集
- 食物鏈(並查集)並查集
- 並查集(Union Find)並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集(二)並查集的演算法應用案例上並查集演算法
- 並查集題目合集並查集
- 並查集深度應用並查集
- 並查集java實現並查集Java
- 【轉】種類並查集並查集
- The Suspects-並查集(4)並查集
- 並查集擴充套件並查集套件
- (Day3)並查集並查集
- 並查集演算法並查集演算法
- 並查集-Java實現並查集Java
- 簡單易懂的並查集演算法以及並查集實戰演練並查集演算法
- 專題五 並查集【Kuangbin】並查集
- 並查集の進階用法並查集
- 並查集(UnionFind)技巧總結並查集
- 關於並查集問題並查集
- 並查集的應用2並查集
- [複習] 種類並查集並查集
- 資料結構-並查集資料結構並查集
- 【LibreOJ109】【模板】並查集並查集
- 【題解】Solution Set - NOIP2024集訓Day8 並查集和可持久化並查集並查集持久化