题目描述:
为了让他们的儿子变得更勇敢些,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?的更多相关文章

  1. 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. ...

  2. 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:  ...

  3. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  5. [ 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 ...

  6. 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 ...

  7. [强连通分量] 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 使用了非标准扩展:“xxx”使用 SEH,并且“xxx”有析构函数

    如果一个函数内使用了异常处理机制, VC 编译器在编译该函数时,它会给此函数插入一些“代码和信息”(代码指的是当该函数中出现异常时的回调函数,而信息主要是指与异常出现相关的一些必要的链表),因此每份函 ...

  2. OpenLDAP配置信息记录

    随着各种研发工具使用越来越多,单独为每个工具维护一个账号系统的开销越来越大,而且作为用户多个账号密码使用也越来越不方便.所以需要做一个统一账号登陆. 查询了多个方法,又因为之前用过LDAP,所以选择了 ...

  3. iOS开发 .framework的Optional(弱引用)和Required(强引用)区别

    首先,参考文档:https://blog.stackmob.com/2013/03/objective-c-tip-of-the-month-optional-frameworks/ 强引用(Requ ...

  4. CentOS(RHEL) 操作备忘

    1.安装中文语言包及切换 yum groupinstall chinese-support vi /etc/sysconfig/i18n change en_US to zh_CN 2.用户自动登录 ...

  5. Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. 【leetcode】Find Peak Element ☆

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  7. Android service的开启和绑定,以及调用service的方法

    界面: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  8. Windows7查看本地Java安装是否成功和路径的方法

    1. 在电脑开始出,点击运行,输入:CMD.右击图标以管理员身份运行.

  9. 李洪强iOS开发之Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  10. LR_问题_运行场景时提示scripts you are running in invalid

    问题描述 脚本在virtual user generator中运行正常. 在Controller中运行场景时报错: the target you defined cannot be reached. ...