# [BZOJ 1015] [JSOI 2008] 星球大战starwar

## 1015: [JSOI2008]星球大战starwar

Time Limit: 3 Sec
Memory Limit: 162 MB

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

1
1
1
2
3
3

## HINT

【题解】

 1 #include<stdio.h>
2 using namespace std;
4 bool des[400001];
5 int ans[400001];
6 int getf(int x) {
7     int r=x;
8     while(r!=pre[r]) r=pre[r];
9     int i=x,j;
10     while(i!=r) {
11         j=pre[i];
12         pre[i]=r;
13         i=j;
14     }
15     return r;
16 }
17 void addedge(int u,int v) {
18     ++tot;
19     to[tot]=v;
22 }
23 int main() {
24     scanf("%d%d",&n,&m);
25     for (int i=1;i<=n;++i) pre[i]=i;
26     for (int i=1;i<=m;++i) {
27         scanf("%d%d",&a[i],&b[i]);
28         a[i]++,b[i]++;
31     }
32     scanf("%d",&k);
33     for (int i=1;i<=k;++i) {
34         scanf("%d",&c[i]);
35         c[i]++;
36         des[c[i]]=1;
37     }
38     int ansx=n-k;
39     for (int i=1;i<=n;++i)
40         if(!des[i]) {
41             int f1=getf(i);
43                 if(!des[to[j]]) {
44                     int f2=getf(to[j]);
45                     if(f1!=f2) pre[f2]=f1, ansx--;
46                 }
47             }
48         }
49     ans[k+1]=ansx;
50     for (int i=k;i>=1;--i) {
51         ++ansx;
52         int f1=c[i];
54             if(!des[to[j]]) {
55                 int f2=getf(to[j]);
56                 if(f1!=f2) pre[f2]=f1,--ansx;
57             }
58         ans[i]=ansx;
59         des[c[i]]=0;
60     }
61     for (int i=1;i<=k+1;++i) printf("%d\n",ans[i]);
62     return 0;
63 }
View Code

posted @ 2015-06-22 23:16  TonyFang  阅读(...)  评论(...编辑  收藏