【CodeForces 129 B】Students and Shoelaces(拓扑排序)
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 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.
Output
Print the single number — the number of groups of students that will be kicked out from the club.
Examples
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
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.
题意:
Anna和Maria要给小学生发鞋带,如果某位小学生的鞋带只跟一个人绑定,那就要踢出去,单独没和人绑定的不予理会,问最少需要几轮可以使所有人都是孤立的。
思路:
每次先处理鞋带与外界绑定数为1的,由于是无向图,无所谓出度入度,进行拓扑排序的同时,计算所进行的总轮数。
#include<bits/stdc++.h>
using namespace std;
int deg[],n,m;
vector<int>to[];
int topsort()
{
queue<int>qu;
int i,go=,sum=;
while(go)
{
go=;
for(i=;i<=n;i++)
if(deg[i]==)
{
qu.push(i);
go=;
}
while(!qu.empty())
{
int a=qu.front();
qu.pop();
deg[a]--;
for(i=;i<to[a].size();i++)
deg[to[a][i]]--;
}
if(go)sum++;
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
int u,v;
int i,j,k;
cin>>n>>m;
for(i=;i<=m;i++)
{
cin>>u>>v;
deg[u]++;deg[v]++;
to[u].push_back(v);
to[v].push_back(u);
}
cout<<topsort()<<endl;
return ;
}
【CodeForces 129 B】Students and Shoelaces(拓扑排序)的更多相关文章
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- CodeForces 1213F (强联通分量分解+拓扑排序)
传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
随机推荐
- 【Python】directory字典类型
它的基本格式是(key是键,value是值): d = {key1 : value1, key2 : value2 } Example dir = {'Mic':1,'Sun':2} for k in ...
- jquery attr和prop区别
<input type="checkbox" /> <script> $(function() { $('input').click(function() ...
- 使用C3的一些新属性绘制谷歌浏览器的图标
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- windows7 端口查看以及杀死进程释放端口
1.调出命令窗口:开始---->运行---->cmd,或者是window+R组合键 2.输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是4300 ...
- skype for business server 2015 报错“不可用:试图检查架构状态时发生故障,请确保能够访问Active Direcotry”
报错“不可用:试图检查架构状态时发生故障,请确保能够访问Active Direcotry” 遇到错误后就上网查询了下,有的人说用下面方法解决了 用域的administrator 登入就可以了(之前是用 ...
- linux(centos、ubuntu)网卡配置文件不生效
今天遇到个问题,服务器上ifcfg配置了eth0文件,但是通过ifcfg命令检查发现网卡IP配置并未生效. 然后通过如下配置修正: ubuntu: # vim /etc/default/grub在”G ...
- MyEcplise使用---使用 MyEclipse 反转引擎生成数据库
使用 MyEclipse 反转引擎,生成数据库 步骤: 1. 新建 Database 连接 2. 新建web项目 temp 添加myeclipse hibernate 能力 3. Hibernate ...
- Hyperledger Fabric 1.0 学习搭建 (二)--- 源码及镜像文件处理
2.1下载Fabric源码下载Fabric源码是因为要用到源码中提到的列子和工具, 工具编译需要用到go语言环境, 因此需要把源码目录放到$GOPATH下. 通过1.3中go的安装配置, $GOPAT ...
- Future Research Directions in Social Recommendation
From the tutorial published by Martin Ester in RecSys 2013 Future Research Directions --Recommendati ...
- SAP CRM One order里user status和system status的mapping逻辑
Below example show: How the mapping relationship between User status and System status maintained in ...