Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19234   Accepted: 5182

Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases.

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

Output

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

1
3 3
1 2
2 3
3 1

Sample Output

Yes

题意:给你一个有向图 判断任意两点是否能够到达

先缩点 然后判断是否是一棵单直链  拓扑排序就可以 每次判断队列中的点是否只有一个就可以

就是每次入度为0的点只有一个

#include<iostream>//HiHo 1515
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=10000+10;
struct node{
int to,next;
}edge[N<<1];
int tot;
int head[N];
int belong[N];
int dfn[N],low[N];
int cnt;
int vis[N];
int num;
vector<int>vc[N];
void init(){
memset(head,-1,sizeof(head));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(vis,0,sizeof(vis));
for(int i=1;i<N;i++)vc[i].clear();
tot=0;
num=0;
cnt=0;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
stack<int>st;
void tarjan(int u){
low[u]=dfn[u]=++num;
vis[u]=1;
st.push(u);
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dfn[v]==0){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int vv;
cnt++;
do{
vv=st.top();
st.pop();
belong[vv]=cnt;
vis[vv]=0;
}while(vv!=u);
}
}
queue<int>q;
int in[N];
void tuposort(){
while(q.empty()==0){
if(q.size()!=1){
cout<<"No"<<endl;
return;
}
int u=q.front();
q.pop();
for(int i=0;i<vc[u].size();i++){
int v=vc[u][i];
in[v]--;
if(in[v]==0){
q.push(v);
}
}
}
cout<<"Yes"<<endl;
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
init();
while(q.empty()==0)q.pop();
memset(in,0,sizeof(in));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=1;i<=n;i++){
if(dfn[i]==0)tarjan(i);
}
//for(int i=1;i<=n;i++)cout<<belong[i]<<" ";cout<<endl;
for(int i=1;i<=n;i++){
for(int j=head[i];j!=-1;j=edge[j].next){
int uu=belong[i];
int vv=belong[edge[j].to];
if(uu!=vv){
vc[uu].push_back(vv);
// cout<<uu<<" "<<vv<<endl;
in[vv]++;
}
}
}
for(int i=1;i<=cnt;i++){
if(in[i]==0){
q.push(i);
}
}
tuposort();
}
}

  

poj 2762(tarjan缩点+判断是否是单链)的更多相关文章

  1. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  2. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  3. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  4. POJ 2672 Tarjan + 缩点 + 拓扑思想

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383 ...

  5. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...

  6. POJ 2186 tarjan+缩点 基础题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37111   Accepted: 15124 De ...

  7. poj 2186(tarjan+缩点)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37083   Accepted: 15104 De ...

  8. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

  9. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

随机推荐

  1. VMWare NAT网络配置

    1. 打开CMD,输入以下命令 ipconfig -all 2. 设置主机Wi-Fi网络对VMnet8虚拟网卡的网络共享 3. 虚拟机NAT模式配置 4. 配置虚拟机网卡信息 切换到ROOT cd / ...

  2. python3.x Day6 多进程

    多进程:1.每个子进程申请到的资源都是独立的,不与其他进程共享.2.语法上和线程基本上差不多,使用multiprocessing.Process(target=xxxx,args=(xxx,xxx,x ...

  3. AutoEncoders原理

    目录 Auto-Encoders How to Train? Auto-Encoders How to Train?

  4. day21 05 员工信息表

    day21 05 员工信息表 假设有一个员工信息表,里面有每个员工的名字,id,年龄,电话,还有他们所作的工作,而有时候我们并不需要所有的信息,而想根据某些条件,寻找符合条件即可,即筛选, 比如想要筛 ...

  5. 10 行 Python 代码,批量压缩图片 500 张,简直太强大了

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/5hpFDgjCpfb0O1Jg-ycACw 熟悉 "Pyth ...

  6. 全文索引(A-1)-用户数据收集(用户研究)

    推荐系统根据用户的信息和历史行为记录,构造出用户的个性化模型,再依据特定的推荐算法,向用户推荐其可能感兴趣的项目. 如何获取用户的偏好? 建议用户对一些指定项目进行评分,如对:小说.传记.技术书.图画 ...

  7. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

  8. [转]如何阅读android framework源码

    但如果想深入的了解Android系统, 那么可以看下我的一些简单的总结. 知识 Java Java是AOSP的主要语言之一. 没得说, 必需熟练掌握. 熟练的Android App开发 Linux A ...

  9. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...

  10. vagrant的学习 之 ThinkPHP5.1

    vagrant的学习 之 ThinkPHP5.1 本文根据慕课网的视频教程练习,感谢慕课网! 慕课视频学习地址:https://www.imooc.com/video/14218. 慕课的参考文档地址 ...