L2-010. 排座位(并查集)*

 L2-010. 排座位

参考博客

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int a[200];
 5 int map[200][200];
 6 int find(int t)
 7 {
 8     if(t!=a[t])
 9     {
10         a[t]=find(a[t]);
11     }
12     return a[t];
13 }
14 int main()
15 {
16     int n,m,k,i;
17     cin>>n>>m>>k;
18     for(i=1;i<=n;i++)
19         a[i]=i;
20     int x,y,z;
21     for(i=1;i<=m;i++)
22     {
23         cin>>x>>y>>z;
24         map[x][y]=map[y][x]=z;
25         if(z==1)
26         {
27             x=find(x); 
28             y=find(y);
29             if(x!=y)
30                  a[x]=y;
31         }
32    }
33     int t1,t2;
34     for(i=1;i<=k;i++)
35     {
36         cin>>t1>>t2;
37         if(map[t1][t2]==1)
38         {
39             cout<<"No problem"<<endl;
40         }
41         else if(map[t1][t2]==-1)
42         {
43             if(find(t1)==find(t2))
44                 cout<<"OK but..."<<endl;
45             else
46                 cout<<"No way"<<endl;
47         }
48         else
49         {
50             cout<<"OK"<<endl;
51         }
52     }
53 }
原文地址:https://www.cnblogs.com/Annetree/p/8678544.html