1. 程式人生 > >【codevs 1995】黑魔法師之門

【codevs 1995】黑魔法師之門

嗯……
通過樣例看出來,答案要麼不變,要麼*2+1
然後
發現如果新加的這條邊same(l[i].f,l[i].t),就*2+1
證明以後再說,機房要關門了……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 200000 + 5;
const int P = 1000000009;
int n,m,p;
int fa[MAXN];
int rank[MAXN];
void init()
{
    for
(int i = 0;i <= n;i ++) fa[i] = i; memset(rank,0,sizeof(rank)); return; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int x,int y) { x = find(x); y = find(y); if(x == y) return; if(rank[x] > rank[y]) swap(x,y); fa[x] = y; if
(rank[x] == rank[y]) rank[y] ++; return; } bool same(int x,int y) { return find(x) == find(y); } int f,t; int k; int main() { scanf("%d %d",&n,&m); init(); while(m --) { scanf("%d %d",&f,&t); if(same(f,t)) k = (k << 1) + 1
,k %= P; printf("%d\n",k); merge(f,t); } return 0; }