这道题和poj 3352差不多的,只不过有重边,这样的话其实不用管,对所有的边都建边就行了,不过貌似poj的数据很弱,反正对于
2 2
1 2
2 1
这种数据我是输出0的,别人的有输出1的都过了。
#include <stdio.h>
#include <string.h>
const int maxn = 5005;
const int maxm = 10005;
struct EDGE{
int to, vis, next;
}edge[maxm<<1];
struct GEDGE{
int u, to;
}gedge[maxm];
int head[maxn], dfn[maxn], low[maxn], belo[maxn], st[maxn];
int E, time, top, gtot, type;
void newedge(int u, int to) {
edge[E].to = to;
edge[E].vis = 0;
edge[E].next = head[u];
head[u] = E++;
}
int min(int a, int b) {
return a > b ? b : a;
}
void dfs(int u) {
dfn[u] = low[u] = ++time;
st[++top] = u;
for(int i = head[u];i != -1;i = edge[i].next) {
if(edge[i].vis) continue;
edge[i].vis = edge[i^1].vis = 1;
int to = edge[i].to;
if(!dfn[to]) {
dfs(to);
low[u] = min(low[u], low[to]);
if(low[to] > dfn[u]) {
int v;
type++;
do {
v = st[top--];
belo[v] = type;
}while(v != to);
gedge[gtot].u = u;
gedge[gtot++].to = to;
}
}
else
low[u] = min(low[u], low[to]);
}
}
void init() {
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(belo, 0, sizeof(belo));
E = time = top = gtot = type = 0;;
}
int ans = 0;
int DFS(int u, int pre) {
int tot = 0;
if(pre != -1) tot++;
for(int i = head[u];i != -1;i = edge[i].next) {
int to = edge[i].to;
if(to != pre) {
tot++;
DFS(to, u);
}
}
if(tot == 1) ans++;
return tot;
}
int main() {
init();
int i, n, m, u, to;
scanf("%d%d", &n, &m);
for(i = 0;i < m ;i++) {
scanf("%d%d", &u, &to);
newedge(u, to);
newedge(to, u);
}
for(i = 1;i <= n; i++) if(!dfn[i])
dfs(i);
memset(head, -1, sizeof(head));
E = 0;
for(i = 0;i < gtot; i++) {
u = gedge[i].u;
to = gedge[i].to;
newedge(belo[u], belo[to]);
newedge(belo[to], belo[u]);
}
DFS(0, -1);
printf("%d\n", (ans+1)/2);
return 0;
}