给出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? 【 强连通 拓扑排序】的更多相关文章

  1. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  2. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  3. 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. ...

  4. 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:  ...

  5. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  6. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  7. [ 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 ...

  8. 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 ...

  9. [强连通分量] 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 ...

随机推荐

  1. swift的计算属性和懒加载

    计算属性每次都重新计算. 懒加载只计算一次. 可以借助backing store将计算属性转化为懒加载属性. 计算属性实质上退化为函数调用. 计算属性的标示是get.set.

  2. RxSwift文档搜集与备份

    http://reactivex.io The Observer pattern done right ReactiveX is a combination of the best ideas fro ...

  3. cent os 安装mariaDB / mySQL 之后初始化的命令

      #安装mysql mysql-server,默认安装的是开源的mariaDB和它的server,mariadb-server,安装源中可能有找不到的,就换个名字再找找 yum install -y ...

  4. Project Euler 30 Digit fifth powers

    题意:判断一个数 N 的每一位的5次方的和是否为其本身 ,求出所有满足条件的数的和 思路:首先设这个数 N 为 n 位,可以简单的判断一下这个问题的上界 10 ^ n <= 9 ^ 5 × n, ...

  5. echarts地图的基本使用配置

    一.空气质量图 代码和配置如下: <template> <div class="box"> <div id="map">&l ...

  6. 参数化取值策略Sequential

    1.Sequential+Each iteration(顺序方式+每次迭代更新取值),设置Run—Logic中action循环迭代11次,并运行以上脚本,结果如下:     2.Sequential+ ...

  7. 【SSH2框架(理论篇)】--SSH2 Vs 经典三层

     这几天一直在学习使用SSH2框架.对于框架本身的使用并非非常困难.相信经过多锻炼就行熟练的掌握框架的使用,让我匪夷所思的是在使用框架的时候感觉非常熟悉,好像在哪里用过似得. 就在某次查看代码的时 ...

  8. [React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)

    In this lesson we'll create a protected route just for logged in users. We'll combine a Route with a ...

  9. 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...

  10. CSDN--十年

    昨天获得了博客专家的勋章,惊喜总是来的有点意外.这个勋章也算是来的一波三折.借着这个机会,回首一下我在CSDN的博客历史. 这个博客如今可查的最早的文章,是04年下半年写的,事实上之前应该另一些自己写 ...