P1197 [JSOI2008]星球大战

  1 #include
  2 #include
  3 #define debug(i) cout<<"(i): "<  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]星球大战)