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. 
 
题意:给定n个人, 第i个人依赖k个人,只有k个人都参加了,那么i才会参加。问是否所有人都能参加。
思路:数据量有些大,可能无法用并查集判是否有回路解。学长给出方法是用拓扑排序,最后将节点数量和n比较判断即可。
拓扑排序大致是查找出度为零的所有节点,压入队列,一个个弹出,同时将该点和临边删除。
用到了Vector邻接表,发现储存图真是好用。
但是我的代码还有点问题,vector并不需要开二维的,一维就够了。
 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#define MAXX 100010
using namespace std;
const int INF = 0x3f3f3f3f;
pair<vector<int>, int> x;
vector< vector<int> >bel();
queue< vector<int> >qu;
int a[MAXX];
int cnt; int findpush(int n)
{
int flag = ;
for(int i = ; i <= n; i++)
{
if(a[i] == )
{
flag = ;
if(bel[i].size())
qu.push(bel[i]);
else
cnt--;
a[i] = INF;
}
}
return flag;
} int main()
{
int T, n, t, ed;
scanf("%d", &T);
while(T--)
{
scanf("%d",&n);
cnt = n; for(int i = ; i <= n; i++)
a[i] = , bel[i].clear();
for(int i = ; i <= n; i++)
{
scanf("%d", &t);
/* if(!t)
cnt--;*/
while(t--)
{
scanf("%d", &ed);
bel[ed].push_back(i);
a[i]++;
}
}
while(qu.empty())
{
int flag = findpush(n);
if(flag == )
{
if(cnt)
printf("No\n");
else printf("Yes\n");
break;
}
int temp = qu.size();
while(temp--)
{ for(int i = ; i < (qu.front()).size(); i++)
{
a[(qu.front())[i] ]--;
}
qu.pop();
cnt--;
}
}
}
}

HUST 1103 校赛 邻接表-拓扑排序的更多相关文章

  1. sdut 2819 比赛排名(边表 拓扑排序)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2819 #include <iost ...

  2. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  3. NOJ/HUST 1095 校赛 Just Go 线段树模板题

    Description There is a river, which contains n stones from left to right. These stones are magic, ea ...

  4. hdu1285 确定比赛名次(拓扑排序多种方法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  5. ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)

    做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...

  6. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  7. 【数据结构】【图文】【oj习题】 图的拓扑排序(邻接表)

    拓扑排序: 按照有向图给出的次序关系,将图中顶点排成一个线性序列,对于有向图中没有限定次序关系的顶点,则可以人为加上任意的次序关系,由此所得顶点的线性序列称之为拓扑有序序列.显然对于有回路的有向图得不 ...

  8. HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 解题报告:有n台机器用来完成m个任务,每个任务有一个难度值和一个需要完成的时间,每台机器有一个可 ...

  9. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. HADOOP (十一).安装hbase

    下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...

  2. php常用方法汇总

    xml格式转成array <?php $str='<xml><node><![CDATA[content]]></node></xml> ...

  3. 适合初学者的嵌入式Linux计划

    俗话说万事开头难,刚开始的时候,你是否根本就不知如何开始,上网查资料被一堆堆新名词搞的找不到北,去图书馆看书也是找不到方向?又是arm,又是linux,又是uboot头都大了,不知道自己究竟从哪里开始 ...

  4. 事后分析报告(M2阶段)

    我们的项目是自选项目,一款名为备忘录锁屏MemoryDebris的软件. 在第二轮的迭代中,由于各科的大作业都集中在这一段时间,所以这段时间各个组员间的负担都比较大,但是在大家共同努力,最终我们还是交 ...

  5. 项目选题报告(I konw)

    一.团队成员及分工 团队名称:I know 团队成员: 陈家权:选题报告word撰写 赖晓连:ppt制作,原型设计 雷晶:ppt制作,原型设计 林巧娜:原型设计,博客随笔撰写 庄加鑫:选题报告word ...

  6. win7 64位在线编辑dsoframer控件的安装和使用配置

    经历了两天的折磨,查阅了网上的资料,按网上的操作试了n种方法结果还是不行,开始以为是dsoframer 是32位控件问题,结果不是(经历了更改解决方案cpu,发布基于x86的网站:以为是操作系统问题, ...

  7. Qt窗口及控件-QTreeview/QTableView排序问题

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt-QTreeview/QTableView排序问题     本文地址:http://tec ...

  8. Linux文件传输FTP详解

    ftp命令用来设置文件系统相关功能.ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里详细介绍Linux ftp命令的一些经常使用的命令,相 ...

  9. Codeforces 1025D(区间dp)

    容易想到设f[i][j][k]为i~j区间以k为根是否能构成bst.这样是O(n4)的.考虑将状态改为f[i][j][0/1]表示i~j区间以i-1/j+1为根能否构成bst.显然如果是i-1作为根的 ...

  10. nginx日志切割总结

    Nginx日志切割   方法1(脚本+定时执行): #step1:加脚本 cut_nginx_log.sh,主进程把USR1信号发给worker,worker接到这个信号后,会重新打开日志文件 #!/ ...