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

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

 
题目意思:
给一n个点、m条边的有向图,问是否随意选两个点u和v,是否能从u到达v或者从v到达u。
 
思路:
如果弱连通分量有多个,那么肯定是不可到达的,所以用并查集处理一下。
强连通分量内部随意两个点是互相到达的,不互相到达的点在强连通分量之间,所以先用tarjan强连通分量缩点,若入度为0的个数>=2||出度为0的个数>=2那么也是不可到达的。
各种条件判断一下即可。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
using namespace std; #define N 1005 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
bool in[N];
int suo[N];
int cnt;
vector<int>ve[N];
int dfn[N], low[N], Time;
stack<int>st;
int father[N]; int findroot(int u){
if(father[u]!=u) father[u]=findroot(father[u]);
return father[u];
} void tarjan(int u){
dfn[u]=low[u]=Time++;
st.push(u);in[u]=true;
int i, j, k;
for(i=;i<ve[u].size();i++){
int v=ve[u][i];
if(dfn[v]==-){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(in[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
while(){
int x=st.top();
suo[x]=cnt;
in[x]=false;
st.pop();
if(x==u||st.empty()) break;
}
cnt++;
}
} main()
{
int i, j, k, u, v;
int t;
cin>>t; while(t--){
scanf("%d %d",&n,&m);
for(i=;i<=n;i++) ve[i].clear();
for(i=;i<m;i++){
scanf("%d %d",&u,&v);
ve[u].push_back(v);
}
Time=cnt=;
memset(dfn,-,sizeof(dfn));
while(!st.empty()) st.pop();
memset(in,false,sizeof(in));
for(i=;i<=n;i++){
if(dfn[i]==-){
tarjan(i);
}
}
int inn[N], out[N];
memset(inn,,sizeof(inn));
memset(out,,sizeof(out));
for(i=;i<=n;i++) father[i]=i;
for(i=;i<=n;i++){
for(j=;j<ve[i].size();j++){
int u=suo[i], v=suo[ve[i][j]];
if(u!=v){
father[findroot(v)]=findroot(u);
inn[v]++;
out[u]++;
}
}
} int num=;
for(i=;i<cnt;i++){
if(father[i]==i) num++;
}
if(num>) {
printf("No\n");continue;
} int n1, n2;
n1=n2=;
for(i=;i<cnt;i++){
if(!inn[i]) n1++;
if(!out[i]) n2++;
}
if(n1>||n2>) printf("No\n");
else printf("Yes\n");
}
}

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: 19234 ...

  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 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  4. 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题

    题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码

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

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

  6. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  7. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

  9. POJ 1182 (经典食物链 /并查集扩展)

    (參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...

随机推荐

  1. Ajax异步调用使用

    //验证通知号重复 function checkinformcodeagage() { var informcode = $("#txtinformcode").val(); if ...

  2. linux 通过 ulimit 改善系统性能

    https://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/ 概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何 ...

  3. sp工具类编写

    package com.itheima.mobilesafe74.utils; import android.content.Context; import android.content.Share ...

  4. 【Todo】Mybatis学习-偏理论

    之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html  <SSM(SpringMVC+Spring+Myba ...

  5. ajax请求成功或失败的参数

    success:function(response, status, xhr){ }, error:function(xhr, errorText, errorType){ alert(errorTe ...

  6. D3.js 第一个程序 HelloWorld

    一.HTML 是怎么输出 HelloWorld 的 <html> <head> <meta charset="utf-8"> <title ...

  7. HTTP请求过程详解

    以请求新浪的首页为例,总结HTTP请求的流程: 步骤1:浏览器首先向服务器发送HTTP请求,请求包括: 方法:GET还是POST,GET仅请求资源,POST会附带用户数据: 路径:/full/url/ ...

  8. nginx日志文件切割

    #!/bin/bash #脚本对日志文件进行切割 #脚本需要crontab 每天定时执行 #Nginx 日志文件的存放路径 logs_path="/../logs/" mkdir ...

  9. Fragment的2中载入方式!

    1.静态有动态 代码如下: public class MainActivity extends AppCompatActivity { private ContentFragment cf; @Ove ...

  10. STM8s在利用库配置端口的小问题

    在应用的时候PA2口需要设置成推挽输出,控制一个外部电源开关,端口初始化程序如下: GPIO_DeInit(GPIOA); GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_O ...