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. ajax异步处理时,如何在JS中获取从Servlet或者Action中session,request

    ssh项目中,我需要登陆某个页面(如a.jsp),通过onblur()鼠标失去焦点后来触发js函数(函数是ajax请求)请求到相应的action,处理完成后将数据存放到session对象里面,然后在a ...

  2. SQL系统视图表

    SQL系统视图表 )) + '学年' + ss.Name AS listtext, sc.SchoolId FROM dbo.SchoolCalendar AS sc INNER JOIN dbo.S ...

  3. RobotFrameWork http/https oauth接口测试 (二)

    在RobotFrameWork http/https oauth接口测试 (一)中,大致介绍了相关的概念,终于可以步入正题了~~~ 先介绍下项目背景: 公司的项目采用的授权模式是第三种resource ...

  4. 混合使用 ForkJoin, Akka, Future 实现一千万个不重复整数的排序

    定位  本文适合于想要了解新语言 Scala 以及异步并发编程框架 Akka, Future 的筒鞋. 读完本文后,将了解如何使用 ForkJoin 框架.如何使用 Akka 构建并发程序.如何使用 ...

  5. iOS 给imageview添加模糊度

    开发工具带的swift2.3,3.0的朋友们改改语法吧! 首先要有一个UIimageview然后: 我是声明了一个全局的UIVisualEffectView------- private var ef ...

  6. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

    问题分析:错误提示中的“ordinal not in range(128)”,意思是,字符不在128范围内,即说明不是普通的ASCII字符,超出处理能力了. import sys print u'系统 ...

  7. JavaScript中,格式化DateTime

    参考 http://www.cnblogs.com/best/p/3537030.html new Date(parseInt(list[i].StudyTime.replace(/\D/igm, & ...

  8. noi 2728 摘花生

    题目链接: 很像上一题,加上自己本身,选最优值. http://noi.openjudge.cn/ch0206/2728/ http://paste.ubuntu.com/23402493/

  9. Balsamiq Mockups 注册码

    Blacklist: Organization name: Rick DongSerial Key: eNrzzU/OLi0odswsqgnKTM5WcMnPS1eoMTQyMjexMDQyAIEa5 ...

  10. Spring 定时任务2

    转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...