POJ 2912 Rochambeau

解题思路:并查集

并查集的思想与POJ1182 食物链相同

枚举所有Judge ID 情况:

  如果有且仅有一个ID没有产生矛盾,则说明该ID即为Judge,且发现的最初回合为其余首次发现矛盾回合的最大值

  如果没有ID产生矛盾,输出Can not determine

  如果有一个以上ID产生矛盾,输出Impossible

欢迎指教
1 #include <iostream>
2  using namespace std;
3
4 #define MAXN 501
5 #define MAXM 2001
6 int root[MAXN],relation[MAXN],x[MAXM],y[MAXM],sa[MAXN];
7 char cmp[MAXM];
8 int findroot(int x)
9 {
10 int t;
11 if(root[x]!=x)
12 {
13 t=root[x];root[x]=findroot(root[x]);
14 relation[x]=(relation[x]+relation[t])%3;
15 }
16 return root[x];
17 }
18
19 bool IsTrue(int x, int y, int d)
20 {
21 int a, b;
22 a = findroot(x);
23 b = findroot(y);
24 if(a==b)
25 return (3+relation[x]-relation[y])%3==d ? true:false;
26 else
27 root[b]=a,relation[b]=(6-d+relation[x]-relation[y])%3;
28 return true;
29 }
30
31 int main()
32 {
33 int i,N,M,judge,r,t,ansP,ansS;
34 while (scanf("%d %d",&N, &M)==2)
35 {
36 memset(sa,0,sizeof(sa));
37 for(i=0;i<M;i++)scanf("%d %c %d",&x[i],&cmp[i],&y[i]);
38 for(judge=0;judge<N;judge++)
39 {
40 for(i=0;i<N;i++)root[i]=i,relation[i]=0;
41 for(i=0;i<M;i++)
42 {
43 if(x[i]==judge||y[i]==judge)continue;
44 if(cmp[i]=='=')r=0;
45 else if(cmp[i]=='>')r=1;
46 else r=2;
47 if(!IsTrue(x[i],y[i],r)){sa[judge]=i+1;break;}
48 }
49 }
50 for(ansS=i=t=0;i<N;i++)
51 {
52 if(sa[i]==0)t++,ansP=i;
53 if(sa[i]>ansS)ansS=sa[i];
54 }
55 if(t==0)printf("Impossible\n");
56 else if(t>1)printf("Can not determine\n");
57 else printf("Player %d can be determined to be the judge after %d lines\n",ansP,ansS);
58 }
59 return 0;
60 }
61

 

posted on 2010-12-08 16:30  ltang  阅读(345)  评论(0编辑  收藏  举报

导航