poj2912 带权并查集,类似食物链

来源:互联网 发布:二维数组回形遍历 编辑:程序博客网 时间:2024/05/16 02:42

真是看不懂题目,看题解才知道它要干嘛

枚举裁判。。

如果只有一个裁判,输出确定他人不是裁判所需最大行数,用数组error记录

如果没有裁判输出impossible,多个裁判不确定

注意unionset推的关系

#include <iostream>#include<stdio.h>#include<math.h>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#include<stack>using namespace std;#define FOR(i,j,k) for(int i=j;i<=k;i++)#define mod 1000000007#define N 510int f[N];int r[N];int error[N];struct node{    int u,v,w;}edge[2010];int n;int find(int x){    if(x==f[x])return f[x];    int t=f[x];    f[x]=find(f[x]);    r[x]=(r[t]+r[x])%3;    return f[x];}bool unionset(int a,int b,int w){    int aa=find(a),bb=find(b);    if(aa==bb)    {        if((r[a]+w)%3==r[b])            return true;        else return false;    }    f[bb]=aa;    r[bb]=(r[a]+w-r[b]+3)%3;    return true;}void init(){    FOR(i,1,n)    f[i]=i;    memset(r,0,sizeof(r));}int main(){    int m;    while(scanf("%d%d",&n,&m)!=EOF)    {        char op;        FOR(i,1,m)        {            scanf("%d%c%d",&edge[i].u,&op,&edge[i].v);            edge[i].u++,edge[i].v++;            if(op=='=')                edge[i].w=0;            if(op=='<')                edge[i].w=1;            if(op=='>')                edge[i].w=2;        }        memset(error,0,sizeof(error));        FOR(i,1,n)                          //枚举i为裁判        {            init();            FOR(j,1,m)            {                if(edge[j].u==i||edge[j].v==i) continue;                if(unionset(edge[j].u,edge[j].v,edge[j].w)==false)                {                    error[i]=j;                    break;                }            }        }        int ans=0;        int ans2=0;        int shu=0;        FOR(i,1,n)        {            //cout<<i<<" "<<error[i]<<endl;            if(error[i]==0)                ans=i,shu++;            ans2=max(ans2,error[i]);        }        ans--;        if(shu>1)            printf("Can not determine\n");        if(shu==0)            printf("Impossible\n");        if(shu==1)            printf("Player %d can be determined to be the judge after %d lines\n",ans,ans2);    }    return 0;}


0 0
原创粉丝点击