标签:ural 并查集 离线 anansi-s cobweb
传送门
大意:给出一个无向图,删除Q条边,每删除一次就询问一次目前的连通块的数目。
思路:离线搞, 把删边转换为加边,每加一次边,若两个顶点不连通就用并查集把着这两个连通块合并。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100005
using namespace std;
int n, m, q;
int s[MAXN], t[MAXN];
int ban[MAXN], fa[MAXN], st[MAXN];
bool f[MAXN], g[MAXN];
int rt(int u)
{
if(fa[u] != u) return fa[u] = rt(fa[u]);
return u;
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
memset(ban, 0, sizeof ban);
memset(fa, 0, sizeof fa);
memset(st, 0, sizeof st);
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
for(int i = 1; i <= m; i ++)
scanf("%d%d", &s[i], &t[i]);
for(int i = 1; i <= n; i ++)
fa[i] = i;
scanf("%d", &q);
for(int i = 1; i <= q; i ++)
{
scanf("%d", ban + i);
f[ban[i]] = 1;
}
int ans = n;
for(int i = 1; i <= m; i ++)
if(!f[i])
{
int t1 = rt(s[i]), t2 = rt(t[i]);
if(t1 != t2) fa[t1] = t2, ans --;
}
for(int i = q; i > 0; i --)
{
st[i] = ans;
int t1 = rt(s[ban[i]]), t2 = rt(t[ban[i]]);
if(t1 != t2) fa[t1] = t2, ans --;
}
printf("%d", st[1]);
for(int i = 2; i <= q; i ++)
printf(" %d", st[i]);
putchar(‘\n‘);
}
return 0;
}
版权声明:请随意转载O(∩_∩)O
URAL1671 Anansi's Cobweb(离线做 + 并查集)
标签:ural 并查集 离线 anansi-s cobweb
原文地址:http://blog.csdn.net/gengmingrui/article/details/47684187