BZOJ - 1015 逆向并查集
Posted NWU_ACM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ - 1015 逆向并查集相关的知识,希望对你有一定的参考价值。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <bitset> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <cmath> 10 #include <list> 11 #include <set> 12 #include <map> 13 #define rep(i,a,b) for(int i = a;i <= b;++ i) 14 #define per(i,a,b) for(int i = a;i >= b;-- i) 15 #define mem(a,b) memset((a),(b),sizeof((a))) 16 #define FIN freopen("in.txt","r",stdin) 17 #define FOUT freopen("out.txt","w",stdout) 18 #define IO ios_base::sync_with_stdio(0),cin.tie(0) 19 #define mid ((l+r)>>1) 20 #define ls (id<<1) 21 #define rs ((id<<1)|1) 22 #define N 400010 23 #define INF 0x3f3f3f3f 24 #define INFF ((1LL<<62)-1) 25 using namespace std; 26 typedef long long LL; 27 typedef pair<int, int> PIR; 28 const double eps = 1e-8; 29 30 int n, m, u, v, t, k[N], f[N], ans[N]; 31 vector <int> G[N]; 32 bool vis[N]; 33 int getfa(int x){ 34 return f[x] == x ? x : f[x] = getfa(f[x]); 35 } 36 int main() 37 { 38 //FIN; 39 scanf("%d %d", &n, &m); 40 rep(i, 1, m){ 41 scanf("%d %d", &u, &v); 42 G[u].push_back(v); 43 G[v].push_back(u); 44 } 45 scanf("%d", &t); 46 rep(i, 1, t) { scanf("%d", &k[i]); vis[k[i]] = true; } 47 rep(i, 0, n) f[i] = i; 48 49 int res = n-t; 50 rep(i, 0, n-1){ 51 if(vis[i]) continue; 52 int x = getfa(i); 53 rep(j, 0, (int)G[i].size()-1){ 54 if(vis[G[i][j]]) continue; 55 int y = getfa(G[i][j]); 56 if(x != y) { f[y] = x; res--; } 57 } 58 } 59 /*int res = 0; 60 rep(i, 0, n-1) { if(!vis[i] && getfa(i) == i) res++; }*/ 61 per(i, t, 0){ 62 ans[i] = res++; 63 vis[k[i]] = false; 64 int x = getfa(k[i]); 65 rep(j, 0, (int)G[k[i]].size()-1){ 66 if(vis[G[k[i]][j]]) continue; 67 int y = getfa(G[k[i]][j]); 68 if(x != y){ 69 f[y] = x; 70 res--; 71 } 72 } 73 } 74 rep(i, 0, t) printf("%d\n", ans[i]); 75 return 0; 76 }
以上是关于BZOJ - 1015 逆向并查集的主要内容,如果未能解决你的问题,请参考以下文章