1103: Party

Time Limit: 2 Sec  Memory Limit: 64 MB
Submit: 11  Solved: 7

Description

N students were invited to attend a party, every student has some friends, only if someone’s all friends attend this party, this one can attend the party(ofcourse if he/she has no friends, he/she also can attend it.), now i
give the friendship between these students, you need to tell me whether all of them can attend the party.
Note that the friendship is not mature, for instance, if a is b’s friend, but b is not necessary a’s friend. 

Input

Input starts with an integer T(1 <= T <= 10), denoting the number of test case.
For each test case, first line contains an integer N(1 <= N <= 100000), denoting the number of students.
Next n lines, each lines first contains an integer K, denoting the number of friends belong to student i(indexed from 1). Then following K integers, denoting the K friends.
You can assume that the number of friendship is no more than 100000, and the relation like student A is himself’s friend will not be existed. 

Output

For each test case, if all of the students can attend the party, print Yes, otherwise print No. 

Sample Input

2
3
1 2
1 3
1 1
3
1 2
0
1 1

Sample Output

No
Yes

HINT

For the first case:

Student 1 can attend party only if student 2 attend it.

Student 2 can attend party only if student 3 attend it.
Student 3 can attend party only if student 1 attend it.
So no one can attend the party. 

今天找了篇博客认真看了下拓扑排序,想自己重新写下这道题。一次就过了运气不错。(很多东西重新回过头来看会有更深入的理解和发现)仔细思考一下确实如文章所说:

不难看出该算法的实现十分直观,关键在于需要维护一个入度为0的顶点的集合:

每次从该集合中取出(没有特殊的取出规则,随机取出也行,使用队列/栈也行,下同)一个顶点,将该顶点放入保存结果的List中。

紧接着循环遍历由该顶点引出的所有边,从图中移除这条边,同时获取该边的另外一个顶点,如果该顶点的入度在减去本条边之后为0,那么也将这个顶点放到入度为0的集合中。然后继续从集合中取出一个顶点…………

当集合为空之后,检查图中是否还存在任何边,如果存在的话,说明图中至少存在一条环路。不存在的话则返回结果List,此List中的顺序就是对图进行拓扑排序的结果。

首先要知道一点入度和出度的概念,比如我在学数学之前要学语文,此时语文和数学之间有一个箭头,那按正常思维肯定是语文比数学优先吧,因为你肯定要先学语文才能学数学啊,那么就在语文和数学之间连一条有向边,就像这样语文——>数学,可以当成你玩游戏时的进度,过了语文这个任务才能过数学。嗯这样应该非常好理解了吧。

因此需要用到三个东西使得程序易于实现和理解

1、容器数组:是记录每一个顶点a所指向的顶点bi的集合;

比如1——>2那么我们就在vector[1]中压入2这个点。

2、入度数组:记录每一个顶点的入度。显然例子中rudu[数学]=1;

3、维护每次找到的入度为0的队列:拓扑排序可以看成一种模拟——首先找到入度为0的即没人可以制约它的点,然后将这个点和它指出去的边删去,此时它连到的点入度肯定会因此减1(平凡图情况下)。那么很可能此时又会产生一个/多个入度为0的点。然后又可以重复前面步骤了。直到找不到入度为0的点或者所有边已经访问过。

那拓扑归拓扑,排序是什么鬼?就是每次出现的入度变为0的点按照出现的先后顺序构成的一个序列。用容器或者数组加一个尾指针来保存就可以。当然此题不需要输出排序结果。

代码(注释内容可以输出排序结果):

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
int const N=100010;
int rudu[N];
vector<int>V[N];
inline void init(int n)
{
for (int i=0; i<=n; i++)
{
V[i].clear();
}
MM(rudu);
}
int main(void)
{
int tcase,i,j,a,n,m,sum;
scanf("%d",&tcase);
while (tcase--)
{
queue<int>Q;
scanf("%d",&n);
sum=0;init(n);
for (i=1; i<=n; i++)
{
scanf("%d",&m);
if(m==0)
{
Q.push(i);
continue;
}
for (j=1; j<=m; j++)
{
scanf("%d",&a);
V[a].push_back(i);
}
rudu[i]+=m;
sum+=m;
}
//vector<int>ans;
while (!Q.empty())
{
int now=Q.front();
Q.pop();
int sz=V[now].size();
for (i=0; i<sz; i++)
{
int v=V[now][i];
rudu[v]--;
sum--;
if(rudu[v]==0)
{
Q.push(v);
//ans.push_back(v);
}
}
}
if(sum)
puts("No");
else
{
puts("Yes");
/*for (vector<int>::iterator it=ans.begin(); it!=ans.end(); it++)
{
printf("%d ",*it);
}
putchar('\n');*/
}
}
return 0;
}

HUST——1103Party(拓扑排序+个人见解)的更多相关文章

  1. HUST 1103 校赛 邻接表-拓扑排序

    Description N students were invited to attend a party, every student has some friends, only if someo ...

  2. UVA10305 拓扑排序

    网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...

  3. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  4. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  7. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  9. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

随机推荐

  1. eclipse中代码注释及其他常用快捷键

    html代码注释/取消注释             Ctrl + Shift + c php代码注释/取消注释                 Ctrl + / (4)Ctrl+Shift+/ 说明: ...

  2. NBear简介与使用图解

    NBear简介与使用图解 框架类型:ORM映射框架 简介:NBear是一个基于.Net 2.0.C#2.0开放全部源代码的的软件开发框架类库.NBear的设计目标是尽最大努力减少开发人员的工作量,最大 ...

  3. 用Windows Native API枚举所有句柄及查找文件句柄对应文件名的方法

    枚举所有句柄的方法 由于windows并没有给出枚举所有句柄所用到的API,和进程所拥有的句柄相关的只有GetProcessHandleCount这个函数,然而这个函数只能获取到和进程相关的句柄数,不 ...

  4. AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?

    问题 An error occurred while starting the application. ArgumentException: AddDbContext was called with ...

  5. Linux 、AIX环境下查看oracle配置信息(service_name、SID、tnsname)。

    SID: echo $ORACLE_SID service_name: sqlplus / as sysdba; show parameter instance_name; show paramete ...

  6. iOS小技巧–用runtime 解决UIButton 重复点击问题

    什么是这个问题 我们的按钮是点击一次响应一次, 即使频繁的点击也不会出问题, 可是某些场景下还偏偏就是会出问题. 通常是如何解决 我们通常会在按钮点击的时候设置这个按钮不可点击. 等待0.xS的延时后 ...

  7. 手机web网页的设计

      Viewport(视口) 1.视口概念 描述:视口,就是视图窗口的简称,页面中视口大小实际上就是html元素的显示大小 说明:页面想要在移动端加载必须进行视口适配 如果不对页面进行调整,默认页面在 ...

  8. Java AES加密解密工具 -- GUI 、在线传输文件

    原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下:     (1)将128位AES ...

  9. 08GNU as汇编

    1. 概述 ​ 由于操作系统许多关键代码要求有很高的执行速度和效率,因此在一个操作系统源代码中通常就会包含大约 10% 左右的起关键作用的汇编语言程序量.Linux 操作系统也不例外,它的 32 位初 ...

  10. paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(index-parameter style with registered outputs)

    case语句中,对于state/next 矢量仅仅做了1-bit比较. parameter 值不是表示FSM的状态编码,而是表示state/next变量的索引.