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 ...
随机推荐
- RSA不对称加密和公钥 私钥
理论上只要有加密的规则 基本都是可以解密的 但是如果解密需要消耗的时间过长 比如1000年 解密过后已经没什么意义了 此时可认为这种算法不能被破解 也就是说此加密可信 MD5 是一种单向操作 加密后不 ...
- LintCode 30插入区间
问题 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [[1,2], ...
- 使用memcached缓存 替代solr中的LRUCache缓存
前沿 在搜索引擎中,缓存被当做是不可缺少的部分,但是很多情况下,将缓存的实现过度依赖于分发服务器及webserver会很大程度上加重webserver 的负担,具体表现就是经常性的假死,拒绝服务,因此 ...
- mac 上使用移动硬盘
1. 打开终端,查看赢盘的Volume Name diskutil list 2. 更新fstab文件,此步骤需要输入密码 sudo nano /etc/fstab 3. 在fstab文件中写入一下内 ...
- 一个batch如何通过一个网络
一个batch下所有的图片一起经过整个网络,不是说一张图片经过网络后再让下一张进入网络,这样一个batch一起通过网络计算速度比一张一张这样快
- 项目中多条数据保存的json实例
//js代码function checkCode(num){ var typeid = $("#typeid").val(); if(typeid == "") ...
- What is the difference between try/except and assert?
assert only check if a condition is true or not and throw an exception. A try/except block can run a ...
- SVN中检出(check out) 跟导出(export) 的区别
SVN中检出(check out) 和导出(export) 的区别?观点一:SVN是常用的一种常见的版本控制软件.SVN中检出(check SVN中检出(check out) 和导出(export ...
- CS193p Lecture 10 - Multithreating, UIScrollView
Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...
- ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle
这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...