poj 2762 Going from u to v or from v to u?(强连通、缩点、拓扑)
题意:(理解错了)在一个洞穴中有多个room,要求任意选两个room:u、v,都能保证u、v之间有通路,注意洞穴中的路是有向边。、
分析:强连通子图中的点必然两两之间可以互通,两个强连通子图之间有通路,必须在树上构成父子关系(不一定相邻),又两两之间有通路,即任意两个点u、v都存在父子关系——所有强连通子图构成一条链。
错误:tarjin初始化忘记更新scc_cnt=dfs_clock=0;
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; const int MAXN=; struct Edge{
int v,next;
Edge(){}
Edge(int _v,int _next):v(_v),next(_next){}
}edge[MAXN*]; struct Tp{
int u,c;
Tp(){}
Tp(int _u,int _c):u(_u),c(_c){}
}; int pre[MAXN],low[MAXN],sccno[MAXN],scc_cnt,dfs_clock;
int head[MAXN],tol;
stack<int>stk; //int mp[MAXN][MAXN];//去重边反而跑的时间更长
vector<int>G[MAXN]; int in[MAXN],color[MAXN];
queue<Tp>q; void init()
{
tol=;
memset(head,-,sizeof(head));
} void add(int u,int v)
{
edge[tol]=Edge(v,head[u]);
head[u]=tol++;
} void dfs(int u)
{
int v;
pre[u]=low[u]=++dfs_clock;
stk.push(u);
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(!pre[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(pre[u]==low[u]){
scc_cnt++;
do{
v=stk.top();
stk.pop();
sccno[v]=scc_cnt;
}while(u!=v);
}
} void find_scc(int n)
{
dfs_clock=scc_cnt=;
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(sccno,,sizeof(sccno)); for(int i=;i<=n;i++)
if(!pre[i])
dfs(i);
} int main()
{
int T;
int n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m); init();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
} find_scc(n); memset(in,,sizeof(in));
for(int i=;i<=scc_cnt;i++)
G[i].clear();
//memset(mp,0,sizeof(mp));
for(int i=;i<=n;i++)
for(int j=head[i];j!=-;j=edge[j].next)
if(sccno[i]!=sccno[edge[j].v]){
//if(mp[sccno[i]][sccno[edge[j].v]]==0){
G[sccno[i]].push_back(sccno[edge[j].v]);
//mp[sccno[i]][sccno[edge[j].v]]=1;
in[sccno[edge[j].v]]++;
//}
}
for(int i=;i<=scc_cnt;i++)
if(!in[i])
q.push(Tp(i,)); memset(color,,sizeof(color));
Tp e;
while(!q.empty())
{
e=q.front();q.pop();
color[e.c]++;
int sz=G[e.u].size();
for(int i=;i<sz;i++)
{
in[G[e.u][i]]--;
if(in[G[e.u][i]]==){
q.push(Tp(G[e.u][i],e.c+));
}
}
} int flog=;
for(int i=;i<=e.c;i++)
if(color[i]>){
flog=;
break;
} if(!flog)
printf("No\n");
else
printf("Yes\n");
}
return ;
}
poj 2762 Going from u to v or from v to u?(强连通、缩点、拓扑)的更多相关文章
- poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...
- Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u
Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...
- 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 ...
随机推荐
- maven坐标
maven坐标 <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat ...
- 编码/解码和进制转化工具hURL
编码/解码和进制转化工具hURL 在安全应用中,各种编码方式被广泛应用,如URL编码.HTML编码.BASE64等.而在数据分析时候,各种进制的转化也尤为频繁.为了方便解决这类问题,Kali Li ...
- [POI2014]FarmCraft
题目大意: 一个$n(n\le5\times10^5)$个点的树,每个点有一个权值$c_i$,从$1$出发进行欧拉遍历,每个单位时间移动一条边,记每个点$i$被访问到的时间是$t_i$,问最后$\ma ...
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-简介
本文翻译自Spring.NET官方文档Version 1.3.2. 受限于个人知识水平,有些地方翻译可能不准确,但是我还是希望我的这些微薄的努力能为他人提供帮助. 侵删. 简介 Aspect-Orie ...
- windows下搭建svn服务器
转自:http://www.cnblogs.com/cloud2rain/archive/2013/04/11/3015080.html 这篇文档非常好,转来学习,有一点就是把subversion创建 ...
- Python命令行参数学习
man python 查看python的帮助文件 命令行参数: -B Don't write .py[co] files on import. See a ...
- python xml与字典的相互转换
def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...
- jquery调用click事件的三种方式
第一种方式: $(document).ready(function(){ $("#clickme").click(function(){ alert("Hello Wor ...
- MySQL时间增加、字符串拼接
MySQL时间增加.字符串拼接 SELECT DATE_ADD(startTime, INTERVAL 10 SECOND); CONCAT(string1,string2,…)
- Zynq Fatfs文件系统应用笔记
Zynq Fatfs文件系统应用笔 Hello,panda 笔记介绍基于所描写叙述的Zynq Fatfs基于Xilinx xilffsv3.0和Sdpsv2.4,文件系统採用在Bare-Metal和轻 ...