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

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

Source

题意: 给你一个有向图,如果对于图中的任意一对点u和v都有一条从u到v的路或从v到u的路,那么就输出’Yes’,否则输出’No’.

分析:

首先求出该图的所有强连通分量,把所有分量缩点,构成一个新的DAG图。现在的问题变成了:该DAG图是否对于任意两点都存在一条路。

多画几个图可以知道该DAG图只能是一条链的时候才行(自己画图验证一下),用拓扑排序验证(队列中始终只有一个元素)。

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 6e3 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int in[N];
int out[N];
int head[N],h[N];
int low[N],dfn[N];
int ins[N],beg[N];
int cnt,id,num,cntt;
int n, m, t;
queue<int> q;
stack<int> s;
struct P {
int to, nxt;
} e[ * N], g[ * N]; void add(int u, int v) {//原图
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void ADD(int u, int v) {//缩点图
g[cntt].to = v;
g[cntt].nxt = h[u];
h[u] = cntt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id;
ins[u]=;
s.push(u);
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;
ins[v]=;
}while(u!=v);
num++;
}
}
bool work()
{
while(!q.empty()) q.pop();
for(int i=;i<num;i++){
if(!in[i]) q.push(i);
// if(in[i]>1||out[i]>1) return 0;//链上点出度入度都不大于1
}
int ans=;
while(!q.empty()){
if(q.size()>) return ;
int u=q.front();q.pop();
ans++;
for(int i=h[u];~i;i=g[i].nxt){
int v=g[i].to;
in[v]--;
if(!in[v]) q.push(v);
}
}
return ans==num;//成链
}
void init()
{
cnt = num = id = cntt= ;
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(head, -, sizeof(head));
memset(h, -, sizeof(h));
memset(low,, sizeof(low));
memset(dfn,, sizeof(dfn));
memset(ins,, sizeof(ins));
memset(beg,, sizeof(beg));
}
int main() {
ci(t);
while(t--)
{
ci(n),ci(m);
init();
for (int i = ; i < m; i++) {
int x, y;
ci(x), ci(y);
add(x, y);
}
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++){
for(int j=head[i];~j;j=e[j].nxt){
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++,in[beg[v]]++,ADD(beg[i],beg[v]);//缩点后的图
}
}
puts(work()?"Yes":"No");
}
}

最新文章

  1. JavaScript知识 一、JS的数据类型
  2. 【Python】用户登录三次锁定
  3. 使用javamail发信过程中的一些问题及解决方法
  4. sizeToFit()使用心得
  5. response小结(一)——用response向客户端输出中文数据(乱码问题分析)
  6. Linux服务的管理
  7. JS 实现 startWith endWith函数
  8. 用户输入与while循环
  9. cocos2dx 图片压缩工具 推荐
  10. webpack2使用ch10-处理图片(png jpg svg 等) 限制图片 压缩图片
  11. 实现ajax的步骤
  12. Food Log with Speech Recognition and NLP
  13. 【托业】【新托业TOEIC新题型真题】学习笔记4-题库一-&gt;P7
  14. day02-格式化输出
  15. 在eclipse中编译调试ns3
  16. 九度 1557:和谐答案 (LIS 变形)
  17. PHP发送HTTP请求的6种方法
  18. 笔记:《机器学习训练秘籍》——吴恩达deeplearningai微信公众号推送文章
  19. BZOJ 3224 普通平衡树 | 平衡树模板
  20. 20155328 2016-2017-2 《Java程序设计》第四周学习总结

热门文章

  1. mongodb 3.4 学习 (五)备份&amp;恢复
  2. monkeyrunner多点触摸
  3. Laravel 教程 - 实战 iBrand 开源电商 API 系统
  4. github的初步认识
  5. ZOJ 3379 Master Spark
  6. 比較全的XML系列工具 能够轻松实现排版、转换和打印!
  7. 布局方式-flex布局
  8. Photoshop 画布的渐变填充
  9. 2018.10.16 Java的IO与NIO
  10. 如何解决“请考虑使用 app.config 将程序集“XXXXXXXX”从版本XXXX重新映射到版本XXXX”的问题