杭电1829 A Bug's Life

来源:互联网 发布:mac最好的翻墙工具 编辑:程序博客网 时间:2024/06/05 03:36
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 100002using namespace std;int father[MAX],rank[MAX];void init(int n){    for(int i = 1; i <=n; i ++)        father[i] = i;    memset(rank,0,sizeof(rank));}int find(int x){    if(x!=father[x])    {        int temp = father[x];        father[x] = find(father[x]);        rank[x] = (rank[x] + rank[temp])%2;        //此式只的向量指向是  根 -> temp == rank[temp]        //temp - > x == rank[x]        //根 --> x   == 根 --> temp --> x;    }    return father[x];}int Union(int x,int y){    int a = find(x);    int b = find(y);    if(a == b) return 1;    father[a] = b;    rank[a] = (rank[y]+1-rank[x])%2;   //必须知道向量关系是根指向叶子节点。    // b -- > y == rank[y], y -->x  == d x --> a == -rank[x]    //b - >y ->x -> b;    return 0;}int main(){    int a,b,m,n,stu,ans;    scanf("%d",&n);        for(int i = 1; i <=n; i ++)        {            cin>>stu>>m;            init(stu);            ans = 0;            for(int j = 1; j <=m; j++)            {                scanf("%d %d",&a,&b);                if(find(a)==find(b))                {                    if(rank[a]==rank[b])                        ans = 1;                }                else                    Union(a,b);            }            if(ans == 1)                printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);            else                printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);        }    return 0;}#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int M = 4005;int Set[M];int T, n,m;void init() {    for(int i = 1; i <= n*2; i++) {        Set[i] = i;    }}int Find(int x) {    return x == Set[x] ? x : Set[x] = Find(Set[x]);}void Union(int a, int b) {    a = Find(a);    b = Find(b);    if(a != b) {        if(a > b)            Set[a] = b;        else            Set[b] = a;    }}int main(){    int a, b;    int flag;    int q = 1;    scanf("%d", &T);    while(T--) {        scanf("%d%d", &n, &m);        init();        flag = 1;        for(int i = 0; i < m; i++) {            scanf("%d%d", &a, &b);                if(Find(a) == Find(b)) {                    flag = 0;                } else {                    Union(a, b + n);                    Union(a+n, b);                }            }        printf("Scenario #%d:\n", q++);        if(flag)            printf("No suspicious bugs found!\n");        else            printf("Suspicious bugs found!\n");            printf("\n");    }    return 0;}


                                             
0 0
原创粉丝点击