[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 ...
随机推荐
- 练习--分治法--Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. / ...
- centos中的配置文件
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- dobbo学习
http://www.cnblogs.com/Javame/p/3632473.html 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以 ...
- django的model对象转化成dict
今天发现一个掉渣天的方法,Django的forms包里面有一个方法:model_to_dict(),它可以将一个model对象转化成dict. In [1]: from apps.dormitory. ...
- pyopenssl
https://pyopenssl.readthedocs.org/en/stable/ pyopenssl是一个封装了openssl的python模块. 使用它可以方便地进行一些加解密操作. 1.产 ...
- 在树莓派上安装leanote
作者:冥王星 "noright0@163.com" 前言 宿舍有个树莓派2B,连接到电视机,安装OSMC系统,USB接口连接移动硬盘一块,平时用来BT下载和看电影.美剧. OSMC ...
- MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
在看深入JVM虚拟机一书(p50,2.4 实战OutOfMemoryError),有一个Java堆溢出的例子,使用到了Eclipse Memory Analyzer插件,由于自己现在使用的是MyEcl ...
- rsyslog VS syslog-ng,日志记录哪家强?
还有慢慢摸索,NG的MYSQL配置,我始终没搞好. RSYSLOG则比较容易. 另外,也可以每个RSYSLOG直接入库,不需要经过LOG SERVER..如果有一个大内网的话... 配合LOGANAL ...
- Win8.1 MSDN各版本下载(64位/32位,简体中文,繁体中文,英文),X86&X64,EN,CHS,CHT
英文64位ed2k://|file|en_windows_8_1_x64_dvd_2707217.iso|3899295744|8E604054013D21209B851E41DC19F6F5|/ 英 ...