tag[i] 表示 i 结点 与其父节点是否在同一个集合中(表示的是一种相对关系)
GetFather时:压缩路径,同时更新tag[i]
合并时:更新tag[father[i]]
#include <stdio.h>
#include <iostream>
#include <stack>
#include <string>
#include <set>
#include <memory.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
int father[100005], N, M, T, tag[100005];
void Init()
{
for( int i = 1; i <= N; i++ )
father[i] = i;
for( int i = 1; i <= N; i++ )
tag[i] = 0;
}
int GetFather( int i )
{
if( i == father[i] )
return i;
int tmp = GetFather( father[i] );
tag[i] = (tag[i] + tag[father[i]]) % 2;
father[i] = tmp;
return tmp;
}
int main()
{
scanf("%d", &T);