Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17993   Accepted: 4816

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

题目大意就是给你张图,问你是否任意两点u,v 可以从u到v或者从v到u
两种解法 先贴拓扑的
另外有些数据
1
3 2
1 2
3 2 1
3 3
1 2
1 3
2 3
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
const int M=;
int head[N],dfn[N],in[N],low[N],bl[N],q[N];
int tot,cnt,scnt,n,m,l;
bool instack[N];
bool adj[N][N];
struct node{
    int u,to,next;
}e[M<<];
void init(){
    memset(dfn,,sizeof(dfn));
    memset(instack,,sizeof(instack));
    memset(in,,sizeof(in));
    memset(head,-,sizeof(head));
    memset(adj,,sizeof(adj));
    l=tot=cnt=scnt=;
}
void add(int u,int v){
    e[tot].u=u;e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
}
void Tajan(int u){
    instack[u]=;
    q[l++]=u;
    low[u]=dfn[u]=++cnt;
    for(int i=head[u];i+;i=e[i].next){
        int v=e[i].to;
        if(!dfn[v]) {
            Tajan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v]&&low[u]>dfn[v]) low[u]=dfn[v];
    }
    if(low[u]==dfn[u]){
        int t;
        ++scnt;
        do{
            t=q[--l];
            instack[t]=;
            bl[t]=scnt;
        }while(t!=u);
    }
}
bool Ju(int u){
    while(scnt--){
        int cont=;
        for(int i=head[u];i+;i=e[i].next){
            int v=e[i].to;
            --in[v];
            if(in[v]==) {
                cont++;u=v;
            }
        }
        if(cont>) return ;
    }
    return ;
}
int main(){
    int T,u,v;
    for(scanf("%d",&T);T--;){
        scanf("%d%d",&n,&m);
        init();
        for(int i=;i<=m;++i){
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        for(int i=;i<=n;++i)
            if(!dfn[i]) Tajan(i);
        memset(head,-,sizeof(head));
        for(int i=;i<tot;++i){
            u=e[i].u,v=e[i].to;
            if(bl[u]==bl[v]||adj[u][v]) continue;
            else {
                adj[bl[u]][bl[v]]=; // 注意这里
                add(bl[u],bl[v]);
                ++in[bl[v]];
            }
        }
        int cont=,k;
        for(int i=;i<=scnt;++i)
            if(!in[i]) {++cont;k=i;}
        if(cont>) {puts("No");continue;}
        else {
            if(Ju(k)) puts("Yes");
            else puts("No");
        }
    }
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; const int MAXN = ;
const int MAXM = ; struct Edge{
int to, next;
}edge[MAXM]; int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];
int Index, top;
int scc;
bool Instack[MAXN];
int num[MAXN];
int n, m; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u]; i != -; i = edge[i].next) {
v = edge[i].to;
if (!DFN[v]) {
Tarjan(v);
if (Low[u] > Low[v]) Low[u] = Low[v];
}
else if (Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if (Low[u] == DFN[u]) {
scc++;
do {
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
} while (v != u);
}
} void solve() {
memset(Low, , sizeof(Low));
memset(DFN, , sizeof(DFN));
memset(num, , sizeof(num));
memset(Stack, , sizeof(Stack));
memset(Instack, false, sizeof(Instack));
Index = scc = top = ;
for (int i = ; i <= n; i++)
if (!DFN[i])
Tarjan(i);
} vector<int> g[MAXN];
int linker[MAXN], used[MAXN]; bool dfs(int u) {
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if (!used[v]) {
used[v] = ;
if (linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungary() {
int res = ;
memset(linker, -, sizeof(linker));
for (int i = ; i <= scc; i++) {
memset(used, , sizeof(used));
if (dfs(i)) res++;
}
return (scc - res)==;
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &n, &m);
init();
int u, v;
for (int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
solve(); for (int i = ; i <= scc; i++) g[i].clear();
for (int u = ; u <= n; u++) {
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (Belong[u] != Belong[v])
g[Belong[u]].push_back(Belong[v]);
}
}
if(hungary()) puts("Yes");
else puts("No");
}
return ;
}

poj2762 判断一个图中任意两点是否存在可达路径 也可看成DAG的最小覆盖点是否为1的更多相关文章

  1. Floyd-Warshall求图中任意两点的最短路径

    原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...

  2. javascript实现有向无环图中任意两点最短路径的dijistra算法

    有向无环图 一个无环的有向图称做有向无环图(directed acycline praph).简称DAG 图.DAG 图是一类较有向树更一般的特殊有向图, dijistra算法 摘自 http://w ...

  3. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

    C#实现如何判断一个数组中是否有重复的元素   如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...

  4. 【C++】判断一个图是否有环 无向图 有向图(转载)

    没有找到原文出处,请参考一下链接: http://www.cnblogs.com/hiside/archive/2010/12/01/1893878.html http://topic.csdn.ne ...

  5. C#实现如何判断一个数组中是否有重复的元素

    如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...

  6. Python判断一个字符串中是否存在多个子串中的一个

    在使用python的开发过程中,常常需要判断,字符串中是否存在子串的问题, 但判断一个字符串中是否存在多个字串中的一个时,如if (a or b) in c或者if x contains a|b|c| ...

  7. c c++怎么判断一个字符串中是否含有汉字

    c c++怎么判断一个字符串中是否含有汉字 (2013-02-05 10:44:23) 转载▼     #include  #include  int main() { char sztext[] = ...

  8. C#判断一个类中有无"指定名称"的方法

    C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  9. javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数

    javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...

随机推荐

  1. linux 之学习路线

    原文地址:https://www.oschina.net/question/587367_156024 推荐的发行版如下: UBUNTU 适合纯菜鸟,追求稳定的官方支持,对系统稳定性要求较弱,喜欢最新 ...

  2. Redis(三):多机数据库的实现

    复制 在Redis中,用户可以通过SLAVEOF命令或是slaveof选项设置服务器的主从关系,从(SLAVE)服务器会复制主(Master)服务器. 旧版复制功能实现(2.8以前) 旧版复制功能主要 ...

  3. Nakamori Akina

    听过中森明菜的歌以后,一直想写点什么.恰好前段时间看过她的一个访谈https://b23.tv/av13810011,节目里已经39岁左右的她看着已经有些衰老,但是那份属于她的天真却保持的很好. 节目 ...

  4. 升级vue项目中的element-ui的版本

    首先卸载项目中的element-ui 命令为: npm uninstall element-ui / cnpm uninstall element-ui 安装更新最新的element-ui 命令为 n ...

  5. 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  6. C. Fountains

    \(整体思路没错,但是我貌似太麻烦了.......\) \(分情况讨论\) \(Ⅰ.coin和diamond各选一个物品,这个简单\) \(Ⅱ.在coin中选两个或者在diamond选两个\) \(开 ...

  7. P1790 矩形分割(隐含的电风扇)

    描述:https://www.luogu.com.cn/problem/P1790 有一个长为a,宽为b的矩形(1≤a≤6,2≤b≤6).可以把这个矩形看作是a*b个小方格. 我们现在接到了这样的一个 ...

  8. SAP ME01创建货源清单

    1业务说明 此文档使用函数:ME_DIRECT_INPUT_SOURCE_LIST创建货源清单 2前台实现 事务代码:ME01 输入抬头信息 保存即可 3代码实现 3.1调用函数 定义参数 字段 调用 ...

  9. Altium Designer PCB封装bug,元件焊盘位置偏移解决方法

    1.问题描述:在拖动几个电阻位置时,意外发现Altium designer20版本软件的一个bug——0805的电阻两焊盘位置发生了偏移,如下图所示. 2.解决办法: ①选中焊盘偏移的封装,右键剪切掉 ...

  10. [hihoCoder1231 2015BeijingOnline]求圆与多边形公共部分的周长

    题意:如题 思路:离散.将所有交点求出来,相当于将多变形的边切成了很多条元边,对每条元边,有两种情况 在圆内,答案加上此边长 在圆外,答案加上此边相对于圆心的"有向转弧" #inc ...