题意:判断一个有向图中的任意两点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. Swift-8-枚举

    // Playground - noun: a place where people can play import UIKit // 枚举语法 enum SomeEnumeration { // e ...

  2. Struts2漏洞

    近日,Struts2曝出2个高危安全漏洞,一个是使用缩写的导航参数前缀时的远程代码执行漏洞,另一个是使用缩写的重定向参数前缀时的开放式重定向漏洞.这些漏洞可使黑客取得网站服务器的“最高权限”,从而使企 ...

  3. WinCC7.3 Win764位系统安装教程

    WinCC7.3 Win764位安装教程 (1)将ISO文件解压缩. (2)编辑Setup.ini文件 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...

  4. MathType可以在Word、PPT中插入矩阵吗

    工科学生或者老师在写论文时最头痛的就是编辑公式,因为word自带的公式编辑器往往满足不了专业的公式需求,MathType就很好的解决了这个问题.在进行公式编辑时,难免会遇到输入矩阵的情况,那么怎么输入 ...

  5. RequireJS禁止缓存

    通过配置文件可以禁止加载缓存的JS文件, 这个在开发过程中非常有用具体做法如下 require.config({ paths: { "E":"/Scripts/MyMod ...

  6. python3----练习题(过滑块验证)

    # 导入模块 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webd ...

  7. mysql 暴力破解 root账号密码

    测试数据库的root账号密码大家都忘记了,好吧,那我们就暴力破解吧 1.找到my.cnf vi /etc/my.cnf在[mysqld]的段中加上一句:skip-grant-tables例如:[mys ...

  8. 第九篇:使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...

  9. 【BZOJ4550】小奇的博弈 博弈论

    [BZOJ4550]小奇的博弈 Description 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色.最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同.   小 ...

  10. shiro权限笔记

    shiro框架运行流程 认证:系统提供的用于识别用户身份的功能,通常就是登录功能.----让系统知道你是谁??授权:系统提供的为用户分配访问系统某些功能的能力.----让系统知道你能做什么?? 官网: ...