POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接: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? 强连通分量缩点+拓扑排序的更多相关文章
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- 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?【强连通分量缩点+拓扑排序】
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?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- 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 ...
- POJ2762 单向连通图(缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19552 ...
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 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 ...
- 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 ...
随机推荐
- django模型层优化(关联对象) 懒加载和预加载 +长链接
懒加载 存在于外键和多对多关系不检索关联对象的数据调用关联对象会再次查询数据库 问题根源 查看django orm的数据加载,两次. 查询user,查询menu 预加载的方法 预加载单个关联对象--s ...
- win10+caffe+GPU
由于学习需要,决定安装caffe,之前用的都是基于theano的keras.听说win下caffe很难配置,经过一个下午和晚上的配置终于成功,以此记录. 我的电脑:win10 64位,N卡GTX950 ...
- echarts将图表Y坐标刻度设置成只显示整数
echarts的配置项中没有直接将坐标刻度强制设为整数的选项,但可以通过minInterval属性将刻度以整数形式显示,在配置项的yAxis对象中添加属性: minInterval: 1 表示将刻度的 ...
- [八省联考2018]林克卡特树lct
题解: zhcs的那个题基本上就是抄这个题的,不过背包的分数变成了70分.. 不过得分开来写..因为两个数组不能同时满足 背包的话就是 $f[i][j][0/1]$表示考虑i子树,取j条链,能不能向上 ...
- Redis数据结构之intset(2)
本文及后续文章,Redis版本均是v3.2.8 上文我们说到intset整型集合的数据结构定义即元素的添加和查询操作,本文我们来看下Redis暴露给外面使用的Set集合,先通过一些基本的命令回顾下se ...
- 前端开发-1React-1概述
React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设Instagram 的网站.做出来以后,发现这套东西很 ...
- Codeforces 1098B. Nice table 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1098B.html 题解 首先,我们来证明一个结论: 合法的矩阵要么满足每列只有两种字符,要么满足每行只有两 ...
- Python 求点到直线的垂足
Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...
- vim编辑器操作命令
vim [参数] [文件 ..] 编辑指定的文件 或: vim [参数] - 从标准输入(stdin)读取文本 或: vim [参数] -t ...
- Codeforces --- 982C Cut 'em all! DFS加贪心
题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1input ...