hdu 3926

来源:互联网 发布:关于网络暴力调查报告 编辑:程序博客网 时间:2024/06/16 11:22
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 10010int fa[N],num[N],mark[N];int nq[N];void Make_Set(){    for(int i = 0; i < N; i++){        num[fa[i] = i] = 1;        mark[i]  = 0;    }}int FindSet(int x){    return fa[x] != x? fa[x] = FindSet(fa[x]):fa[x];}void Union(int a,int b){    a = FindSet(a); b = FindSet(b);    if(a != b){        fa[a] = b;        num[b] += num[a];    }else {        mark[a] = 1;    }}struct node{    int flag, t;}v[N],w[N];bool Comp(const struct node &a, const struct node &b){    if(a.t != b.t){        return a.t < b.t;    }    else {        return a.flag < b.flag;    }}int top1, top2;void built(int n,struct node *cnt,int &top){    top = 0;    memset(nq,0,sizeof(nq));//    cout << "n=" << n <<endl;    for(int i = 1; i <= n; i++){        int index = FindSet(i);        if(nq[index] == 0){            nq[index] = 1;            cnt[top].t = num[index];            cnt[top].flag = mark[index];            top++;        }    }}int judge(){    sort(v,v+top1,Comp);    sort(w,w+top2,Comp);    if(top1 != top2) {        return 0;    }//    cout << top2 << " " << top1 << endl;    for(int i = 0; i < top1; i++){               if(v[i].t != w[i].t || v[i].flag != w[i].flag){            return 0;        }    }    return 1;}int main(){//    freopen("in","r",stdin);    int caseNumber;    scanf("%d",&caseNumber);    for(int cas  = 1; cas <= caseNumber; cas ++){        int m1, m2;        for(int i=0; i < 2; i++){            int n, m ;            scanf("%d%d",&n,&m);            Make_Set();            while(m--){                int u, v;                scanf("%d%d",&u,&v);                Union(u,v);            }            if(i == 0) {                built(n,v,top1);            }            else {                built(n,w,top2);            }        }        if(judge()){            printf("Case #%d: YES\n",cas);        }        else {            printf("Case #%d: NO\n",cas);        }    }    return 0;}

原创粉丝点击