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. Sublime Text 插件之:MarkDown

    Sublime Text 插件之:MarkDown 喜欢写文档的同学应该离不开 MarkDown ,ST(Sublime Text)的插件 Markdown Preview 就支持实时在浏览器中预览p ...

  2. cookie,localstorge,sessionstorge三者总结

    相同点:都是客户端存储东西的: 不同: 1大小,cookie最小;locastorge最大 2 cookie设置好会在header头里面自动带的:但是ls和ss不会:ls同个浏览下不同网页(非跨域)都 ...

  3. Thunder团队Beta周贡献分分配结果

    小组名称:Thunder 项目名称:爱阅app 组长:王航 成员:李传康.翟宇豪.邹双黛.苗威.宋雨.胡佑蓉.杨梓瑞 分配规则 规则1:基础分,拿出总分的20%(8分)进行均分,剩下的80%(32分) ...

  4. 【IdentityServer4文档】- 整体情况

    整体概况 大多数现代应用程序看起来或多或少像这样: 最常见的交互是: 浏览器与 Web 应用程序进行通信 Web 应用程序与 Web API 进行通信(有时是Web应用程序自己发起,有时代表用户发起) ...

  5. 刷ROM必備的clockworkmod recovery

    Desire HD 手機早早就 Root,前陣子也S-OFF 變成工程版的 HBOOT(ENG S-OFF),想要刷機的朋友一定常常聽人提起 clockworkmod recovery ,接下來就是安 ...

  6. lintcode-158-两个字符串是变位词

    158-两个字符串是变位词 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 说明 What is Anagram? Two strings are ...

  7. iOS- 利用UIImageView自己整了个不会说话的汤姆猫

    1.实现思路 先说说我实现它的主要思路,很简单,主要利用UIImageView连续动画播放,和按钮的点击事件,就可以完成了这么一个简单的不会说话的汤姆猫. 2.实现细节 2.1.加载本地字典里保存的本 ...

  8. ResultSet 可滚动性和可更新性

    JDBC 2.0 API 为结果集增加了两个新的基本能力:可滚动性和可更新性,我想肯定满足了你的要求.在滚动结果集中可用的方法有: rs.previous();//向前滚动 rs.next();//向 ...

  9. java 基础--数组--004

    1,数组定义格式 String[] aa; String aa[];2,二位数组的定义格式1 String aa[][] = new String[m][n]; String[] aa[] = new ...

  10. C#中的is和as操作符

    在C#语言中进行类型转换的操作符is和as.is和as都是强制类型转换,但这两者有什么相同之处和不同之处呢?在使用is和as需要注意哪些事项?下面我们从简单的代码示例去探讨这个简单的问题.注:此博文只 ...