标签:
Time Limit: 20 Sec
Memory Limit: 256 MB
http://www.lydsy.com/JudgeOnline/problem.php?id=1015
Input
Output
输出文件的第一行是开始时星球的连通块个数。接下来的N行,每行一个整数,表示经过该次打击后现存星球的连通块个数。
Sample Input
8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
Sample Output
1
1
1
2
3
3
题意
题解:
倒着并查集就好了~
直接搞
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <map> #include <stack> typedef long long ll; using namespace std; #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1000000 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; //const int inf=0x7fffffff; //нчоч╢С const int inf=0x3f3f3f3f; /* inline void P(int x) { Num=0;if(!x){putchar(‘0‘);puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } */ inline ll read() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar(‘0‘);puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** const int N = 500000, M = 500000; int key[M], head[N], next[M], cnt; int a[N], p[N], ans[N]; bool v[N]; inline void add(const int & x, const int & y) { key[cnt] = y; next[cnt] = head[x]; head[x] = cnt ++; } inline int F (int x) { return p[x] == x ? x : p[x] = F (p[x]); } int main () { int n, m;memset (head, -1, sizeof head); scanf ("%d%d", &n, &m); for (int i = 1, a, b; i <= m; i ++) scanf ("%d%d", &a, &b), add (a, b), add (b, a);int k; scanf ("%d", &k); for (int i = 1; i <= k; i ++) scanf ("%d", &a[i]), v[a[i]] = true; for (int i = 1; i <= n; i ++) p[i] = i; int z = n - k; for (int i = 0; i < n; i ++) if (!v[i]) for (int j = head[i]; ~ j; j = next[j]) if (!v[key[j]]) if (F (i) != F (key[j])) p[F (i)] = F (key[j]), z --; ans[k + 1] = z; for (int i = k; i >= 1; i --) { v[a[i]] = false; z ++; for (int j = head[a[i]]; ~ j; j = next[j]) if (!v[key[j]]) if (F (a[i]) != F (key[j])) p[F (a[i])] = F (key[j]), z --; ans[i] = z; } for (int i = 1; i <= k + 1; i ++) printf ("%d\n", ans[i]); return 0; }
bzoj 1015: [JSOI2008]星球大战starwar 并查集
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/4571434.html