POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)
题目大意:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
usingnamespace std;
#define INF 0x7ffffff
#define maxn 1005
typedef longlong LL;
#define Min(a,b) (a<b?a:b)
#define MOD 1000000007
int m, n, Time, top, ans;
int Stack[maxn], dfn[maxn], low[maxn], P[maxn][maxn], blocks[maxn], In[maxn];
bool InStack[maxn];
vector<vector<int> > G;
void init()
{
memset(In, 0, sizeof(In));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(P, 0, sizeof(P));
ans = Time = top = 0;
G.clear();
G.resize(n+2);
}
bool TopSort()
{
int cnt = 0;
top = 0;
for(int i=0; i<ans; i++)
{
if(In[i] == 0)
{
Stack[top ++] = i;
cnt ++;
break;
}
} while(top)
{
int v = Stack[--top];
for(int i=0; i<ans; i++)
{
if(P[v][i] && ! --In[i])
{
Stack[top++] = i;
cnt ++;
}
}
}
return cnt == ans;
} void Tarjan(int u)
{
dfn[u] = low[u] = ++Time;
Stack[top++] = u;
InStack[u] = true;
int len = G[u].size(), v; for(int i=0; i<len; i++)
{
v = G[u][i];
if( !low[v] )
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
elseif( InStack[v] )
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
do
{
v = Stack[--top];
InStack[v] = false;
blocks[v] = ans;
}
while(u != v);
ans ++;
}
} void solve()
{
for(int i=1; i<=n; i++)
{
if(!low[i])
Tarjan(i);
} for(int i=1; i<=n; i++)
{
int len = G[i].size(), v;
for(int j=0; j<len; j++)
{
v = G[i][j];
if(blocks[i] != blocks[v])
{
int a = blocks[i], b = blocks[v];
P[a][b] ++;
if(P[a][b] == 1)
{
In[b] ++;
break;
} }
}
}
if( !TopSort() )
printf("No\n");
else
puts("Yes");
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d",&n, &m);
init();
while(m --)
{
int a, b;
scanf("%d %d",&a, &b);
G[a].push_back(b);
}
solve();
}
return0;
}
POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)的更多相关文章
- 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 ...
随机推荐
- Java基础知识强化之集合框架笔记28:ArrayList集合练习之去除ArrayList集合中的重复字符串元素(升级)
1. 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) 要求:不能创建新的集合,就在以前的集合上做. 2. 代码示例之 去除集合中重复元素,不创建新的集合: package ...
- Practice 15.07.07 计算几何 - 1
[题目在这里] A.POJ 2318 叉积判断点在直线的那一侧 cross(X2-X1,P-X1) (x1,x2是直线上两点,p是要判断的点) >0 表示在左侧 <0 表示在右侧 (右手 ...
- Python之路【第十篇】:HTML -暂无等待更新
Python之路[第十篇]:HTML -暂无等待更新
- linux 简单命令
很久没有接触linux了,很多命令也忘记了,现在自己独立安装一个linux,独立安装LAMP,让自己记录下来这段. 怎么进入命令行 init 3, 回到桌面 init 5在不是root用户情况下,切换 ...
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 中文帮助文档 分类: ASP.NET 2014-10-28 14:09 177人阅读 评论(1) 收藏
下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...
- 在iframe中获取iframe外的对象
parent.document.getElementById("dom ID"); $($(parent.document.getElementById("video-i ...
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...
- NRPE: Unable to read output 问题处理总结
自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...
- iOS UICollectionview的详细介绍
转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...
- CSS3美化表单控件
表单的默认控件在不同的浏览器中的样式不同,用户体验很差.用CSS3可以实现表单控件的美化,可以提供更好的用户体验.不足之处就是浏览器的兼容性问题. 一.下拉控件 效果图: 下拉控件的布局结构: < ...