Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.
To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.
Determine how many groups of students will be kicked out of the club.
The first line contains two integers n and m — the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b — the numbers of students tied by the i-th lace (1 ≤ a, b ≤ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
Print the single number — the number of groups of students that will be kicked out from the club.
3 3
1 2
2 3
3 1
0
6 3
1 2
2 3
3 4
2
6 5
1 4
2 4
3 4
5 4
6 4
1
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> #define true ture #define false flase using namespace std; #define ll __int64 #define inf 0xfffffff int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } int du[110]; int mapp[110][110]; int gg[110]; int main() { int x,y,z,i,t,q; scanf("%d%d",&x,&q); while(q--) { int u,v; scanf("%d%d",&u,&v); du[u]++; du[v]++; mapp[u][v]=mapp[v][u]=1; } int ans=0; while(1) { int ji=0; for(i=1;i<=x;i++) { if(du[i]==1) gg[ji++]=i; } for(i=0;i<ji;i++) { for(t=1;t<=x;t++) { if(mapp[gg[i]][t]) { mapp[gg[i]][t]=mapp[t][gg[i]]=0; du[gg[i]]--; du[t]--; } } } if(!ji) break; ans++; } printf("%d ",ans); return 0; }