poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】
给出n个点,m条边,问是否任意两点u,v,是否满足u能够到达v,或者v能够到达u
自己写的时候以为缩一下点,然后再判断一下能不能拓扑排序就可以了
但是--wa---
后来看了这篇题解
http://edward-mj.com/archives/27
按紫书上讲的,如果图中存在有向环,则不存在拓扑排序,反之则存在
所以上面这幅图是满足拓扑排序的
但是因为u,v的入度都为0,u,v之间不能到达
所以缩点完之后的图应该满足是一条长链才行
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
int first[maxn];
int low[maxn],pre[maxn],sc[maxn];
int in[maxn],dout[maxn];
int n,m;
int ecnt,scnt,dfs_clock;
stack<int> S; vector<int> g[maxn];
int c[maxn];
int ans[maxn]; struct Edge{
int v,next;
}e[*maxn]; void init(){
ecnt = ;
memset(first,-,sizeof(first));
memset(in,,sizeof(in));
memset(dout,,sizeof(dout));
} void addedges(int u,int v){
e[ecnt].v = v;
e[ecnt].next = first[u];
first[u] = ecnt++;
} void dfs(int u){
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = first[u];~i;i = e[i].next){
int v = e[i].v;
if(!pre[v]){
dfs(v);
low[u] = min(low[u],low[v]);
}
else if(!sc[v]) low[u] = min(low[u],pre[v]);
}
if(pre[u] == low[u]){
scnt++;
for(;;){
int x = S.top();S.pop();
sc[x] = scnt;
if(x == u) break;
}
}
} void find_scc(){
while(!S.empty()) S.pop();
memset(low,,sizeof(low));memset(pre,,sizeof(pre));
memset(sc,,sizeof(sc));
dfs_clock = scnt = ;
for(int i = ;i <= n;i++) if(!pre[i]) dfs(i);
} bool topsort(int N) {
queue<int> s;
for (int i = ; i <= N; i++) {
if (!in[i]) s.push(i);
if (s.size() == ) return false;
}
while (!s.empty()) {
int u = s.front();s.pop();
bool flag = false;
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
in[v] -= ;
if (!in[v]) {
if (flag) return false;
s.push(v);
flag = true;
}
}
}
return true;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init();
for(int i = ;i <= m;i++){
int u,v;
scanf("%d %d",&u,&v);
addedges(u,v);
}
find_scc();
if(scnt == ) {
puts("Yes");
continue;
}
for(int i = ;i <= scnt;i++) g[i].clear();
for(int u = ;u <= n;u++){
for(int i = first[u];~i;i = e[i].next){
int v = e[i].v;
if(sc[u] != sc[v]) {
g[sc[u]].push_back(sc[v]);
in[sc[v]]++;
}
}
}
if(topsort(scnt)) puts("Yes");
else puts("No");
}
return ;
}
poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】的更多相关文章
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 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 ...
随机推荐
- html img加载不同大小图像速度
最近要想法提高网页的性能,在查看图片加载时,产生了试验的想法.一直以来都没有太去深究,还是挖掘一下的好. 很简单的试验,<img>加载两个图像,一个2.3MB,5000*5000,一个22 ...
- Dynamics CRM Online 快速的debug 方法
这里的前提想大家了解一下. Dynamics 365 online的产品的session是30分钟 timeout. 如果你logout之后, session还是会储存在服务器端不会release. ...
- 理解Faster-RCNN 中的Anchor
先上图看一下Faster R-CNN操作流程: 图片说明:Faster R-CNN=Fast R-CNN+RPN,其中Fast R-CNN结构不变:RPN负责生成proposals,配合最后一层的f ...
- anaconda下jieba和wordcloud安装
1.在anaconda交互环境下安装jieba,输入命令: pip install jieba 2.在https://pypi.python.org/pypi/wordcloud下载wordclou ...
- C++基础 (10) 第十天 C++中类型转换 异常 栈解旋 io操作
1之前内容的回顾 C语言中的类型转换(int)a 强转可读性太差了 C++把()拆分成了四种转换方式 static_cast static_cast在编译器编译阶段就进行转换了 2.dynamic_ ...
- nyoj124-中位数
中位数 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一组数据按从小到大的顺序依次排列,处在中间位置的一个数叫做中位数. 比如 1 5 10 11 9 其中位数就是9 ...
- preload、prefetch的认识
预加载 现在的网络情况虽然很乐观,但是 defer和async 当浏览器碰到 script 脚本的时候: <script src="script.js"></sc ...
- 基于ALSA的WAV播放和录音程序
http://blog.csdn.net/azloong/article/details/6140824 这段时间在探索ALSA架构,从ALSA Core到ALSA Lib,再到Android Aud ...
- js获取日期当天的开始时间和结束时间
//函数调用传参格式为 2018-6-6或者2018.6.6//如:startUnix(2018-6-6) 返回的时间戳格式‘1528300799’ function startUnix($date) ...
- 【hihocoder 1312】搜索三·启发式搜索(启发式搜索写法)
[题目链接]:http://hihocoder.com/problemset/problem/1312?sid=1092363 [题意] [题解] 定义一个A*函数 f = step+val 这里的v ...