题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762

题意:输入多组样例,输入n个点和m条有向边,问该图中任意两点x, y之间是否满足x可以到y或者y可以到x。

一开始WA的原因是因为没注意到是或者, 如果是并且的话,就是一道简单的强连通分量的题,直接判断整个图是否为一个强连通分量

对于该题, 先用强连通分量进行缩点,简化图。图就变成了DAG,用拓扑排序判断图中点的入度, 图中入度为0的点只能存在一个, 若存在2个及以上的话,那么这2个点或多个点是无法互相到达。

在拓扑排序中用列队, 入度为0的点入队,每次都对列队中的数判断,若大于等于2则直接return 不满足。

#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std; int n, m, cnt, cnt1;
int head[1010], head1[1010];
int dfn[1010], low[1010], vis[1010], deep, k_color, color[1010];
stack<int>S;
int in[1010], flag; struct Edge
{
int to, next;
}edge[6010], edge1[6010]; void add(int a, int b)
{
edge[++ cnt].to = b;
edge[cnt].next = head[a];
head[a] = cnt;
} void add1(int a, int b)
{
edge[++ cnt1].to = b;
edge[cnt1].next = head1[a];
head1[a] = cnt1;
} void tarjan(int now)
{
dfn[now] = low[now] = ++ deep;
vis[now] = 1;
S.push(now);
for(int i = head[now]; i != -1; i = edge[i].next)
{
int to = edge[i].to;
if(!dfn[to])
{
tarjan(to);
low[now] = min(low[now], low[to]);
}
else if(vis[to])
{
low[now] = min(low[now], dfn[to]);
}
}
if(dfn[now] == low[now])
{
k_color ++;
while(1)
{
int temp = S.top();
S.pop();
color[temp] = k_color;
vis[temp] = 0;
if(temp == now)
break;
}
}
} void topo_sort()
{
queue<int>Q;
while(!Q.empty())
Q.pop();
for(int i = 1; i <= k_color; i ++)
if(!in[i])
Q.push(i);
if(Q.size() > 1)
{
flag = 0;
return ;
}
while(!Q.empty())
{
int temp = Q.front();
Q.pop();
for(int i = head1[temp]; i != -1; i = edge[i].next)
{
int to = edge[i].to;
in[to] --;
if(!in[to])
Q.push(to);
if(Q.size() > 1)
{
flag = 0;
return ;
}
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
flag = 1;
cnt = deep = k_color = cnt1 = 0;
memset(head, -1, sizeof(head));
memset(in, 0, sizeof(in));
memset(head1, -1, sizeof(head1));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i ++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for(int i = 1; i <= n; i ++)
if(!dfn[i])
tarjan(i);
for(int i = 1; i <= n; i ++)//缩点
{
for(int j = head[i]; j != -1; j = edge[j].next)
{
int to = edge[j].to;
int x = color[i], y = color[to];
if(x != y)
{
add1(x, y);
in[y] ++;
}
}
}
topo_sort();
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

  

POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序的更多相关文章

  1. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

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

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

  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. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  6. POJ2762 单向连通图(缩点+拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

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

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

  8. Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  9. poj2762 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: 13040 ...

随机推荐

  1. 【java】Java相关学习参考链接(持续更新)

    How to do in java,https://howtodoinjava.com/,Java手册,分版本,并且有每个版本的新特性的详细解析. Java World,https://www.jav ...

  2. 数据表为null的字段添加默认值

    UPDATE im_clusters SET `location`='深圳会展中心' WHERE `location` is NULL

  3. Spring AOP概念理解

    1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后 ...

  4. 【转】GB2312、GBK和UTF-8三种编码的简要说明

    原文地址:http://www.cnblogs.com/hust-yingjie/p/5481966.htmlGB2312.GBK和UTF-8都是一种字符编码,除此之外,还有好多字符编码.只是对于我们 ...

  5. sql语法总结

    1.创建表 . 创建时间 default current_imestamp(6) 更新时间 default current_timestamp(6)  on update current_timest ...

  6. .net程序员面试小结(内附一些面试题和答案)

    今天下午去面试,面试官和HR小姐姐都很好,没有做面试题,用聊天的方式来交流技术,整个过程很轻松,从中也学到了很多知识. 下面就来总结一下面试过程. 一.深刻了解自己的简历 无论是HR还是技术面试人,首 ...

  7. python traceback获取详细的异常信息

       运行结果 : 用try except 会报出报错信息,但是没有具体哪个地方报错,多少行,这样不利于查找报错信息 这时我们就可以使用traceback模块 运行结果:    如上图,报错信息会具体 ...

  8. Java面试宝典2018

    转 Java面试宝典2018 一. Java基础部分…………………………………………………………………………………….. 7 1.一个“.java”源文件中是否可以包括多个类(不是内部类)?有什么限制 ...

  9. 最大子矩阵和问题dp

    给定一个矩阵 matrix,其中矩阵中的元素可以包含正数.负数.和0,返回子矩阵的最大累加和.例如,矩阵 matrix 为: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 - ...

  10. BOM 浏览器对象模型_不超过 4 KB 的 document.cookie 对象

    注意: 客户端储存应该使用 Web storage API 和 IndexedDB,不推荐使用 Cookie document.cookie 对象 是服务器保存在浏览器的一小段文本信息 用于读写当前网 ...