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 ...
随机推荐
- 初学python之路-day03
我在前面的文章提到了变量的概念,这里详细介绍下变量的命名.变量名,只能是字母.数字及下划线 "_" 任意组成,而且不能以数字开头.在命名变量时,尽量避免与系统关键词重名,如:'an ...
- 新建的亚马逊云服务器EC2 ping 不通 解决方案
在EC2配置安全组里面新加一条规则
- Vertx eventbus模块解析
eventbus 事件總線 協議棧 TCP分包,粘包解決採用方案: 消息定长(定義消息体總长度),消息分为消息头和消息体 dataType bytes description int 4 包体总大小 ...
- Linux history显示时间/用户/ip的设置
在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再google,原来可以自己设置history的显示. 记录设 ...
- Codeforces 513D2 Constrained Tree
Constrained Tree 没写出来好菜啊啊. 首先根据输入我们能算出某些节点的左儿子的范围, 右儿子的范围(此时并不准确) 然后我们在划分u这个节点的时候我们从左右开始用树状数组check每一 ...
- BZOJ3160 万径人踪灭 字符串 多项式 Manachar FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8810140.html 题目传送门 - BZOJ3160 题意 给你一个只含$a,b$的字符串,让你选择一个子序列 ...
- Problem B. Beer Refrigerator
http://codeforces.com/gym/241680/problem/B比赛的时候考虑的是,它们3个尽可能接近,然后好麻烦,不如暴力枚举,这里不需要质因数分解,而是两重循环枚举所有因数,第 ...
- Selenium2+python自动化45-18种定位方法(find_elements)
前言 江湖传言,武林中流传八种定位,其中xpath是宝刀屠龙,css是倚天剑. 除了这八种,其实还有十种定位方法,眼看就快失传了,今天小编让失传已久的定位方法重出江湖! 一.十八种定位方法 前八种是大 ...
- 阿里云服务器端配置TensorFlow & jupyter
在阿里云上搭建爬取某信的公众号文章的程序时,发现需要验证码验证,技穷之后考虑做一个验证码识别程序,所以开始在服务器上搭建机器学习平台,背景,服务器上已经有其他应用在跑着了,所以不想停服,初始环境:ce ...
- 移动端click事件出现300ms延迟
问题分析: 双击缩放是指手在屏幕上快速点击两次,iOS自带的Safari浏览器会将网页缩放至原始比例.当用户在屏幕上单击某元素时,浏览器会先捕获此处单击,但浏览器不知道用户是要单击链接还是要双击该部分 ...