poj2762 Going from u to v or from v to u?
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13040 | Accepted: 3383 |
Description
Input
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
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1051
#define E 30000
#define M 1000000
using namespace std;
int head[N],head2[N],next2[E],vec2[E],sta[M],re,ans,next[E],id[N],in[N],vec[E],vis[N],dfn[N],low[N],clock_m,edge_m,edge_m2;
int addedge(int s,int e){
vec[edge_m]=e;next[edge_m]=head[s];head[s]=edge_m++;
}
int addedge2(int s,int e){
vec2[edge_m2]=e;next2[edge_m2]=head2[s];head2[s]=edge_m2++;
}
int init(){
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(head2,-1,sizeof(head2));
memset(id,0,sizeof(id));
memset(in,0,sizeof(in));
edge_m=0;clock_m=0;ans=0;re=0;edge_m2=0;
}
int tarjan(int x){
dfn[x]=low[x]=++clock_m;
sta[++ans]=x;
vis[x]=1;
for(int i=head[x];i!=-1;i=next[i]){
int goal=vec[i];
if(!dfn[goal]){
tarjan(goal);
low[x]=min(low[x],low[goal]);
}
else if(/*vis[goal]*/!id[goal])
low[x]=min(low[x],dfn[goal]);
}
if(low[x]==dfn[x]){
re++;int v;
do{
v=sta[ans--];
vis[v]=0;
id[v]=re;
}while(v!=x);
}
return 1;
}
int topsort(int n){
ans=0;
for(int i=1;i<=re;i++){
if(in[i]==0){
sta[ans++]=i;
}
}
if(ans>1)return 0;
while(ans>0){
ans--;
int qtop=sta[ans];
for(int j=head2[qtop];j!=-1;j=next2[j]){
in[vec2[j]]--;
if(in[vec2[j]]==0)
sta[ans++]=vec2[j];
}
if(ans>1)
return 0;
}
return 1;
}
int main()
{
int tcase,n,m,s,e;
scanf("%d",&tcase);
while(tcase--){
//system("PAUSE");
init();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&s,&e);
addedge(s,e);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
{
tarjan(i);
}
if(re==1){
printf("Yes\n");
continue;
}
for(int i=1;i<=n;i++){
for(int j=head[i];j!=-1;j=next[j]){
if(id[vec[j]]!=id[i]){
in[id[vec[j]]]++;
addedge2(id[i],id[vec[j]]);
}
}
}
if(topsort(re))printf("Yes\n");
else printf("No\n");
}
return 0;
}
poj2762 Going from u to v or from v to u?的更多相关文章
- POJ2762 Going from u to v or from v to u? 强连通+缩点
题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足 随意两点ab 都能 从a到达b 或 从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...
- POJ2762 Going from u to v or from v to u(单连通 缩点)
判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Going from u to v or from v to u? Tim ...
- 【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?
Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...
- Oracle基本数据字典:v$database、v$instance、v$version、dba_objects
v$database: 视图结构: SQL> desc v$database; Name Null? Type - ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage
SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...
- [强连通分量] 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 ...
随机推荐
- C# 弗洛伊德(Floyd)算法
弗洛伊德(Floyd)算法 主要是用于计算图中所有顶点对之间的最短距离长度的算法,如果是要求某一个特定点到图中所有顶点之间的最短距离可以用; ; ; ; ...
- Oracle10g安装中遇到的错误及解决办法
linux解决xhost: unable to open display实用技巧:在Linux下设置xhost方法步骤 第一步:用root登陆linux,启动vnc服务:第二步:根据vnc起来的端口, ...
- poj 2975 Nim_最经典的Nim取石子
题意:给你n堆石头,每次只能在一堆取最少一个石子,最后拿走最后一堆的为胜者,问胜者有多少种赢得取法 #include <iostream> #include<cstdio> u ...
- iOS 创建推送证书
1.首先你想创建推送证书和以前你做真机测试证书一样,需要实现准备一个99$的付费账号.然后登陆苹果开发者网站.http://developer.apple.com/ 2.登陆以后你能看到这个界面然后选 ...
- Android Studio Build选项的功能
再开发过程中出现了如下错误: 无论如何clean,或者删除项目中build文件夹,Rebuild Project还是报错. 解决方案:Make Project 后出现有代码报错.修复代码问题,运行项目 ...
- ASP.NET打印EXCEl报表技术总结
序言:我们在做企业项目或者一些管理系统的时候往往会用到导出到excel报表这项功能,下面我介绍的是用windows自带的excel来打印 首先必须引入:Interop.Excel.dll.Intero ...
- checklistbox的使用
public class CheckListboxHelper { #region 为checklistbox绑定数据源 /// <summary> /// 为checklistbox绑定 ...
- Android 常用 adb 命令总结
Android 常用 adb 命令总结 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命 ...
- Java学习—— for循环
For双重循环 /* 循环语句嵌套 */ class ForForTest { public static void main(String[] args) { /*int x,y = 0; for( ...
- Sql遍历数据库
Sql遍历数据库 set nocount on ) ) ) set @str='ad' Declare cur_Depart Cursor For select name,id from syscol ...