3352

1 /*
2 我这种方法,其实就是把各个连通分量的点集找出来了
3 */
4
5 // include file
6 #include <cstdio>
7 #include <cstdlib>
8 #include <cstring>
9 #include <cmath>
10 #include <cctype>
11 #include <ctime>
12
13 #include <iostream>
14 #include <sstream>
15 #include <fstream>
16 #include <iomanip>
17 #include <bitset>
18
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 #include <queue>
23 #include <set>
24 #include <list>
25 #include <functional>
26
27 using namespace std;
28
29 // typedef
30 typedef long long LL;
31 typedef unsigned long long ULL;
32 typedef __int64 Bint;
33
34 //
35 #define read freopen("in.txt","r",stdin)
36 #define write freopen("out.txt","w",stdout)
37 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
38 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
39 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
40 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
41 #define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
42 #define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
43 #define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
44
45 #define FF(i,a) for(int i=0;i<(a);i++)
46 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
47
48 #define Z(a) (a<<1)
49 #define Y(a) (a>>1)
50
51 const double eps = 1e-6;
52 const double INFf = 1e10;
53 const int INFi = 1000000000;
54 const double Pi = acos(-1.0);
55
56 template<class T> inline T sqr(T a){return a*a;}
57 template<class T> inline T TMAX(T x,T y)
58 {
59 if(x>y) return x;
60 return y;
61 }
62 template<class T> inline T TMIN(T x,T y)
63 {
64 if(x<y) return x;
65 return y;
66 }
67 template<class T> inline void SWAP(T &x,T &y)
68 {
69 T t = x;
70 x = y;
71 y = t;
72 }
73 template<class T> inline T MMAX(T x,T y,T z)
74 {
75 return TMAX(TMAX(x,y),z);
76 }
77
78 #define MAXN 5010
79 int N,M;
80
81 // 邻接表
82 struct node1
83 {
84 int e;
85 int next;
86 };
87 struct node2
88 {
89 int next;
90 };
91 node1 mem[MAXN*4];
92 node2 G[MAXN];
93
94 // 并查集
95 int father[MAXN];
96 int rank[MAXN];
97
98 // Edge_Bcc_Tarjan
99 bool used[MAXN];
100 int dfn[MAXN];
101 int low[MAXN];
102 int deg[MAXN];
103 int cnt;
104
105 void Init()
106 {
107 FORi(1,N+1,1)
108 {
109 father[i] = i;
110 rank[i] = 1;
111 }
112 }
113
114 int Find(int i)
115 {
116 if(father[i]!=i)
117 father[i] = Find(father[i]);
118 return father[i];
119 }
120
121 void Joint(int a,int b)
122 {
123 a = Find(a);
124 b = Find(b);
125 if(rank[a]<=rank[b])
126 {
127 father[a] =b;
128 rank[b]+=rank[a];
129 }
130 else
131 {
132 father[b] = a;
133 rank[a]+=rank[b];
134 }
135 }
136
137 // 加边
138 void Add_Edge(int lo,int s,int e)
139 {
140
141 int dx=G[s].next;
142 while(dx!=-1)
143 {
144 if(mem[dx].e==e) return ;
145 dx=mem[dx].next;
146 }
147
148 mem[lo].e = e;
149 mem[lo].next = G[s].next;
150 G[s].next = lo;
151 }
152
153 //
154 void Edge_BCC_Tarjan(int i,int fa)
155 {
156 dfn[i] = cnt;
157 low[i] = cnt;
158 cnt++;
159 used[i] = true;
160
161 int dx = G[i].next;
162 while(dx!=-1)
163 {
164 int v = mem[dx].e;
165 if(!used[v])
166 {
167 Edge_BCC_Tarjan(v,i);
168 low[i] = TMIN(low[i],low[v]);
169
170 if(low[v]<=low[i])
171 {
172 Joint(v,i);
173 }
174 }
175 else if(v!=fa)
176 {
177 low[i] = TMIN(low[i],dfn[v]);
178 }
179 dx = mem[dx].next;
180 }
181 }
182
183 void Shrink(int &bcc)
184 {
185 int dx,a,b;
186 FORi(1,N+1,1)
187 {
188 dx = G[i].next;
189 a = Find(i);
190 while(dx!=-1)
191 {
192 b = Find(mem[dx].e);
193 if(a!=b)
194 {
195 deg[b]++;
196 if(b>bcc) bcc=b;
197 }
198 dx = mem[dx].next;
199 }
200 }
201 }
202
203 int main()
204 {
205 read;
206 write;
207 int a,b;
208 while(scanf("%d %d",&N,&M)!=-1)
209 {
210 FORi(1,N+1,1)
211 {
212 G[i].next = -1;
213 }
214 FORi(0,M,1)
215 {
216 scanf("%d %d",&a,&b);
217 Add_Edge(2*i+0,a,b);
218 Add_Edge(2*i+1,b,a);
219 }
220 Init();
221 memset(used,0,sizeof(used));
222 memset(dfn,0,sizeof(dfn));
223 memset(low,0,sizeof(low));
224 cnt = 1;
225 FORi(1,N+1,1)
226 {
227 if(!used[i])
228 Edge_BCC_Tarjan(i,-1);
229 }
230 int bcc=-1,ans=0;
231 memset(deg,0,sizeof(deg));
232 Shrink(bcc);
233 FORi(1,bcc+1,1)
234 {
235 if(deg[i]==1)
236 ans++;
237 }
238 printf("%d\n",(ans+1)/2);
239 }
240 return 0;
241 }
posted @ 2011-03-11 20:19  AC2012  阅读(473)  评论(0编辑  收藏  举报