【CodeForces 129 B】Students and Shoelaces(拓扑排序)

时间:2022-07-01 05:53:51

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 (【CodeForces 129 B】Students and Shoelaces(拓扑排序)). 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

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.

题意:

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(拓扑排序)的更多相关文章

  1. Codeforces Beta Round &num;29 &lpar;Div&period; 2&comma; Codeforces format&rpar; C&period; Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  2. CodeForces 1213F &lpar;强联通分量分解&plus;拓扑排序&rpar;

    传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...

  3. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  4. Educational Codeforces Round 25 E&period; Minimal Labels 拓扑排序&plus;逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces Round &num;292 &lpar;Div&period; 1&rpar; B&period; Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  6. Codeforces &num;541 &lpar;Div2&rpar; - D&period; Gourmet choice(拓扑排序&plus;并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  7. Codeforces Round &num;541 &lpar;Div&period; 2&rpar; D(并查集&plus;拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 &gt ...

  8. Codeforces Round &num;541 &lpar;Div&period; 2&rpar; D 并查集 &plus; 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  9. Codeforces Round &num;397 by Kaspersky Lab and Barcelona Bootcamp &lpar;Div&period; 1 &plus; Div&period; 2 combined&rpar; E&period; Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

随机推荐

  1. Java发送邮件代码

    MailSenderInfo.java package com.nihaorz.mail.util; import java.util.Properties; public class MailSen ...

  2. EF Attach时已存在的处理方式

    如果我们在先前的步骤中读取过数据,如 var list = db.Model.ToList(); 之后再,附加 var o = new Model { Id = 1 }; db.Model.Attac ...

  3. androidSDK也要配置环境变量(转)

    android的开发人员来说,首先要做的就是环境变量的配置.java是需要配置环境变量的.当然,安卓的环境变量需要我们配置adb的使用,将开发平台的两个工具包配置到环境变量里. 工具/原料 andro ...

  4. CSS自定义select下拉选择框&lpar;不用其他标签模拟&rpar;

    今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...

  5. 矩形嵌套 南阳理工ACM

    描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).例如(1, ...

  6. 正确看待HTML5的语法变化

    也许会有人问:“HTML4已经很普及了,如果改变基础语法,会不会有什么影响?” 我们都知道,在HMTL5之前几乎没有符合标准规范的Webu浏览器!在这种情况下,各个浏览器之间的互相兼容性和互操作性在很 ...

  7. frame&period;origin&period;x 的意思和作用&quest;

    frame.origin.x 的意思和作用? scrollView.frame 一个view的frame 包含它的矩形形状(size)的长和宽. 和它在父视图中的坐标原点(origin)x和y坐标 f ...

  8. 长安大学ACM竞赛部

    本博客为长安大学ACM竞赛部的公共博客,记录长大ACMer的成长点滴. 开此博客,诸君共勉.

  9. numpy array分割-【老鱼学numpy】

    有合并,就有分割. 本节主要讲述如何通过numpy对数组进行横向/纵向分割. 横向/纵向分割数组 首先创建一个6行4列的数组,然后我们对此数组按照横向进行切割,分成3块,这样每块应该有2行,见例子: ...

  10. Ajax原理与封装详解

    Ajax大家每天都在用,jquery库对Ajax的封装也很完善.很好用,下面我们看一下他的内部原理,并手动封装一个自己的Ajax库. 更多有关ajax封装及数据处理,请参看上海尚学堂<Ajax中 ...