# BZOJ 1015 星球大战starwar 逆向并查集

https://www.lydsy.com/JudgeOnline/problem.php?id=1015

 1 #include<bits/stdc++.h>
2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数，会超时
4 #define Min(a, b) ((a) < (b) ? (a) : (b))
5 #define Mem(a) memset(a, 0, sizeof(a))
6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
8 #define lson ((o)<<1)
9 #define rson ((o)<<1|1)
10 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
11 using namespace std;
12 inline int read()
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19 typedef long long ll;
20 const int maxn = 400000 + 10;
21 const int mod = 100003;//const引用更快，宏定义也更快
22 const int INF = 1e9;
23 int p[maxn];
24 vector<int>G[maxn];
25 bool vis[maxn];
26 stack<int>ans;
27 int Find(int x)
28 {
29     return x == p[x] ? x : p[x] = Find(p[x]);
30 }
31 int a[maxn];
32 int main()
33 {
34     int n, m, u, v;
35     scanf("%d%d", &n, &m);
36     while(m--)
37     {
38         scanf("%d%d", &u, &v);
39         G[u].push_back(v);
40         G[v].push_back(u);
41     }
42     for(int i = 0; i < n; i++)p[i] = i;
43     scanf("%d", &m);
44     for(int i = 0; i < m; i++)scanf("%d", &a[i]), vis[a[i]] = 1;
45     for(int u = 0; u < n; u++)
46     {
47         if(!vis[u])for(int i = 0; i < G[u].size(); i++)
48         {
49             int v = G[u][i];
50             if(!vis[v])
51             {
52                 int x = Find(u), y = Find(v);
53                 p[x] = y;
54             }
55         }
56     }
57     int tot = 0;
58     for(int i = 0; i < n; i++)if(!vis[i])if(p[i] == i)tot++;
59     ans.push(tot);
60     for(int i = m - 1; i >= 0; i--)
61     {
62         int u = a[i], cnt = 0;
63         vis[u] = 0;
64         for(int j = 0; j < G[u].size(); j++)
65         {
66             int v = G[u][j];
67             if(!vis[v])
68             {
69                 int x = Find(u), y = Find(v);
70                 if(x != y)
71                 {
72                     cnt++;
73                     p[x] = y;
74                 }
75             }
76         }
77         tot = tot - cnt + 1;
78         ans.push(tot);
79     }
80     while(!ans.empty())
81     {
82         printf("%d\n", ans.top()), ans.pop();
83     }
84     return 0;
85 }

posted @ 2018-09-11 18:14  _努力努力再努力x  阅读(143)  评论(0编辑  收藏  举报