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 ...
随机推荐
- perl /m 当作多行处理
高级用法 多行匹配: zjtest7-frontend:/root/0825# cat a2.pl print "1111111111111\n"; my $_="abc ...
- spark aggregate
该函数官方的api,说的不是很明白: aggregate(zeroValue, seqOp, combOp) Aggregate the elements of each partition, and ...
- lamp apache配置虚拟主机
You don't have permission to access /index.php on this server
- malloc用法
malloc用法三部曲:(#include<stdlib.h>下的库函数) 1.malloc eg.ps=(char*)malloc(sizeof(char)*20)的意思是,动态分配空间 ...
- Period(kmp)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- C++ Primer笔记9_构造函数_拷贝构造(深拷贝与浅拷贝)
1.构造函数: >构造函数是一个特殊的.与类同名的成员函数,用于给每一个成员设置适当的初始值. >构造函数不能有返回值,函数名与类名同样. >缺省构造函数时,系统将自己主动调用该缺省 ...
- iOS 各种传值方式
属性传值 将A页面所拥有的信息通过属性传递到B页面使用 B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面. A页面DetailViewController.h ...
- 响应式布局之BootStrap
本文在于巩固基础 学习bootStrap官网http://getbootstrap.com/ 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多 ...
- IE6下a标签上发送ajax请求总是error
IE6下真是处处是坑啊!!!走过了一个又一个坑,记录一下吧. 之前不知道a标签上注册click事件之后,发送ajax请求总是error.后来经过几番网上搜索,终于找到高人遇到此坑的解决办法.原来是a标 ...
- c# 读取IntPtr 中的数据
c++的写法是这样的:LRESULT CPictureQueryDlg::OnQueryPicNty(WPARAM wp, LPARAM lp){EnableWindow(TRUE); BYTE *p ...