标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12676 | Accepted: 5368 |
Description
Input
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
/* * Author: sweat123 * Created Time: 2016/6/21 20:07:00 * File Name: main.cpp */ #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<time.h> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 5010; struct node{ int to; int next; }edge[MAXN<<2]; int pre[MAXN],vis[MAXN],pa[MAXN],dfn[MAXN],low[MAXN],n,m,ind; int px[MAXN],py[MAXN]; int pcnt; void add(int x,int y){ edge[ind].to = y; edge[ind].next = pre[x]; pre[x] = ind ++; } int find(int x){ if(pa[x] != x)pa[x] = find(pa[x]); return pa[x]; } void dfs(int rt,int k,int fa){ dfn[rt] = low[rt] = k; for(int i = pre[rt]; i != -1; i = edge[i].next){ int t = edge[i].to; if(!dfn[t] && t != fa){ dfs(t,k+1,rt); low[rt] = min(low[rt],low[t]); if(low[t] > dfn[rt]){ px[pcnt] = rt,py[pcnt++] = t; } else { int fx = find(t); int fy = find(rt); if(pa[fx] != pa[fy]){ pa[fx] = fy; } } } else if(t != fa){//bridge is differenet from point low[rt] = min(low[rt],dfn[t]); } } } int d[MAXN],f[MAXN]; int main(){ while(~scanf("%d%d",&n,&m)){ ind = 0; pcnt = 0; memset(pre,-1,sizeof(pre)); for(int i = 1; i <= m; i++){ int x,y; scanf("%d%d",&x,&y); add(x,y),add(y,x); } for(int i = 1; i <= n; i++){ pa[i] = i; } memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); dfs(1,1,-1); //for(int i = 1; i <= n; i++){ //cout<<dfn[i]<<‘ ‘<<low[i]<<endl; //} //cout<<endl; memset(d,0,sizeof(d)); memset(f,-1,sizeof(f)); int pnum = 0; for(int i = 1; i <= n; i++){ int fx = find(i); if(f[fx] == -1)f[fx] = ++pnum; f[i] = f[fx]; } for(int i = 0; i < pcnt; i++){ d[f[px[i]]] ++,d[f[py[i]]] ++; } int ans = 0; for(int i = 1; i <= pnum; i++){ if(d[i] == 1)ans ++; } printf("%d\n",(ans + 1) / 2); } return 0; }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/5604988.html