题意:判断一个有向图中的任意两点u、v,是否可以由其中一个点到达另一个点。

分析:这个问题转化以后就是:将该图强连通缩点后再判断其是否是单向连通的。缩点用Tarjan处理强连通分量。

有一个定理是这样的:一个有向图是单项连通的当且仅当其拓扑排序唯一。那么将这个子问题再转化为其缩点之后的图拓扑排序是否唯一。

如果一个有向图拓扑排序唯一,那么在根据入度求拓扑排序的过程中,不会有超过一个点在同一时刻同时为0。

#include<stack>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN = 1e3+;
struct Edge{
int to,next;
}edges[MAXN<<],E[MAXN<<];
int ID,H[MAXN];
int dfn[MAXN],low[MAXN],sccno[MAXN],head[MAXN],tot,dfs_clock,scc_cnt,in[MAXN],sccnum[MAXN];
stack<int> S; void init()
{
ID=dfs_clock=tot=scc_cnt=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(in,,sizeof(in));
memset(sccno,,sizeof(sccno));
memset(head,-,sizeof(head));
memset(H,-,sizeof(H));
} void Tarjan(int u)
{
int v;
dfn[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=head[u];~i;i=edges[i].next){
v = edges[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
++scc_cnt;
while(true){
int x = S.top();S.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void AddEdge(int u,int v)
{
edges[tot]=(Edge){v,head[u]};
head[u]=tot++;
}
void new_AddEdge(int u,int v){
E[ID]= (Edge){v,H[u]};
H[u]=ID++;
} bool Topo()
{
queue<int> Q;
for(int u=;u<=scc_cnt;++u){
if(!in[u]) Q.push(u);
}
if(Q.size()>) return false;
while(!Q.empty()){
int x = Q.front();Q.pop();
for(int i=H[x];~i;i=E[i].next){
int v =E[i].to;
in[v]--;
if(!in[v]) Q.push(v);
}
if(Q.size()>) return false;
}
return true;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
init();
for(int i=;i<=M;++i){
scanf("%d%d",&u,&v);
AddEdge(u,v);
}
for(int i=;i<=N;++i){
if(!dfn[i])Tarjan(i);
}
for(int u=;u<=N;++u){
for(int i=head[u];~i;i=edges[i].next){
int v=edges[i].to;
if(sccno[u]!=sccno[v]){
new_AddEdge(sccno[u],sccno[v]);
in[sccno[v]]++;
}
}
}
if(Topo()||scc_cnt==) 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. ADO.NET数据库编程

    ADO.NET数据库编程 1.ADO.NET的相关概念. Microsoft的新一代技术,是ADO组件的后继者. 主要目的是在.NET Framework平台存取数据. 提供一致的对象模型,可以存取和 ...

  2. YARN源码分析(一)-----ApplicationMaster

    转自:http://blog.csdn.net/androidlushangderen/article/details/48128955 YARN学习系列:http://blog.csdn.net/A ...

  3. dom4j api 详解

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP. DOM4J ...

  4. Unity3D和网页数据交互的基本原理

    简介: 1.Unity3D的游戏引擎是和编辑器集成在一起的,所有它也是一个制作/开发平台. 2.Unity3D是使用JavaScript.C#作为核心脚本语言来驱动事个游戏引擎. 3.平台可以发布Ex ...

  5. debug命令

    debug -r 查看寄存器-a 输入指令-t 执形命令 通用寄存器:AX=AH+ALBX=BH+BLCX=CH+CLDX=DH+DL 2Byte 16bitFFFF0-(2的16次方减1) debu ...

  6. c# @符号后面对 双引号转义

    本文讲述c#中如何转义双引号. c#中转义双引号",使用的转义字符仍然是\. string str = "\"www.itjsxx.com\""; 但 ...

  7. 【python】计算器

    from __future__ import division import sys from math import * from PyQt4.QtCore import * from PyQt4. ...

  8. Android无线测试之—UiAutomator UiObject API介绍四

    输入文本与清除文本 一.输入文本与清除文本相关API 返回值 API 描述 boolean setText(String test) 在对象中输入文本 void clearTextField() 清除 ...

  9. spring boot打包会有.war.original文件的原因 (笔记)

    今天使用spring boot 2.1.1.RELEASE版本搭建项目,虽然可以直接打包成可运行的jar包,但是由于公司准备采用docker容器来管理项目,所以需要把jar包变成war包,并且war包 ...

  10. xmpp muc 群聊协议 3

    6. Entity Use Cases A MUC implementation MUST support Service Discovery [7]. 服务端必须实现 service discove ...