codeforces 129B students and shoes
https://vjudge.net/problem/CodeForces-129B
题意:
有n个学生,他们之间被鞋带缠住了。现在,老师首先把所有只与一个学生直接相连的学生找出来,让他们聚集到一起,然后把他们踢出去,直到无人可踢为止。问可以踢多少次。
思路:
用拓扑排序的思路,从所有点的度数下手。将所有度数为1的点入队,之后把他们连接的点的度数全部减一,自己也减1,之后再把所有的度数为1的点入队,循环,直到队列为空。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std; int d[]; queue<int> q;
vector<int> v[]; int main()
{
int n,m; scanf("%d%d",&n,&m); for (int i = ;i < m;i++)
{
int a,b; scanf("%d%d",&a,&b); d[a]++;d[b]++; v[a].push_back(b);
v[b].push_back(a);
} for (int i = ;i <= n;i++)
{
if (d[i] == ) q.push(i);
} int ans = ; while (!q.empty())
{
int cnt = ; int b[]; while (!q.empty())
{
b[cnt++] = q.front();q.pop();
} if (cnt > ) ans++; for (int i = ;i < cnt;i++)
{
int t = b[i];
d[t]--; for (int j = ;j < v[t].size();j++)
{
int s = v[t][j]; if (d[s] >= )
{
d[s]--;
}
}
} for (int i = ;i <= n;i++)
{
if (d[i] == ) q.push(i);
}
} printf("%d\n",ans); return ;
}
codeforces 129B students and shoes的更多相关文章
- Codeforces 最大流 费用流
这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...
- codeforces A. Group of Students 解题报告
题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...
- 图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces
题目传送门 /* 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) */ #include <cstdio> #include <cs ...
- Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]
PROBLEM A. Shockers 题 http://codeforces.com/contest/906/problem/A 906A 907C 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...
- 【Codeforces 332C】Students' Revenge
Codeforces 332 C 我爱对拍,对拍使我快乐... 题意:有\(n\)个议题,学生们会让议会同意\(p\)个,其中主席会执行\(k\)个, 每一个议题执行后主席会掉\(a_i\)的头发,不 ...
- 【CodeForces 129 B】Students and Shoelaces(拓扑排序)
Anna and Maria are in charge of the math club for junior students. When the club gathers together, t ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题
A. Two Rival Students There are
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students
You are the gym teacher in the school. There are nn students in the row. And there are two rivalling ...
- [模拟]Codeforces Circle of Students
Circle of Students time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- python 2.7中urllib 2 与python 3.5中 urllib的区别。
python 3.x中urllib库和urilib2库合并成了urllib库. 其中urllib2.urlopen()变成了urllib.request.urlopen() urllib2.Reque ...
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 第45篇 js操作打开本地程序
原文地址:http://blog.laofu.online/2017/06/10/how-js-controlApp/ 背景 假设有这样一个产品,一个web和一个winform客户端,在客户在web的 ...
- Dojo初探之1:AMD规范,编写符合AMD规范(异步模块加载机制)的模块化JS(其中dojo采用1.11.2版本)
一.AMD规范探索 1.AMD规范(即异步模块加载机制) 我们在接触js的时候,一般都是通过各种function来定义一些方法,让它们帮我们做一些事情,一个js可以包含很多个js,而这些functio ...
- 2015阿里巴巴安全峰会PPT
有幸参加了阿里巴巴安全峰会,不得不佩服阿里巴巴神盾局,真牛B!然后亲眼目睹了第二天的各大厂商牛(zhuang)B人才上台演讲,有被捧的,有被喷的,呵呵.总的来说,大家的分享精神还是阔以的. 下面是会议 ...
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建
http://blog.csdn.net/u013142781/article/details/50380920
- AngularJS高级程序设计读书笔记 -- 服务篇
服务是提供在整个应用程序中所使用的任何功能的单例对象. 单例 : 只用一个对象实例会被 AngularJS 创建出来, 并被程序需要服务的各个不同部分所共享. 1. 内置服务 一些关键方法也被 Ang ...
- AWS Organizations
AWS Organizations offers policy-based management for multiple AWS accounts and is now generally avai ...
- [HNOI2009]梦幻布丁 算法技巧之邻接链
题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输出格式 输入格式: 第 ...
- javascript对象转换,动态属性取值
$(document).ready(function(){ var exceptionMsg = '${exception.message }'; var exceptionstr = ''; //j ...