[poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14778 | Accepted: 3911 |
Description
Input
The first line for each case contains two integers n, m(0 < n
< 1001,m < 6000), the number of rooms and corridors in the cave.
The next m lines each contains two integers u and v, indicating that
there is a corridor connecting room u and room v directly.
Output
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
题意:给一个图,问是否为单向连通。
Kosaraju+缩点,然后拓扑序搞一下,若只有一条没有分支的链,则Yes,否则No
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=;
vector<int>G[maxn];
vector<int>rG[maxn];
vector<int>vs;
int vis[maxn];
int cmp[maxn];
int last[maxn];
int deg[maxn];
int next[maxn];
int V;
vector<int>vc[maxn];
void add_edge(int u,int v)
{
G[u].push_back(v);
rG[v].push_back(u);
}
void init(int n)
{
for(int i=;i<n;i++)
{
G[i].clear();
rG[i].clear();
vc[i].clear();
vis[i]=;
last[i]=-;
deg[i]=;
next[i]=-;
}
}
void dfs(int v)
{
vis[v]=;
for(int i=;i<G[v].size();i++)
{
if(!vis[G[v][i]])dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k)
{
vis[v]=;
cmp[v]=k;
vc[k].push_back(v);
for(int i=;i<rG[v].size();i++)
{
if(!vis[rG[v][i]])rdfs(rG[v][i],k);
}
}
int scc()
{
memset(vis,,sizeof(vis));
vs.clear();
for(int i=;i<V;i++)
{
if(!vis[i])dfs(i);
}
memset(vis,,sizeof(vis));
int k=;
for(int i=vs.size()-;i>=;i--)
{
if(!vis[vs[i]])rdfs(vs[i],k++);
}
return k;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
V=n;
init(n);
int u,v;
for(int i=;i<m;i++)
{
cin>>u>>v;
add_edge(--u,--v);
}
int k=scc();
memset(vis,,sizeof(vis));
int flag=;
int num=;
for(int i=;i<k;i++)
{
for(int j=;j<vc[i].size();j++)
{
u=vc[i][j];
for(int l=;l<G[u].size();l++)
{
v=G[u][l];
if(cmp[u]!=cmp[v])
{
if(last[cmp[v]]==-)
{
last[cmp[v]]=cmp[u];
}
else if(last[cmp[v]]==cmp[u])continue;
else flag=;
if(next[cmp[u]]==-)
{
next[cmp[u]]=cmp[v];
}
else if(next[cmp[u]]==cmp[v])
{
continue;
}
else flag=;
}
}
}
}
for(int i=;i<k;i++)
{
if(last[i]==-)num++;
}
if(flag&&num==)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} return ;
}
代码君
[poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)的更多相关文章
- POJ2762 Going from u to v or from v to u? 强连通+缩点
题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足 随意两点ab 都能 从a到达b 或 从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...
- POJ2762 Going from u to v or from v to u(单连通 缩点)
判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- 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 ...
- 【缩点+拓扑判链】POJ2762 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 ...
- Oracle基本数据字典:v$database、v$instance、v$version、dba_objects
v$database: 视图结构: SQL> desc v$database; Name Null? Type - ...
- 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 ...
- 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage
SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...
- [强连通分量] 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 ...
随机推荐
- OpenGL ES 2.0 纹理映射
纹理坐标用符点数表示,范围一般从0.0到1.0,在纹理坐标系中.纹理坐标系原点在左上侧,向右为S轴,向下为T轴.两个轴的取值范围都是0.0-1.0. 纹理映射 纹理映射:把一幅纹理图应用到相应的几何图 ...
- nodejs 保存 payload 发送过来的文件
1:接受文件 http://stackoverflow.com/questions/24610996/how-to-get-uploaded-file-in-node-js-express-app-u ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- [bzoj1002][FJOI2007 轮状病毒] (生成树计数+递推+高精度)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...
- [原]Sublime Text3 搭建16位汇编环境(windows)
最近在学习王爽的<汇编程序>,参考<简单OS开发前奏<一>EDITPLUS+MASM32搭建汇编开发环境(16位+32位)>http://www.cnblogs.c ...
- 考查嵌入式C开发人员的最好的16道题
约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int 两个字节 2 byte ( ...
- WPF笔记(2.8 常用的布局属性)——Layout
原文:WPF笔记(2.8 常用的布局属性)--Layout 这一节老没意思,啰里啰唆的尽是些HTML的属性,挑几个好玩的List出来,备忘:Padding与Margin的区别:Margin指控件边界与 ...
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- back_insert_iterator和iterator用起来不一样。
先看代码: #include<iostream> #include<vector> #include<algorithm> #include<iterator ...
- 12 个 Linux 进程管理命令介绍
执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...