kuangbin_UnionFind J (POJ 2492)

时间:2023-03-09 22:52:05
kuangbin_UnionFind J (POJ 2492)

加权并查集mod2模板 本体的难点是bug的释义(笑)

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std; int father[],sum[];
int find(int x)
{
if(father[x] != x) {
int tmp = father[x];
father[x] = find(tmp);
sum[x] = (sum[x] + sum[tmp]) % ;
}
return father[x];
}
void merge(int x,int y)
{
int tx = find(x);
int ty = find(y);
//printf("x = %d y = %d\ntx = %d ty = %d\n", x, y, tx, ty);
if(tx == ty) return;
father[tx] = ty;
//printf("father(1) = %d\n",father[1]);
sum[tx] = (sum[x] - sum[y] + ) % ;
return;
}
int main()
{
int t;
scanf("%d",&t);
for(int kase = ; kase <= t; kase++)
{
int n, m;
bool flag = true;
scanf("%d%d", &n, &m);
//Initiate
for(int i = ; i <= n; i++)
{
father[i] = i;
sum[i] = ;
}
while(m--)
{
int a, b;
scanf("%d%d", &a, &b);
if(find(a) == find(b))
{
if(sum[a] != (sum[b] + ) % ){
//printf("a = %d b = %d\n", a, b);
flag = false;
}
}
else
merge(a, b);
}
printf("Scenario #%d:\n", kase);
if(flag) printf("No suspicious bugs found!\n\n");
else printf("Suspicious bugs found!\n\n");
}
return ;
}