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的更多相关文章

  1. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...

  2. codeforces A. Group of Students 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...

  3. 图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

    题目传送门 /* 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) */ #include <cstdio> #include <cs ...

  4. 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 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...

  5. 【Codeforces 332C】Students' Revenge

    Codeforces 332 C 我爱对拍,对拍使我快乐... 题意:有\(n\)个议题,学生们会让议会同意\(p\)个,其中主席会执行\(k\)个, 每一个议题执行后主席会掉\(a_i\)的头发,不 ...

  6. 【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 ...

  7. Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题

    A. Two Rival Students There are

  8. 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 ...

  9. [模拟]Codeforces Circle of Students

    Circle of Students time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. MarkDown编辑器快捷方式

    常用快捷: Ctrl+K 插入代码块 Ctrl+G 插入图片 Ctrl+B 文字加粗 Ctrl+I 文字倾斜 Ctrl+G 插入图片 Tab 默认新建高亮代码块 ">" 向右 ...

  2. 用java来实现验证码功能。

    昨天在网上看到了一篇关于验证码的文章,很不错,但是有些不尽人意的地方,比如没有考虑到前端传过来的验证码如果是小写的话,那么做验证的时候就会出现错误, 因为java是严格区分大小写的,还有就是验证码会重 ...

  3. 转发:Ubuntu软件卸载安装的命令

    说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...

  4. EF Core学习Code First

    下面通过实例来学习EF Core Code First,也就是通过EF Core迁移来完成从模型生成数据库. 本实例使用EntityFrameworkCore SQLite 数据库进行介绍,大家也可以 ...

  5. 360安全检测出的WordPress漏洞的修复方法

    1.跨站脚本攻击(XSS) 这个漏洞注意是因为用户评论可以提交代码,有安全风险.虽然你的WordPress以及是最新版,但是你的WordPress主题却不一定跟着更新!因此,需要稍微修改一下评论相关的 ...

  6. JAVA 23种开发模式详解(代码举例)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  7. jquery移出select指定option

    $("#selectLine option[value!='']").remove();

  8. php隔行换色输出表格

    <?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...

  9. 【LeetCode】49. Group Anagrams

    题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...

  10. 【Android Developers Training】 81. 解析XML数据

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...