http://poj.org/problem?id=2762

题意:
给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u。

思路:

判断图是否是单连通的。

首先来一遍强连通缩点,重新建立新图,接下来我们在新图中找入度为0的点,入度为0的点只能有1个,如果有多个那么这些个点肯定是不能相互到达的。

如果只有一个入度为0的点,走一遍dfs判断新图是否是单链,如果有分支,那么分支上的点肯定是不能相互到达的。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL; const int maxn=+; int n,m; int x[maxn],y[maxn];
int in[maxn];
int flag; vector<int> G[maxn];
vector<int> new_G[maxn]; stack<int> S;
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt; void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
lowlink[u] = min(lowlink[u], pre[v]);
}
if (pre[u] == lowlink[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x == u) break;
}
}
} void find_scc(int n)
{
dfs_clock = scc_cnt = ;
memset(pre, , sizeof(pre));
memset(sccno, , sizeof(sccno));
for (int i = ; i <= n; i++)
if (!pre[i]) dfs(i);
} void dfs2(int u)
{
if(flag==) return;
if(new_G[u].size()>) {flag=;return;}
if(new_G[u].size()!=) dfs2(new_G[u][]);
} void rebulid()
{
for(int i=;i<=scc_cnt;i++) {new_G[i].clear();in[i]=;}
for(int i=;i<m;i++)
{
if(sccno[x[i]]!=sccno[y[i]])
{
new_G[sccno[x[i]]].push_back(sccno[y[i]]);
in[sccno[y[i]]]=;
}
}
int num=;
int pos;
for(int i=;i<=scc_cnt;i++)
{
if(in[i]==) {num++;pos=i;}
if(num>=) {flag=;return;}
}
if(flag) dfs2(pos);
} int main()
{
//freopen("D:\\input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
flag=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d",&x[i],&y[i]);
G[x[i]].push_back(y[i]);
}
find_scc(n);
rebulid();
if(flag) puts("Yes");
else puts("No");
}
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. [ 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 ...

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

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

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

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

  9. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

随机推荐

  1. WCF学习 (三)深入认识WCF契约

    什么是契约? 从SOA概念上讲,契约属于服务公开接口的一部分.一个服务契约,定义了服务端公开的服务方法,使用传输协议,可访问地址,传输的消息格式等内容.换句话说:契约描述了该服务的功能和作用,它告诉S ...

  2. CH601后缀数组【Trie树】

    内含字典树创建及查询模板 1601 前缀统计 0x10「基本数据结构」例题 描述 给定N个字符串S1,S2...SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前 ...

  3. 利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景

    背景: 如何加载不同环境的配置文件已经成了势在必行的,我们通常利用profile进行,详情参见我上篇博客 http://www.cnblogs.com/lianshan/p/7347890.html, ...

  4. Grafana+Prometheus监控

    添加模板一定要看说明以及依赖 监控redis https://blog.52itstyle.com/archives/2049/ http://www.cnblogs.com/sfnz/p/65669 ...

  5. 【apt install】Unable to locate package python3-pip

    解决办法: 先 sudo apt update 然后再 sudo apt install python3-pip,完成. 如果还不行的话参考这个:

  6. CMDB内功心法,助我登上运维之巅

    很多70.80后甚至90后都在金庸.古龙.梁羽生先生等武林大家熏陶下成长的,这么多年过去了,我的武侠梦依然不曾散去.曾几何,梦想有一天练就一身绝学,搂着小师妹花前月下,仗剑走天涯,快意人生.可每次的酣 ...

  7. 如何修改git分支名名称

    1. 修改本地git分支名称指令 git branch -m oldBranchName newBranchName 2. 修改远程仓库(github)上的分支名称 git本地分支名已修改,只需推送到 ...

  8. Randcher 2.0部署K8s集群(一)

    环境准备 1.系统版本 CentOS7.5 + docker ee 2.配置阿里云yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirro ...

  9. 使WebDev.WebServer.exe 当web服务器

    Vs自带的Visual Studio Development_server非常好用.比XP的IIS强多了. XP的IIS经常报访问数过多. 但是, WebDev.WebServer.exe 有个限制就 ...

  10. chrome 关闭安全模式

    chrome.exe --disable-web-security --user-data-dir