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 ...
随机推荐
- WIN 10 增删输入法
第一步: 任务栏右击 “语言——设置” 第二步: 第三步: 删除或者增加就好.
- 网络教程(12) TCP协议
IP协议的限制 IP协议需要 datalink帧来包装它 Ethernet或者PPP 一般都有1500byte字节或者大小的限制 可能会出现的问题 Packet loss – retransmit R ...
- PHP中调用Soap/WebService
关于在PHP中如何调用Soap/WebService的描述,网络上有不少帖子.但是主要讲述了如何用PHP开发服务器端.客户端并加以关联,而很少触及在PHP中调用现成的WebService的情况.在本文 ...
- UVA-11806 Cheerleaders 计数问题 容斥定理
题目链接:https://cn.vjudge.net/problem/UVA-11806 题意 在一个mn的矩形网格里放k个石子,问有多少方法. 每个格子只能放一个石头,每个石头都要放,且第一行.最后 ...
- [caffe] caffe训练tricks
Tags: Caffe Categories: Tools/Wheels --- 1. 将caffe训练时将屏幕输出定向到文本文件 caffe中自带可以画图的工具,在caffe路径下: ./tools ...
- Windows使用docker打开新窗口error解决办法
环境 win7 Error: error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.26/containers/json ...
- NYIST 1083 美丽的校园
美丽的校园时间限制:1000 ms | 内存限制:65535 KB难度:3 描述 美丽的校园需要大家维护,作为南工学子,当然有责任! 在计科系门前有n棵树,按一行排列在计科系楼前面,它们在一条直线上, ...
- Java的五大原则
五个基本原则: 单一职责原则(Single-Resposibility Principle):一个类,最好只做一件事,只有一个引起它的变化.单一职责原则可以看做是低耦合.高内聚在面向对象原则上的引申, ...
- AS常见的错误
导入的项目使用的gradle版本和本地的要一致,不然会提示类似"Minimum supported Gradle version is 3.3. Current version is 2.1 ...
- mysql基础综述(四)
1.数据库的简单介绍 1.1 数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle 是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公司 ...