POJ2912-带权并查集

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

题目:传送门

 (原题目描述及样例在最下面)

类似食物链那个题。


AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define mm0(a) memset((a),0,sizeof((a)))  
using namespace std;
typedef long long LL;
const int N = 1e4+5;
int n,m;
int fa[N],r[N],pos[N];
struct lp{
    int a,tmp,b;
}cw[N];
bool judge[N];
int Fi(int x){
    if(fa[x]==x)return x;
    int tmp=Fi(fa[x]);
    r[x]=(r[x]+r[fa[x]])%3;
    return fa[x]=tmp;
}
void un(int a,int b,int tmp){
    int pa=Fi(a),pb=Fi(b);
    if(pa==pb)return;
    fa[pb]=pa;
    r[pb]=(r[a]-r[b]+3*2-tmp)%3;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("E://ADpan//in.in", "r", stdin);
    freopen("E://ADpan//out.out", "w", stdout);  
#endif
    while(~scanf("%d%d",&n,&m)){
        memset(pos,-1,sizeof(pos));
        memset(judge,true,sizeof(judge));
        for(int i=0;i<m;++i){
            char c;
            scanf("%d%c%d",&cw[i].a,&c,&cw[i].b);
            if(c=='='){
                cw[i].tmp=0;
            }else if(c=='>'){
                cw[i].tmp=1;
            }else{
                cw[i].tmp=2;
            }
        }
        for(int i=0;i<n;++i){
            for(int i=0;i<=n;++i)fa[i]=i,r[i]=0;
            for(int j=0;j<m;++j){
                int a=cw[j].a ,b=cw[j].b ,tmp=cw[j].tmp ;
                if(a==i||b==i)continue;
                int pa=Fi(a),pb=Fi(b);
                if(pa==pb){
                    if((r[b]+tmp)%3!=r[a]){
                        judge[i]=0;
                        pos[i]=j+1;
                        break;
                    }
                }else{
                    un(a,b,tmp);
                }
            }
        }
        int tot=0,who=0,max_no=0;
        for(int i=0;i<n;++i){
            if(judge[i]){
                tot++;
                who=i;
            }else{
                max_no=max(max_no,pos[i]);
            }
        }
        if(tot==0){
            printf("Impossible\n"); 
        }else if(tot==1){
            printf("Player %d can be determined to be the judge after %d lines\n",who,max_no); 
        }else{
            printf("Can not determine\n");  
        }
    }
    return 0;
}


题目描述:

N children are playing Rochambeau (scissors-rock-cloth) game with you. One of them is the judge. The rest children are divided into three groups (it is possible that some group is empty). You don’t know who is the judge, or how the children are grouped. Then the children start playing Rochambeau game for M rounds. Each round two children are arbitrarily selected to play Rochambeau for one once, and you will be told the outcome while not knowing which gesture the children presented. It is known that the children in the same group would present the same gesture (hence, two children in the same group always get draw when playing) and different groups for different gestures. The judge would present gesture randomly each time, hence no one knows what gesture the judge would present. Can you guess who is the judge after after the game ends? If you can, after how many rounds can you find out the judge at the earliest?

Input

Input contains multiple test cases. Each test case starts with two integers N and M (1 ≤ N ≤ 500, 0 ≤ M ≤ 2,000) in one line, which are the number of children and the number of rounds. Following are M lines, each line contains two integers in [0, N) separated by one symbol. The two integers are the IDs of the two children selected to play Rochambeau for this round. The symbol may be “=”, “>” or “<”, referring to a draw, that first child wins and that second child wins respectively.

Output

There is only one line for each test case. If the judge can be found, print the ID of the judge, and the least number of rounds after which the judge can be uniquely determined. If the judge can not be found, or the outcomes of the M rounds of game are inconsistent, print the corresponding message.

Sample Input

3 3
0<1
1<2
2<0
3 5
0<1
0>1
1<2
1>2
0<2
4 4
0<1
0>1
2<3
2>3
1 0
Sample Output

Can not determine
Player 1 can be determined to be the judge after 4 lines
Impossible
Player 0 can be determined to be the judge after 0 lines
Source

Baidu Star 2006 Preliminary
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/

猜你喜欢

转载自blog.csdn.net/qq_39599067/article/details/80704003