POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 17104 | Accepted: 4594 |
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<cstring>
#include<cstdio>
#include<stack>
#define maxm 6010
#define maxn 1010
using namespace std;
int T,n,m;
struct edge{
int u,v,next;
}e[maxm],ee[maxm];
int head[maxn],js,headd[maxn],jss;
bool exist[maxn];
int visx,cur;// cur--缩出的点的数量
int dfn[maxn],low[maxn],belong[maxn];
int rudu[maxn],chudu[maxn];
stack<int>st;
void init(){
memset(rudu,,sizeof(rudu));memset(chudu,,sizeof(chudu));
memset(head,,sizeof(head));memset(headd,,sizeof(headd));
jss=js=visx=cur=;
memset(exist,false,sizeof(exist));
while(!st.empty())st.pop();
memset(dfn,-,sizeof(dfn));memset(low,-,sizeof(low));
memset(belong,,sizeof(belong));
}
void add_edge1(int u,int v){
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
}
void tarjan(int u){
dfn[u]=low[u]=++visx;
exist[u]=true;
st.push(u);
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(dfn[v]==-){
tarjan(v);
if(low[v]<low[u]) low[u]=low[v];
}
else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
}
int j;
if(low[u]==dfn[u]){
++cur;
do{
j=st.top();st.pop();exist[j]=false;
belong[j]=cur;
}while(j!=u);
}
}
void add_edge2(int u,int v){
ee[++jss].u=u;ee[jss].v=v;
ee[jss].next=headd[u];headd[u]=jss;
}
bool topo()
{
int tp=,maxt=;
for(int i=;i<=cur;i++)
{
if(rudu[i]==){
tp++;
}
if(chudu[i]>maxt)maxt=chudu[i];
}
if(tp>)return ;// 入读等于0的缩点只能有一个 否则..
if(maxt>)return ;
return ;
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);add_edge1(u,v);
}
for(int i=;i<=n;i++){// 求强连通分量
if(dfn[i]==-) tarjan(i);
}
for(int i=;i<=js;i++){
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v]){
add_edge2(belong[u],belong[v]);
rudu[belong[v]]++;chudu[belong[u]]++;
}
}
if(topo()==) printf("Yes\n");
else printf("No\n");
}
return ;
}
思路:首先建一个有向图,进行Tarjan算法,进行缩点,缩完点之后,再建一张有向图(这张图只能成一条链),对其进行检验,入度为0的店只能有一个或没有。。
POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题的更多相关文章
- KMP算法 学习例题 POJ 3461Oulipo
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37971 Accepted: 15286 Description The ...
- 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 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ tarjan + dfs ] 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 L ...
- POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...
- [强连通分量] 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 ...
- 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: 15812 ...
随机推荐
- mac下只遍历目录不遍历文件
install brew install tree 命令 tree -d
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- 在行列都排好序的矩阵中找数 【题目】 给定一个有N*M的整型矩阵matrix和一个整数K, matrix的每一行和每一 列都是排好序的。实现一个函数,判断K 是否在matrix中。 例如: 0 1 2 5 2 3 4 7 4 4 4 8 5 7 7 9 如果K为7,返回true;如果K为6,返 回false。 【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。
从对角考虑 package my_basic.class_3; /** * 从对角开始 */ public class Code_09_FindNumInSortedMatrix { public s ...
- ios之UISplitViewController
iPad的屏幕比iPhone大,所以在界面上,iPad比iPhone多一个UISplitViewController,用来实现iPad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容. ...
- ios之UIWebView(1)
UIWebView可以让你创建一个网页浏览器,类似safari,而不是在程序中启动safsri哦.是不是觉得很棒呢?废话少说,切入正题. 一.创建UIWebView [java] view plain ...
- Cocos2d-X研究之3.0 场景切换特效汇总
Cocos2d-X研究之3.0 场景切换特效汇总 2014-08-05 0个评论 来源:游戏编程 收藏 我要投稿 cocos2d-x 3.0中场景切换特效比较多,而且游戏开 ...
- 006 CSS三种引入方式
CSS三种引入方式 一.三种方式的书写规范 1.行间式 <div style="width: 100px; height: 100px; background-color: red&q ...
- MySQL中常见的锁
一.按读写方式分类 1.读锁又称共享锁,读锁是共享的,读锁之间是互不阻塞. 2.写锁又称排他锁,写锁是排他的,写锁会阻塞其他读锁和写锁 二.按锁的粒度分类 1.表锁是MySQL中最基本的锁策略,该锁的 ...
- hosts设置本地虚拟域名
C:\Windows\System32\drivers\etc hosts 需要用管理员运行
- cache支持single/increment/increment4三种方式传输
1.cache bypass signle---data length 已知 increment ---data length 不知 用 last data address 结束数据传输 2.cac ...