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

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

[Submit]   [Go Back]   [Status]  
[Discuss]

题意:对于随意两个节点u,v。假设能从u到v或者v到u,那么输出Yes,否则输出No。

思路:先强连通缩点,此时一定不含环,看能不能找到一条最长路包括全部的缩点即可(实际上是找推断单链),用拓扑排序就ok,只是假设仅仅有一个强连通分量那么一定是Yes啦,否则topo排序推断 ,代码例如以下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn =1e4+10;
const ll mod=20140413;
vector<int>G1[maxn],G2[maxn],G[maxn];
int sccno[maxn],vis[maxn],scc_cnt;//sccno强连通编号,scc_cnt表示强连通分量的个数
int ind[maxn];//ind表示入度
void init_G(int n)//初始化缩点图
{
memset(ind,0,sizeof ind);
for(int i=1;i<=n;i++)G[sccno[i]].clear();
for(int i=1;i<=n;i++) {
for(int j=0;j<G1[i].size();++j){
int &v =G1[i][j];
if(sccno[i]!=sccno[v]) {
G[sccno[i]].push_back(sccno[v]);
ind[sccno[v]]++;
}
}
}
}
bool toposort(int n)//topo排序
{
init_G(n);
int u,cnt=0;
queue<int>q;
for(int i=1;i<=n;i++){
if(!ind[sccno[i]]){
if(!q.empty())return false;
q.push(sccno[i]);
cnt++;
}
}
while(!q.empty()){
u=q.front();
q.pop();
ind[u]=-1;
for(int i=0;i<G[u].size();i++) {
int &v=G[u][i];
ind[v]--;
if(ind[v]==0){
if(!q.empty())return false;
q.push(v);
cnt++;
}
}
}
return cnt==scc_cnt;
}
vector<int>S;
void init(int n)
{
memset(vis,0,sizeof vis);
memset(sccno,0,sizeof sccno);
S.clear();
scc_cnt=0;
for(int i=1;i<=n;i++) {
G1[i].clear();
G2[i].clear();
G[i].clear();
}
}
void AddEdge(int u,int v)
{
G1[u].push_back(v);
G2[v].push_back(u);
}
void dfs1(int u)
{
if(vis[u])return ;
vis[u]=1;
for(int i=0;i<G1[u].size();i++)dfs1(G1[u][i]);
S.push_back(u);
}
void dfs2(int u)
{
if(sccno[u]) return ;
sccno[u]=scc_cnt;
for(int i=0;i<G2[u].size();++i)dfs2(G2[u][i]);
}
bool is_Semiconnect(int n)///计算强连通分量,初步推断
{
for(int i=1;i<=n;i++)dfs1(i);
for(int i=S.size();i>=1;i--){
if(!sccno[S[i-1]]){
scc_cnt++;
dfs2(S[i-1]);
}
}
return scc_cnt<=1;
}
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
int u,v;
init(n);
while(m--) {
scanf("%d%d",&u,&v);
AddEdge(u,v);
}
if(is_Semiconnect(n))puts("Yes");
else{
if(toposort(n))puts("Yes");
else puts("No");
}
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)的更多相关文章

  1. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

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

  3. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. poj 2762(强连通+判断链)

    题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...

  5. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  6. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  7. [强连通分量] POJ 2762 Going from u to v or from v to u?

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

  8. poj 3177-3352边双联通

    买一送一啊  3177和3352的区别在于3177数据有重边!但是我先做3177的  那么就直接ctrl+c+v搞3352了~. 题意:给一个无向图,要令每个点之间至少有两条不重合的路,需要至少加多少 ...

  9. POJ 2762 Going from u to v or from v to u?- Tarjan

    Description 判断一个有向图是否对于任意两点 $x$,  $y$ 都有一条路径使$x - >y$或 $y - >x$ Solution 对于一个强联通分量内的点 都是可以互相到达 ...

随机推荐

  1. c++,基类声明的指针变量和引用类型变量可以指向派 生类的对象

    基类声明的指针变量和引用类型变量可以指向派生类的对象,而反过来派生类的指针却不能指向基类变量. 这与基类和派生类之间,被允许的赋值方向是相反的. 但是从逻辑上很容易推敲其合理性.

  2. 字符串查找函数 find()函数

    find()函数可以帮助你在两个字符串之间,查找很多他们的关系... #include<iostream> #include<string> using namespace s ...

  3. BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )

    直接用STL的的deque就好了... ---------------------------------------------------------------------- #include& ...

  4. Reapter 添加删除按钮

    repeater中的删除按钮和datagrid下的删除在实现上,还是有一定的区别的,由于repeater在客户端生成的html代码是非常干净的,所以特别受到众多web2.0网站的欢迎(不像datagr ...

  5. Hadoop集群的安装与配置(centos 6.5)

    一.Hadoop搭建准备(centOs6.5  且每个系统都要有同一个用户,如:hadoop)     1.IP的配置 包括Master和Slaves的IP配置,之间能够相互ping通:  例如:   ...

  6. javaCore分析示例(转)

    当两个或多个线程彼此形成循环依赖关系时,就出现了死锁.例如,如果线程 A 处于等待线程 B 的等待状态,而同时线程 B 处于等待线程 A 的等待状态,则出现了死锁.一旦形成此情况,线程 A 和线程 B ...

  7. 数据挖掘算法学习(三)NaiveBayes算法

    算法简单介绍 NBC是应用最广的分类算法之中的一个.朴素贝叶斯模型发源于古典数学理论,有着坚实的数学基础,以及稳定的分类效率.同一时候,NBC模型所需预计的參数非常少,对缺失数据不太敏感,算法也比較简 ...

  8. 关于apche无缘无故个启动不了,解决方法

    1. 对于用户不小心把apache下的conf文件不小心给修改了,可那会导致,启动不了apache, 解决办法可以重新下载一个, 64为  32位  下载地址 http://www.veryhuo.c ...

  9. 新买一款打印机hp5525N

    11900 RMB 彩色.激光.彩打. 让法国的工艺工程师给改成法语的了.

  10. day8 - isinstance--issubclass-异常处理-自定义异常

    一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 __author__ = 'Administrator' class Foo(object): pass obj ...