UVALive - 3644 X-Plosives (并查集)

思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断。


AC代码:

//#define LOCAL
#include 
#include 
const int maxn = 1e5 + 5;
int par[maxn], rank[maxn];

void init() {
    memset(rank, 0, sizeof(rank));
    for(int i = 0; i <= maxn; i++) {
        par[i] = i;
    }
}

int findRoot(int x) {
    return x != par[x] ? par[x] = findRoot(par[x]) : x;
}

//启发式合并
void unionSet(int x, int y) {
    if(rank[x] > rank[y]) {
        par[y] = x;
    } else {
        par[x] = y;
        if(rank[x] == rank[y]) rank[y]++;
    }
}

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL

    int x, y, refuse = 0;
    init();
    while(scanf("%d", &x) == 1) {
        if(x == -1) {
            printf("%d\n", refuse);
            init();
            refuse = 0;
        }else {
            scanf("%d", &y);
            x = findRoot(x);
            y = findRoot(y);
            if(x == y) {
                //refuse it
                refuse++;
            } else {
                //union
                unionSet(x, y);
            }
        }
    }
    return 0;
}

如有不当之处欢迎指出!

你可能感兴趣的:(算法之路)