POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

2024-08-23 11:08
文章标签 排序 poj 拓扑 联通 going 2762

本文主要是介绍POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://poj.org/problem?id=2762

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

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

POJ Monthly--2006.02.26,zgl & twb


题意:

给出一个有向图,判断对于任意两点u,v,是否可以从u到达v或者从v到达u。

分析:

判断有向图的单联通性。首先强联通缩点,得到一个DAG,如果这个DAG是一条单链,那么显然是可以的。如何判断DAG是否为单链呢?只要判断拓扑序是否唯一即可。


/*** Author : fcbruce <fcbruce8964@gmail.com>** Time : Tue 14 Oct 2014 11:37:19 AM CST**/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10#ifdef _WIN32#define lld "%I64d"
#else#define lld "%lld"
#endif#define maxm 8964
#define maxn 1007using namespace std;int n,m;
int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
int e_max;int pre[maxn],low[maxn],sccno[maxn];
int st[maxn],top;
int scc_cnt,dfs_clock;int deg[maxn];inline void add_edge(int _u,int _v)
{int e=e_max++;u[e]=_u;v[e]=_v;nex[e]=fir[u[e]];fir[u[e]]=e;
}void tarjan_dfs(int s)
{pre[s]=low[s]=++dfs_clock;st[++top]=s;for (int e=fir[s];~e;e=nex[e]){int t=v[e];if (pre[t]==0){tarjan_dfs(t);low[s]=min(low[s],low[t]);}else{if (sccno[t]==0)low[s]=min(low[s],pre[t]);}}if (low[s]==pre[s]){scc_cnt++;for (;;){int x=st[top--];sccno[x]=scc_cnt;if (x==s) break;}}
}void find_scc()
{scc_cnt=dfs_clock=0;top=-1;memset(sccno,0,sizeof sccno);memset(pre,0,sizeof pre);for (int i=1;i<=n;i++)if (pre[i]==0)  tarjan_dfs(i);
}bool unique_toposort()
{top=-1;for (int i=1;i<=scc_cnt;i++){if (deg[i]==0) st[++top]=i;}for (int i=0;i<scc_cnt;i++){if (top==1 || top==-1) return false;int x=st[top--];for (int e=fir[x];~e;e=nex[e]){deg[v[e]]--;if (deg[v[e]]==0) st[++top]=v[e];}}return true;
}int main()
{
#ifdef FCBRUCEfreopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCEint T_T;scanf("%d",&T_T);while (T_T--){scanf("%d%d",&n,&m);e_max=0;memset(fir,-1,sizeof fir);for (int i=0,u,v;i<m;i++){scanf("%d%d",&u,&v);add_edge(u,v);}find_scc();int temp=e_max;e_max=0;memset(fir,-1,sizeof fir);memset(deg,0,sizeof deg);for (int e=0;e<temp;e++){if (sccno[u[e]]==sccno[v[e]]) continue;deg[sccno[v[e]]]++;add_edge(sccno[u[e]],sccno[v[e]]);}if (unique_toposort())puts("Yes");elseputs("No");}return 0;
}


这篇关于POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1099203

相关文章

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

Python中lambda排序的六种方法

《Python中lambda排序的六种方法》本文主要介绍了Python中使用lambda函数进行排序的六种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1.对单个变量进行排序2. 对多个变量进行排序3. 降序排列4. 单独降序1.对单个变量进行排序

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int