poj 2762 Going from u to v or from v to u? trajan+拓扑
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
题解:求这图是不是单联通;缩点以后这图一定是一个链;
数据
3
3 2
1 2
3 2
5 4
1 2
2 3
3 4
4 5
5 4
1 2
1 3
3 4
3 5
No
Yes
No
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
int u,v;
int next;
}edge[],edgetop[];
int head[];
int belong[];
int dfn[];
int low[];
int stackk[];
int instack[];
int du[];
int n,m,jiedge,lu,bel,top,jiedgetop;
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void updatetop(int u,int v)
{
jiedgetop++;
edgetop[jiedgetop].u=u;
edgetop[jiedgetop].v=v;
edgetop[jiedgetop].next=head[u];
head[u]=jiedgetop;
}
void dfs(int x)
{
dfn[x]=low[x]=++lu;
stackk[++top]=x;
instack[x]=;
for(int i=head[x];i;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
dfs(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(instack[edge[i].v])
low[x]=min(low[x],dfn[edge[i].v]);
}
int ne;
if(low[x]==dfn[x])
{
//cout<<x<<" "<<"XXX"<<endl;
bel++;
do
{
ne=stackk[top--];
belong[ne]=bel;
instack[ne]=;
}while(x!=ne);
}
}
void tarjan()
{
memset(dfn,,sizeof(dfn));
bel=lu=top=;
for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
}
int topsort()
{
int st;
int flag=;
for(int i=;i<=bel;i++)
{
if(du[i]==)
{
flag++;
st=i;
}
}
if(flag>) return ;
int n=bel,en;
while(n--)
{
int flagg=;
for(int i=head[st];i;i=edgetop[i].next)
{
en=edgetop[i].v;
du[en]--;
if(du[en]==)
{
flagg++;
st=en;
}
}
if(flagg>) return ;
}
return ;
}
int main()
{
int i,t;
int nn;
scanf("%d",&nn);
while(nn--)
{
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
memset(belong,,sizeof(belong));
jiedge=;
for(i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
}
tarjan();
memset(head,,sizeof(head));
memset(du,,sizeof(du));
jiedgetop=;
/* cout<<bel<<endl;
for(i=1;i<=n;i++)
cout<<belong[i]<<endl;*/
for(int i=;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(belong[u]!=belong[v])
{
du[belong[v]]++;
updatetop(belong[u],belong[v]);
}
}
if(topsort())
printf("Yes\n");
else
printf("No\n");
}
return ;
}
/*
3 2
1 2
3 2
*/
poj 2762 Going from u to v or from v to u? trajan+拓扑的更多相关文章
- 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 ...
- 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 In o ...
- poj 2762 Going from u to v or from v to u?
题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...
随机推荐
- 在vue init webpack my-project卡住的问题
在安装完node.js后,也用cnpm代替了npm,然后使用cnpm在node.js的安装路径下安装了vue-cli:cnpm install --global vue-cli.然后使用vue ini ...
- pip第三方模块
在安装好pip的情况下,在cmd窗口调用命令:pip install package -i --trusted-host http://pypi.douban.com/simple(这是豆瓣的源)
- PAT Sign In and Sign Out[非常简单]
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- javascript的Object对象的defineProperty和defineProperties
Object的属性 查看官网:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Obje ...
- ArcGIS 10——版本编辑流程
上一篇文章学习了ArcGIS有关版本机制实现的基本原理,本文结合ArcGIS的数据编辑知识来将版本编辑.协调.解决冲突.提交更改的整个过程加以说明. 同上篇文章一样,写作本文的初始意图是因为目前的项目 ...
- Python tricks(4) -- with statement
简介 with是从2.5版本引入的一个语法. 这个语法本身是为了解决try..finally繁琐的释放各类资源(文件句柄, Lock等)的问题. 如果想在旧版本中使用这个功能, 直接引入future模 ...
- 生成word附件和word域动态赋值
生成word文档和word域动态赋值,很多时候需要生成这样的word文档供下载和打印,先制作一个包含了域的 word的模板附件,放在服务器端或者字节流存入数据库,以供需要的时候代码可以获取到,如: 其 ...
- 打造高可靠与高性能的React同构解决方案
前言 随着React的兴起, 结合Node直出的性能优势和React的组件化,React同构已然成为趋势之一.享受技术福利的同时,直面技术挑战,在复杂场景下,挑战10倍以上极致的性能优化. 什么是同构 ...
- 一起学koa
gitbook地址 http://17koa.com/koa-generator-examples/