Codeforces Beta Round #94 (Div. 2 Only) B
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
In the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.
In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.
In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
题意大概:Anna and Maria管理俱乐部,然后发给小学生鞋带如果鞋子上只跟一个人捆绑那就惩罚他,然后把它踢出去,问最多几轮踢走只剩一人,应该不用理单独的拿一些(例子二)
分析:依据题意,每次都要先处理鞋带为一的(因为是无向图,所以入度出度无所谓了),放进队列中然后依此除掉相应的其他人与他的联系然后累计;
#include<iostream>//拓扑排序
#include<vector>
#include<queue>
#define maxn 110
using namespace std;
int n,m,u,v;
int val[maxn];
vector<int> to[maxn];
queue<int>que;
int toposort()
{
int ans=0;
bool button=true;
while(button)
{
button=false;
for(int i=1;i<=n;i++)
{
if(val[i]==1)
{
que.push(i);
button=true;
}
}
while(!que.empty())
{
int temp=que.front();
que.pop();
val[temp]--;
for(int i=0;i<to[temp].size();i++)
{
val[to[temp][i]]--;
}
}
if(button)
ans++;
}
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
while(cin>>n>>m)
{
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
val[u]++;val[v]++;
to[u].push_back(v);
to[v].push_back(u);
}
printf("%d\n",toposort());
}
return 0;
}