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
| #include<bits/stdc++.h> #define maxn 10007 #define M 100007 using namespace std; int n,m,cent=1,t,head[maxn],low[maxn],dfn[maxn],ans1,ans2; int stackk[maxn],cnt,top,root,cut[maxn],col[maxn],vis[maxn]; int ol; vector<int >dcc[maxn]; struct node{ int next,to; }edge[M*2];
inline void add(int u,int v){ edge[++cent]=(node){head[u],v};head[u]=cent; }
void Tarjan(int x,int fa){ dfn[x]=low[x]=++t; stackk[++top]=x; if(x==root&&head[x]==0){ dcc[++cnt].push_back(x); return ; } for(int i=head[x];i;i=edge[i].next){ int y=edge[i].to; if(!dfn[y]){ Tarjan(y,i); low[x]=min(low[x],low[y]); if(low[y]>=dfn[x]){ if(low[y]>dfn[x]) ans1++; int z;cnt++; do{ z=stackk[top--]; dcc[cnt].push_back(z); }while(z!=y); dcc[cnt].push_back(x); } }else if((i^1)!=fa) low[x]=min(low[x],dfn[y]); } }
void clear(){ ans1=ans2=top=cnt=t=ol=0;cent=1; memset(head,0,sizeof head); memset(low,0,sizeof low); memset(dfn,0,sizeof dfn); memset(stackk,0,sizeof stackk); memset(dcc,0,sizeof dcc); memset(col,0,sizeof col); memset(edge,0,sizeof edge); }
int main(){
while(scanf("%d%d",&n,&m)){ if(n==0&&m==0)break; clear(); int a,b; for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); add(a+1,b+1),add(b+1,a+1); } for(int i=1;i<=n;i++){ if(!dfn[i]) root=i,Tarjan(i,-1); } for(int i=1;i<=cnt;i++,ol=0){ for(int j=0;j<dcc[i].size();j++){ vis[dcc[i][j]]=1; } for(int k=0;k<dcc[i].size();k++){ for(int j=head[dcc[i][k]];j;j=edge[j].next){ if(vis[edge[j].to]) ol++; } } if(ol/2>dcc[i].size()) ans2+=ol/2; memset(vis,0,sizeof(vis)); } printf("%d %d\n",ans1,ans2); } }
|