POJ - 2762 Going from u to v or from v to u? (强连通缩点+判断单向连通)
题意:判断一个有向图中的任意两点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? (强连通缩点+判断单向连通)的更多相关文章
- 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 ...
随机推荐
- tomocat解决乱码问题
使用Tomcat进行JSP开发最头疼的莫过于中文乱码问题了,总结Tomcat乱码问题出现的原因必须明白以下几点: 1.Tomcat一般总是默认使用ISO-8859-1作为字符编码方式的.所以,除非你在 ...
- css 禅意花园 笔记
1. FOUC(Flash Of Unstyled Content) 现象( 在某些情况下,IE加载网页时会出现短暂的CSS样式失效. a: 只发生在Windows上的 IE (5.0版本以上) b ...
- 使用jmeter实现对jar包的调用
一.前言 在我们测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据(如有时需要对参数的输入值使用第三方jar包进行加密操作),涉及到这种的情况,普遍做法是:手动调用jar包获得需要的值 ...
- asp.net知识汇总-页面跳转Server.Transfer和Response.Redirect
1. Server.Transfer 服务器端跳转 webform1.aspx跳转到webform2.aspx页面 webform1.aspx代码如下: protected void Page_Loa ...
- c# @符号后面对 双引号转义
本文讲述c#中如何转义双引号. c#中转义双引号",使用的转义字符仍然是\. string str = "\"www.itjsxx.com\""; 但 ...
- sql语句中3表删除和3表查询
好久没来咱们博客园了,主要近期在忙一些七七八八的杂事,包括打羽毛球比赛的准备和自己在学jqgrid的迷茫.先不扯这些没用的了,希望大家能记得小弟,小弟在此谢过大家了. 回归正题:(以下的sql是本人在 ...
- 【机器学习具体解释】KNN分类的概念、误差率及其问题
转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/50923056 勿在浮沙筑高台 KNN概念 KNN(K-Nearest Neig ...
- Django admin 注册多个app
class game(models.Model): content = models.TextField() def __str__(self): return 'To game %s' % self ...
- Web API中的模型验证Model Validation
数据注释 在ASP.NET Web API中,您可以使用System.ComponentModel.DataAnnotations命名空间中的属性为模型上的属性设置验证规则. using System ...
- NSTimer的循环引用
在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...