码迷,mamicode.com
首页 > Web开发 > 详细

P1197 [JSOI2008]星球大战

时间:2019-10-09 14:02:01      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:add   col   ide   des   include   show   bool   play   names   

技术图片
  1 #include<iostream>
  2 #include<cstring>
  3 #define debug(i) cout<<"(i): "<<i<<endl
  4 using namespace std;
  5 
  6 int fa[400010];
  7 
  8 int find(int x)
  9 {
 10     if (fa[x] == x) return x;
 11     fa[x] = find(fa[x]);
 12     return fa[x];
 13 }
 14 
 15 void merge(int a, int b)
 16 {
 17     fa[find(a)] = find(b);
 18 }
 19 
 20 bool check(int a, int b)
 21 {
 22     return find(a) == find(b);
 23 }
 24 
 25 int n, m;
 26 int k;
 27 int ans[400010];
 28 int head[400010];
 29 int cnt;
 30 int be;
 31 
 32 struct Edge
 33 {
 34     int u, v, nxt;
 35 }e[400010];
 36 
 37 int b[400010];
 38 bool des[400010];
 39 
 40 void add(int a, int b)
 41 {
 42     e[cnt].u = a;
 43     e[cnt].nxt = head[a];
 44     head[a] = cnt;
 45     e[cnt++].v = b;
 46 }
 47 
 48 int main()
 49 {
 50     cin >> n >> m;
 51     for (int i = 0; i < n; i++)
 52     {
 53         fa[i] = i;
 54         head[i] = -1;
 55     }
 56     for (int i = 0; i < m; i++)
 57     {
 58         int a, b;
 59         cin >> a >> b;
 60         add(a, b);
 61         add(b, a);
 62     }
 63     cin >> k;
 64     be = n - k;
 65     for (int i = 0; i < k; i++)
 66     {
 67         //debug(i);
 68         cin >> b[i];
 69         des[b[i]] = true;
 70     }
 71     //cout << endl;
 72     for (int i = 0; i < cnt; i++)
 73     {
 74         if (!des[e[i].u] && !des[e[i].v]&&!check(e[i].u,e[i].v))
 75         {
 76             //debug(be);
 77             be--;
 78             merge(e[i].u, e[i].v);
 79         }
 80     }
 81     ans[k] = be;
 82     for (int i = k - 1; i >= 0; i--)
 83     {
 84         int t = b[i];
 85         be++;
 86         des[t] = false;
 87         for (int j = head[t]; j != -1; j = e[j].nxt)
 88         {
 89             if (!des[e[j].v] && !check(t, e[j].v))
 90             {
 91                     be--;
 92                     merge(t, e[j].v);
 93             }
 94         }
 95         ans[i] = be;
 96     }
 97     //cout << endl;
 98     for (int i = 0; i <= k; i++)
 99     {
100         cout << ans[i] << endl;
101     }
102     return 0;
103 }
View Code

 

P1197 [JSOI2008]星球大战

标签:add   col   ide   des   include   show   bool   play   names   

原文地址:https://www.cnblogs.com/thjkhdf12/p/11641354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!