[TOJ 5224] exclusive seats (Combined Query set + two-dimensional array), toj5224
Description
The most subtle thing about banquet arrangement is to arrange seats for the guests who come to the banquet. In any case, you cannot always Route Two dead enemies to the same orders table! This arduous task is now handed over to you. For any guests, please write a program to tell the host whether they can be assigned the same seat.
Input
Enter the first line to give three positive integers: N (N ≤ 100), that is, the total number of guests who come to the banquet, then these people are numbered from 1 to N; M is the number of relationships between the two known guests, and K is the number of queried items. Next, in line M, each row shows the relationship between a pair of guests. The format is: the relationship between guests 1 and guests 2. The relationship 1 indicates a friend, and the relationship-1 indicates a friend. Note that two people cannot be both friends and enemies. In the last K rows, each row provides a pair of guests to be queried.
Let's assume that a friend is also a friend. But the enemy is not necessarily a friend, and the enemy is not necessarily an enemy. Only the direct hostility is absolutely not the same.
Output
Output a row of results for each query: If two guests are friends and there is No hostility, No problem is output; if they are not friends but not friends, OK is output; if there is hostility between them, but there are mutual friends, then output OK ...; if there is only hostility between them, No way is output.
Sample Input
7 8 4
5 6 1
2 7-1
1 3 1
3 4 1
6 7-1
1 2 1
1 4 1
2 3-1
3 4
5 7
2 3
7 2
Sample output
No problem
OK
OK...
No way
Question
The two-dimensional array judges the hostility and queries the set to establish a friend relationship.
#include<iostream>#include<algorithm>using namespace std;int p[105],g[101][101];int find(int r){ if(p[r]!=r) p[r]=find(p[r]); return p[r];}int join(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy) p[fx]=fy; } int init(){ for(int i=1;i<=100;i++) p[i]=i;}int main(){ init(); int n,m,k,i,a,b,c; cin>>n>>m>>k; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); if(c==-1) g[a][b]=g[b][a]=1; if(c==1) join(a,b); } for(i=1;i<=k;i++) { scanf("%d%d",&a,&b); if(find(a)==find(b)&&g[a][b]==0) cout<<"No problem"<<endl; else if(find(a)!=find(b)&&g[a][b]==0) cout<<"OK"<<endl; else if(find(a)==find(b)&&g[a][b]==1) cout<<"OK but..."<<endl; else if(find(a)!=find(b)&&g[a][b]==1) cout<<"No way"<<endl; } return 0;}