poj 2762 Going from u to v or from v to u?
题目描述:
为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中。洞穴中有n个房
间,有一些单向的通道连接某些房间。每次,Wind选择两个房间x和y,要求他们的一个儿子从
一个房间走到另一个房间,这个儿子可以从x走到y,也可以从y走到x。Wind保证她布置的任
务是可以完成的,但她确实不知道如何判断一个任务是否可以完成。为了使Wind下达任务更容
易些,Jiajia决定找这样的一个洞穴,每对房间(设为x和y)都是相通(可以从x走到y,或者
可以从y走到x)的。给定一个洞穴,你能告诉Jiajia,Wind是否可以任意选择两个房间而不用
担心这两个房间可能不相通吗?
//
求解的是单连通性,但首先要转换成强连通分量的求解。这是因为,强连通分量中
的顶点间存在双向的路径,因此可以将每个强连通分量收缩成一个新的顶点。在有向图的处理中
经常需要将强连通分量收缩成一个顶点。 强连通分量收缩后,再求其拓扑排序。假设求得的拓扑序存储在topo[MAX]中,topo[i]与
topo[i+1]存在边连通(i 到i+1 或i+1 到i),则定有i 到i+1 的边。而如果每个topo[i]与topo[i+1]
都存在边连通(即有i 到i+1 的边)时,topo[i]到任意topo[j]便都有边连通。
// topsort时偷懒 用了邻接矩阵 复杂度成了 n^2 写成邻接表应该要快好多
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MOD 1000000007
#define maxn 6100
#define maxm 1010
struct Edge{
int to;
int next;
Edge(){};
Edge(int u,int v){to=u;next=v;}
}E[maxn];
stack<int> S;
int V[maxm],num;
int belong[maxm];
int pre[maxm];
int dfst,scc;
int ans;
bool G[maxm][maxm];
int in[maxm];
void init(int n){
dfst=scc=;
num=;
ans=;
while(!S.empty())
S.pop();
for(int i=;i<=n;i++){
V[i]=-;
pre[i]=;
belong[i]=;
}
}
void add(int u,int v){
E[num].to=v;
E[num].next=V[u];
V[u]=num++;
}
int tarjan(int u){
int lowu=pre[u]=++dfst;
int v,e;
S.push(u);
for(e=V[u];e!=-;e=E[e].next){
v=E[e].to;
if(!pre[v]){
int lowv=tarjan(v);
lowu=min(lowu,lowv);
}
else if(!belong[v]) lowu=min(lowu,pre[v]);
}
if(lowu==pre[u]){
scc++;
for(;;){
int x=S.top();S.pop();
belong[x]=scc;
if(x==u) break;
}
}
return lowu;
}
int top[maxm],tn;
void topsort(){
int i,j,k;
tn=;
bool vi[maxm]={};
while(){
for(i=;i<=scc;i++)
if(!in[i]&&!vi[i]) break;
vi[i]=true;
// printf("%d ",i);
if(i>scc) break;
top[tn++]=i;
for(j=;j<=scc;j++)
if(G[i][j]) in[j]--;
}
}
int main()
{
int n,m,T;
int u,v;
int i,j=;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init(n);
for(i=;i<=m;i++){
scanf("%d %d",&u,&v);
add(u,v);
}
for(i=;i<=n;i++)
if(!pre[i]) tarjan(i);
// for(i=1;i<=n;i++) printf("%d ",belong[i]);
for(i=;i<=scc;in[i]=,i++)
for(j=;j<=scc;j++)
G[i][j]=;
int e,u,v; for(i=;i<=n;i++)
{
for(e=V[i];e!=-;e=E[e].next){
u=belong[i];
v=belong[E[e].to];
if(u!=v){
G[u][v]=;
in[v]++;
// printf("%d ",v);
}
}
} topsort();//printf("\\");
int flag=;
for(i=;i<tn;i++)
if(!G[top[i-]][top[i]]){
flag=;break;
}
if(flag) printf("Yes\n"); else printf("No\n");
}
return ;
}
poj 2762 Going from u to v or from v to 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?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ tarjan + dfs ] 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 L ...
- POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...
- [强连通分量] 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 ...
- 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? Tarjan算法 学习例题
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17104 Accepted: 4594 Description In o ...
随机推荐
- ssh连接慢
suse刚装完,开始用ssh的时候,总会遇到这样的问题:输入了用户名以后,等半天才出输入密码的框,很是急人.这是dns反查造成的.解决方法:编辑 /etc/ssh/sshd_conf , 将 #Use ...
- PE工具
PE编辑工具 Stud_PE v. 2.4.0.1 PE工具,用来学习PE格式十分方便. http://www.cgsoftlabs.ro/ 汉化版:http://bbs.pediy.com/show ...
- linux源代码阅读笔记 get_free_page()代码分析
/* 34 * Get physical address of first (actually last :-) free page, and mark it 35 * used. If no fre ...
- Linq to Entity中连接两个数据库时要注意的问题
Linq to Entity中连接两个数据库时要注意的问题 今天大学同学问了我一个问题,Linq to Entity中连接两个数据库时,报错“指定的 LINQ 表达式包含对与不同上下文关联的查询的引用 ...
- poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...
- [转] ADO.NET实体框架引发争论
转自:http://developer.51cto.com/art/200811/76356.htm 2008-11-11 14:00 朱永光译 infoq 我要评论(0) 一个在ADO.NET实体框 ...
- Windows JDK环境变量的配置
下载JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html 安装 计算机-->属性-->高级系统设置- ...
- wireshark: there are no interfaces on which a capture can be done
权限问题,简单的直接sudo就行. 更安全的做法是: # chmod 4755 /usr/bin/dumpcap dumpcap的所在目录可用whereis命令查看.
- XML中如何使用schema
Schema简介 DTD的语法相当复杂,并且它不符合XML文件的标准,自成一个体系,W3C定义的Schema用来代替DTD. chema相对于DTD的明显好处是XML Schema文档本身也是XML文 ...
- cojs EX_香蕉 题解报告
这道题目是香蕉的加强版 当m=100w时矩阵会很大,而且又有多组询问,所以这道题用原来香蕉的程序会T 所以我们需要更好点的做法 我们考虑优化我们的状态 首先考虑这道题的隐藏性质,考虑不合法的情况 那么 ...