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

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

 
原题大意:T组数据,每组数据输入点的个数n和边的个数m,接下来m行输入a,b,表示从a到b有一条有向边,问是否每两个点之间存在通路(即a->b和b->a任意存在一条即可)。
 
解题思路:首先这是一个连通问题,我们要看是否连通,为了让算法变得简单,可以将原图中所有强连通分量看作一个点,于是要先缩点。
              于是,先将整个图用tarjian标记,然后缩点成DAG图(有向无环图,简称G图)。
              接下来我们考虑这个G图,想一下首先,如果其中一个点(即原图中一个强连通分量)的出度>1,那么很显然,它所指向的另外几个点互不相通。
              那么解法就来了:
              假设G图中入度为0的点为起点,它所指向的点为子节点,那么每个点只能有一个子节点。
              也就是说,这个G图中各个点链接像单向链表的时候满足题意。
              于是我们可以简单模拟(也就是用入度和出度和它们的方向暴力一遍),也可以改一下拓扑排序,判断是否为单链。
              最后输出答案就可以了。
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
queue<int> q;
struct list
{
int v;
list *next;
};
list *head[],*rear[],*map_head[],*map_rear[];
int stack[],s[],dfn[],low[],indegree[];
bool instack[];
int top,cnt,times;
void tarjian(int v)
{
dfn[v]=low[v]=++times;
stack[top++]=v;
instack[v]=true;
for(list *p=head[v];p!=NULL;p=p->next)
if(!dfn[p->v])
{
tarjian(p->v);
if(low[p->v]<low[v]) low[v]=low[p->v];
} else if(low[p->v]<low[v]&&instack[p->v]) low[v]=low[p->v];
if(dfn[v]==low[v])
{
++cnt;
do
{
v=stack[--top];
instack[v]=false;
s[v]=cnt;
}while(dfn[v]!=low[v]);
}
return;
}
bool topsort()
{
int count=,i;
while(!q.empty()) q.pop();
for(i=;i<=cnt;++i)
{
if(!indegree[i])
{
++count;
q.push(i);
}
}
if(count>) return false;
while(!q.empty())
{
int u=q.front();
q.pop();
count=;
for(list *p=map_head[u];p!=NULL;p=p->next)
{
--indegree[p->v];
if(!indegree[p->v])
{
++count;
q.push(p->v);
}
}
if(count>) return false;
}
return true;
}
int main()
{
int T,n,m,i,u,v,a,b,num;
scanf("%d",&T);
while(T--)
{
memset(head,,sizeof(head));
memset(rear,,sizeof(rear));
scanf("%d%d",&n,&m);
for(i=;i<m;++i)
{
scanf("%d%d",&u,&v);
if(rear[u]!=NULL)
{
rear[u]->next=new list;
rear[u]=rear[u]->next;
}else head[u]=rear[u]=new list;
rear[u]->v=v;
rear[u]->next=NULL;
}
top=times=cnt=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(stack,,sizeof(stack));
memset(instack,false,sizeof(instack));
memset(s,,sizeof(s));
for(i=;i<=n;++i) if(!dfn[i]) tarjian(i);
memset(map_head,,sizeof(map_head));
memset(map_rear,,sizeof(map_rear));
memset(indegree,,sizeof(indegree));
for(i=;i<=n;++i)
for(list *p=head[i];p!=NULL;p=p->next)
if(s[i]!=s[p->v])
{
++indegree[s[p->v]];
if(map_rear[s[i]]!=NULL)
{
map_rear[s[i]]->next=new list;
map_rear[s[i]]=map_rear[s[i]]->next;
} else map_head[s[i]]=map_rear[s[i]]=new list;
map_rear[s[i]]->v=s[p->v];
map_rear[s[i]]->next=NULL;
}
if(topsort()) printf("Yes\n");
else printf("No\n");
}
return ;
}

[强连通分量] POJ 2762 Going from u to v or from v to u?的更多相关文章

  1. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  2. tarjan算法+缩点:求强连通分量 POJ 2186

    强连通分量:1309. [HAOI2006]受欢迎的牛 ★★   输入文件:cow.in   输出文件:cow.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 每一头牛 ...

  3. [强连通分量] POJ 1236 Network of Schools

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16803   Accepted: 66 ...

  4. 浅析强连通分量(Tarjan和kosaraju)

    理解   在有向图G中,如果两点互相可达,则称这两个点强连通,如果G中任意两点互相可达,则称G是强连通图. 定理: 1.一个有向图是强连通的,当且仅当G中有一个回路,它至少包含每个节点一次.     ...

  5. 图->连通性->有向图的强连通分量

    文字描述 有向图强连通分量的定义:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly co ...

  6. 强连通分量(Korasaju & Tarjan)学习笔记

    好久以前学过的东西...现在已经全忘了 很多图论问题需要用到强连通分量,还是很有必要重新学一遍的 强连通分量(Strongly Connected Component / SCC) 指在一个有向图中, ...

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

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

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

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

随机推荐

  1. bzoj2424 [HAOI2010]订货

    模拟一下仓库里面存储物品的价格情况即可,如果当前物品大于仓库里面物品那么就替换一下仓库里的物品,然后订货直接从仓库里先取,仓库里不够则直接购买,每次做完后记得买当前物品填补一下仓库直至仓库填满,当然这 ...

  2. Qt之自定义信号和槽函数

    自定义信号和槽函数: 1.类的声明和实现分别放在.h和.cpp文件中: 2.类声明包含Q_OBJECT宏: 3.信号只要声明不要设计其的实现函数 4.发射信号用emit关键字 5.自定义槽的实现与普通 ...

  3. Objective-C基础

    1.C语言面向过程,OC面向对象 2.第一个OC程序 #import <Foundation/Foundation.h> int main(int argc, const char * a ...

  4. 如何拥有一个自己的Vagrant box

    这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...

  5. ClippingNode实现新手引导高亮裁切

    ClippingNode的使用 概述 ClippingNode(裁剪节点)可以用来对节点进行裁剪,可以根据一个模板切割图片的节点,生成任何形状的节点显示. ClippingNode是Node的子类,可 ...

  6. Uva 11542 乘积是平方数

    题目链接:http://vjudge.net/contest/142484#problem/A 这个题目也是2016年CCPC网赛上面的题目,当时我是不会做的,但是大牛们都知道这是一个原题,最后给一队 ...

  7. AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)(转载)

    转载自:  http://m.blog.csdn.net/blog/Enjoying_Science/42008801 题目链接:http://acm.hust.edu.cn/vjudge/probl ...

  8. spring schedule

    spring-scheduler.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?><bean ...

  9. [问题2014S06] 解答

    [问题2014S06]  解答  (本解答由巴闻嘉同学给出) 设特征多项式 \[f(x)=\det(xI_V-\varphi)=x^n+a_{n-1}x^{n-1}+\cdots+a_1x+a_0,\ ...

  10. ArcgisAdd-In开发入门实例

    1.开发环境 Vs2012+Arcgis10.2+win7 64bit 2.实现代码 首先在VS2012中新建一个解决方案,命名AddInTest. 接着,给解决方案AddInTest新建一个项目: ...