1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; long long read() { long long x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int N=1000+10; vector <int> e[N],e2[N]; int dfn[N],dfn_to,low[N],mstack[N],top,belong[N],cnt; bool vis[N],InStack[N]; void Tarjan(int now,int father) { vis[now]=InStack[now]=true; mstack[++top]=now; dfn[now]=low[now]=++dfn_to; for(int i=0;i<int(e[now].size());i++) if(vis[e[now][i]]==false) { Tarjan(e[now][i],now); low[now]=min(low[now],low[e[now][i]]); } else if(e[now][i]!=father and InStack[e[now][i]]==true) low[now]=min(low[now],dfn[e[now][i]]); if(low[now]==dfn[now]) { cnt++; while(mstack[top+1]!=now) InStack[mstack[top]]=false, belong[mstack[top--]]=cnt; } } int n,m; int dfs(int now,int father) { vis[now]=true; int t_ans=0; for(int i=0;i<int(e2[now].size());i++) if(e2[now][i]!=father) t_ans+=dfs(e2[now][i],now); if(e2[now].size()==1) t_ans++; if(e2[now].size()==0) t_ans+=2; return t_ans; } int main() { for(int o=1;;o++) { if(scanf("%d%d",&n,&m)==EOF) break;
for(int i=0;i<=n;i++) e[i].clear(),e2[i].clear(); for(int i=1;i<=n;i++) e[i].reserve(4),e2[i].reserve(4); for(int i=1;i<=m;i++) { int s=read(),t=read(); e[s].push_back(t); e[t].push_back(s); }
memset(vis,0,sizeof vis); memset(mstack,0,sizeof mstack); dfn_to=cnt=0; for(int i=1;i<=n;i++) if(vis[i]==false) Tarjan(i,i);
for(int i=1;i<=n;i++) for(int j=0;j<int(e[i].size());j++) if(belong[i]!=belong[e[i][j]]) e2[belong[i]].push_back(belong[e[i][j]]); memset(vis,0,sizeof vis); if(cnt==1) printf("0\n"); else { int ans=0; for(int i=1;i<=n;i++) if(vis[belong[i]]==false) ans+=dfs(belong[i],belong[i]); printf("%d\n",ans/2+ans%2); } } return 0; }
|