数据生成程序

//数据生成程序
/*
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <set>
using namespace std;

int main() {
    int t = 999;
    srand( time(NULL) );
    freopen("e.txt","w",stdout);
    printf("%d
",t);
    while(t--) {
        int n = rand() % 2000 + 1;
        int m = rand() % 1000 + 1;
        printf("%d %d
", n, m);
        for(int i = 0; i < m; i++) {
            int x = rand() % n + 1;
            int y = rand() % n + 1;
            printf("%d %d
",x, y);
        }
    }
}
*/

//标程
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 2005;
int fa[maxn], r[maxn];

void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        fa[i] = i;
        r[i] = 0;
    }
}

int find(int x)
{
    if(fa[x] == x) return x;
    int fx = fa[x];
    fa[x] = find(fa[x]);
    r[x] = ( r[fx] + r[x] ) % 2;
    return fa[x];
}

void unin(int x, int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
    {
        fa[fy] = fx;
        r[fy] = ( r[x] + r[y] + 1 ) % 2;
    }
}

int main()
{
    int t;
    int n, m;
    int a, b;
    freopen("e.txt","r",stdin);
    freopen("f.txt","w",stdout);
    scanf("%d",&t);
    for(int kase = 1; kase <= t; kase++)
    {
        bool flag = true;
        scanf("%d %d",&n, &m);
        init(n);
        for(int i = 0; i < m; i++){
            scanf("%d %d",&a, &b);
            if(!flag) continue;
            if(find(a) != find(b)) {
                unin(a, b);
            } else {
                if(find(a) == find(b) && r[a] == r[b]){
                    flag = false;
                }
            }
        }
        printf("Scenario #%d:
", kase);
        if(flag) {
            puts("No suspicious bugs found!");
        } else {
            puts("Suspicious bugs found!");
        }
        if(kase != t) puts("");
    }
}
原文地址:https://www.cnblogs.com/zhanzhao/p/3863826.html