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 ...
随机推荐
- 【JAVAEE学习笔记】hibernate01:简介、搭建、配置文件详解、API详解和CRM练习:保存客户
今日学习:hibernate是什么 一.hibernate是什么 框架是什么: 1.框架是用来提高开发效率的 2.封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架 ...
- 每天一个JS 小demo之“随机”抽奖。主要知识点:Math函数,数组方法,递归
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- (转)微信禁用右上角的分享按钮,WeixinJSBridge API以及隐藏分享的子按钮等菜单项
<!--禁用微信分享按钮--> <script> function onBridgeReady() { WeixinJSBridge.call('hideOptionMenu' ...
- servlet与Javabean之间的区别
在JSP中调用JAVA类和使用JavaBean有什么区别? 可以像使用一般的类一样使用JavaBean,Bean只是一种特殊的类.特殊在可以通过<jsp:useBean/>调用JavaBe ...
- 允许mysql用户从远程登录
1.修改/etc/mysql/my.cnf,将下面的行注释掉bind=127.0.0.1注释#bind=127.0.0.1 2.修改用户权限,允许从任何主机登录mysql>use mysql;m ...
- WPF MVVM 架构 Step By Step(4)(添加bindings - 完全去掉后台代码)
之前的改进已经挺棒的,但是我们现在知道了后台代码的问题,那是否可能把后台代码全部去除呢?这时候就该WPF binding 和 commands 来做的事情了. WPF就是以超吊的binding,com ...
- 石头剪刀布 R语言统计分析
关于石头剪刀布,做出了详细的分析,具体ppt见地址 http://files.cnblogs.com/files/GMGHZ971322/R%E8%AF%AD%E8%A8%80.pptx 16级电商三 ...
- IT学习网站
网站 网站定位 http://www.51cto.com/ 中国领先的IT技术网站. http://www.iteye.com/ 内容齐全,功能丰富的中文IT技术门户和社区网站. http://www ...
- 从String类型字符串的比较到StringBuffer和StringBuilder
1. String类型 String类源码 为了从本质上理解String类型的特性所在,我们从String类型的源码看起,在源码中String类的注释中存在以下: /**Strings are con ...