[强连通分量] POJ 2762 Going from u to v or from v to u?
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17089 | Accepted: 4590 |
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<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?的更多相关文章
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- tarjan算法+缩点:求强连通分量 POJ 2186
强连通分量:1309. [HAOI2006]受欢迎的牛 ★★ 输入文件:cow.in 输出文件:cow.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 每一头牛 ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
- 浅析强连通分量(Tarjan和kosaraju)
理解 在有向图G中,如果两点互相可达,则称这两个点强连通,如果G中任意两点互相可达,则称G是强连通图. 定理: 1.一个有向图是强连通的,当且仅当G中有一个回路,它至少包含每个节点一次. ...
- 图->连通性->有向图的强连通分量
文字描述 有向图强连通分量的定义:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly co ...
- 强连通分量(Korasaju & Tarjan)学习笔记
好久以前学过的东西...现在已经全忘了 很多图论问题需要用到强连通分量,还是很有必要重新学一遍的 强连通分量(Strongly Connected Component / SCC) 指在一个有向图中, ...
- 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. ...
- 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: ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
随机推荐
- Zju1015 Fishing Net
弦图判定 代码 #include<cstdio> #include<queue> #define mp make_pair #define fi first #define s ...
- 使用compass自动合并css雪碧图(css sprite)
本文转载自: 使用compass自动合并css雪碧图(css sprite)
- java名词解释
依赖注入 DI 面向切面编程 AOP 简单java对象 POJO -- JavaBean(spring称呼) 企业级javabean EJB
- Java语言中,类所拥有的“孩子”,他们的关系是怎样的
学习了一本有关Java的书.初步了解了一些面向对象的内容. java是由一个个的类组成的,这些类组成了java程序.类之下有他的孩子,这四个孩子分别是: 成员变量:就相当于一个个的变量,他由stati ...
- 【转】有监督训练 & 无监督训练
原文链接:http://m.blog.csdn.net/article/details?id=49591213 1. 前言 在学习深度学习的过程中,主要参考了四份资料: 台湾大学的机器学习技法公开课: ...
- flexigrid随手记
最近要用到flexigrid做表格,随手记一些知识点. 引入了两个jquery库(jquery.js和jquery-1.7.1.min.js),发生冲突,只保留一个 $("#gridTabl ...
- Smallest Bounding Rectangle - uva10173
Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...
- ShadowGun Demo学习(非技术向)
主要针对拿来主义,并对一些使用范围广的shader进行研究.虽然是4,5年前的demo,但还是有学习价值的 1.GodRays MADFINGER/Transparent/GodRays 传统的上帝之 ...
- word2013 blog test
测试一:style里的内容能保存吗?: int read; byte * buf; int tries; int read; byte * buf ...
- VS2012下基本类型大小