B. Students and Shoelaces
2013-11-22 22:37
423 查看
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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 tom.
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.
Output
Print the single number — the number of groups of students that will be kicked out from the club.
Sample test(s)
input
output
input
output
input
output
Note
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.
解题说明:n个人,m对关系。每次能删除当前所有只跟一个人有关系的人,删到不能删要搞多少次,可以用暴力来做,用数组保存节点,循环判断。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int n, m, i, j;
int a[100][2], b[100] = {0};
scanf("%d %d", &n, &m);
for (i = 0; i < m; i++)
{
scanf("%d %d", &a[i][0], &a[i][1]);
a[i][0]--;
a[i][1]--;
}
for (i = 0; ; i++)
{
int f = 0;
int c[100] = {0};
for (j = 0; j < m; j++)
{
if (b[a[j][0]] == 0 && b[a[j][1]] == 0)
{
c[a[j][0]]++;
c[a[j][1]]++;
}
}
for (j = 0; j < n; j++)
{
if (c[j] == 1)
{
f = 1;
b[j] = 1;
}
}
if (f == 0)
{
break;
}
}
printf("%d\n", i);
return 0;
}
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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 tom.
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.
Output
Print the single number — the number of groups of students that will be kicked out from the club.
Sample test(s)
input
3 3 1 2 2 3 3 1
output
0
input
6 3 1 2 2 3 3 4
output
2
input
6 5 1 4 2 4 3 4 5 4 6 4
output
1
Note
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.
解题说明:n个人,m对关系。每次能删除当前所有只跟一个人有关系的人,删到不能删要搞多少次,可以用暴力来做,用数组保存节点,循环判断。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int n, m, i, j;
int a[100][2], b[100] = {0};
scanf("%d %d", &n, &m);
for (i = 0; i < m; i++)
{
scanf("%d %d", &a[i][0], &a[i][1]);
a[i][0]--;
a[i][1]--;
}
for (i = 0; ; i++)
{
int f = 0;
int c[100] = {0};
for (j = 0; j < m; j++)
{
if (b[a[j][0]] == 0 && b[a[j][1]] == 0)
{
c[a[j][0]]++;
c[a[j][1]]++;
}
}
for (j = 0; j < n; j++)
{
if (c[j] == 1)
{
f = 1;
b[j] = 1;
}
}
if (f == 0)
{
break;
}
}
printf("%d\n", i);
return 0;
}
相关文章推荐
- 关于win8的flash问题
- IOS 作业项目(4)步步完成 画图 程序(剧终)
- test.c
- POJ1741 Tree dp+树的点分治(楼天成推荐男人必做八题之一)
- 谈谈面向对象
- 菜根谭#7
- 深度优先生成树(无向图,邻接矩阵,DFS)
- Ubuntu更新源
- StringGrid列的插入、删除、排序
- 在java中使用mysql数据库
- win32串口同步、异步编程
- 迷人的hessian
- Hex Dump In Many Programming Languages
- 文件对比
- linux中查看java进程
- vmware 10 注册码
- 不是我说的
- 在linux下,完全杀死tomcat等进程脚本
- 视频背景差分检测
- plsql从入门到精通