题目链接:https://hihocoder.com/problemset/problem/1343

#1343 : Stable Members

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Recently Little Hi joined an algorithm learning group. The group consists of one algorithm master and N members. The members are numbered from 1 to N. Each member has one or more other members as his mentors. Some members' mentor is the master himself.

Every week each member sends a report of his own learning progress and the reports collected from his pupils (if there is any) to his mentors. The group is so well designed that there is no loop in the reporting chain so no one receives his own report from his pupil. And finally the master gets every one's report (maybe more than once).

Little Hi notices that for some members their reporting routes to the master can be easily cut off by a single member's (other than the master and himself) absence from the reporting duty. They are called unstable members while the others are stable members. Given the reporting network of the group, can you find out how many members are stable?

Assume there are 4 members in the group. Member 1 and 2 both have the master as their only mentor. Member 3 has 2 mentors: member 1 and member 2. Member 4 has 1 mentor: member 3. Then member 4 is the only unstable member in the group because if member 3 is absent his learning report will be unable to be sent to the master.

输入

The first line contains an integer N, the number of members.

The i-th line of the following N lines describe the mentors of the i-th member. The first integer is Ki, the number of mentors of the i-th member. Then follows Ki integers A1 ... AN, which are his mentors' numbers. Number 0 indicates that the master is one of his mentor.

For 40% of the data, 1 ≤ N ≤ 1000.

For 100% of the data, 1 ≤ N ≤ 100000.

For 100% of the data, 1 ≤ Ki ≤ 10, Ki < N, 0 ≤ AiN.

输出

Output the number of stable members.

样例输入
5
1 0
1 0
2 1 2
1 3
2 4 3
样例输出
3
题意:给一个有向无环图,定义一个点为unstable当且仅当删掉一个点(不能为它自己或点0)时,它不能与点0连通;其他点则为stable,求图中有几个stable点。
样例解释:
如左图所示,删掉点3,则4和5都无法连通到点0即master,所以4和5都是unstable点,1、2和3都是stable,所以最后答案为3个stable点。
题解:依次对点v进行拓扑排序,遍历其后续点,如果后续点的所有父节点都染色为v,则也染色为v,并入队列,标记为unstable点。
 #include<bits/stdc++.h>
using namespace std;
const int N = ;
struct node {
int color = ;
vector<int>s, p;//子节点、父节点
}a[N];
bool unstable[N];
bool all_colored(int v, int color) {
int num = a[v].p.size();
bool flag = true;
for(int i = ; flag && i < num; ++i)
flag &= (a[a[v].p[i]].color == color);
return flag;
}
void topo(int v) {
if(unstable[v]) return;
queue<int>q;
q.push(v);
a[v].color = v;
while(!q.empty()) {
int u = q.front(); q.pop();
int num = a[u].s.size();
for(int i = ; i < num; ++i) {
int son = a[u].s[i];
if(all_colored(son, v)) {
a[son].color = v;
unstable[son] = true;
q.push(son);
}
}
}
}
int main() {
int n, k, i, v, ans = ;
scanf("%d", &n);
for(i = ; i <= n; ++i) {
scanf("%d", &k);
while(k--) {
scanf("%d", &v);
a[i].p.push_back(v);
a[v].s.push_back(i);
}
}
for(i = ; i <= n; ++i) topo(i);
for(i = ; i <= n; ++i) ans += unstable[i];
printf("%d\n", n - ans);
return ;
}
还有其他解法,参考:http://www.cnblogs.com/demian/p/6536799.html

hihoCoder1343 : Stable Members【BFS拓扑排序】的更多相关文章

  1. hihocoder 1343 : Stable Members【拓扑排序】

    hihocoder #1343:题目 解释:一个学习小组,一共有N个学员,一个主管.每个学员都有自己的导师(一个或者多个),导师可以是其他学员也可以是主管.每周学员都要把自己的学习报告和收到的报告提交 ...

  2. C. Journey bfs 拓扑排序+dp

    C. Journey 补今天早训 这个是一个dp,开始我以为是一个图论,然后就写了一个dij和网络流,然后mle了,不过我觉得如果空间开的足够的,应该也是可以过的. 然后看了题解说是一个dp,这个dp ...

  3. uvaLA4255 Guess BFS+拓扑排序

    算法指南白书 思路:“连续和转化成前缀和之差” #include <stdio.h> #include <string.h> #include <iostream> ...

  4. CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]

    题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条 ...

  5. hihocoder 1174 [BFS /拓扑排序判断是否有环]

    hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...

  6. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  7. 【ZOJ - 3780】 Paint the Grid Again (拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  8. BFS (1)算法模板 看是否需要分层 (2)拓扑排序——检测编译时的循环依赖 制定有依赖关系的任务的执行顺序 djkstra无非是将bfs模板中的deque修改为heapq

    BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not ro ...

  9. hdu1532 用BFS求拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目给出一些点对之间的先后顺序,要求给出一个字典序最小的拓扑排列.对于拓扑排序的问题,我们有DF ...

随机推荐

  1. MVC 之var与dynamic

    如果你用MVC写过程序,那么你应该知道ViewBag这个用于前后台的数据传递工具,那么你是否对ViewBag的用法感到过疑惑呢? ViewBag.Mode1l=new object(); ViewBa ...

  2. [Redis] redis数据备份恢复与持久化

    数据库备份,使用save命令,将会在redis的安装目录中生成dump.rdb 例如:在我的目录下 redis/src/dump.rdb 使用命令config get dir,获取当前redis的安装 ...

  3. 请比较throw 合throws的区别

    throw语句用在方法体内,表示抛出异常.throws语句用在方法声明的后面,表示再抛出异常,由该方法的调用者来处理.throws主要声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常. ...

  4. Mysql explain分析sql语句执行效率

    mysql优化–explain分析sql语句执行效率 Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 ...

  5. Maven 项目管理从未如此通畅

    一,写在前面 Maven到底是什么?它能做些什么?能为我们的开发工作提供什么样的帮助?为什么会有如此大的知名度?另外,常听大厂的人说“私服”,工具管理吧啦吧啦的一堆也是不明觉厉.相信仁者见仁,着智者见 ...

  6. 小tip: base64:URL背景图片与web页面性能优化——张鑫旭

    一.base64百科 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息. 某人: 唉,我彻底废柴了,为何上面明明是中文,洒家却看不懂嘞,为什 ...

  7. HTML5 MutationObserver检测页面劫持

    好久没写博客了,业务一直在变化,陆陆续续的做了很多web app,被业务流淹没就很少有机会去反思,前端技术发展如此之快,常常有种不学则退的恐慌,一种技术还没吃透就涌出新的技术,然后一波人又打着各种旗帜 ...

  8. vue-router 实现导航守卫(路由卫士)

    路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...

  9. opencv3.2.0图像对比度与亮度调整

    ##名称:图像对象度与对比度调整(由轨迹条分别控制对比度和亮度值) ##平台:QT5.7.1+opencv3.2.0 ##时间:2017年12月13日 /***********建立QT控制台程序*** ...

  10. vmware参数详解

    config.ini - 设置 VMX文件参数 文件configs.ini或config(在Linux中)为所有用户设置参数,或者如果您喜欢 - 为主机设置参数. 如果要为所创建的所有虚拟机使用某些设 ...