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 ...
随机推荐
- BZOJ 4589 Hard Nim(FWT加速DP)
题目链接 Hard Nim 设$f[i][j]$表示前$i$个数结束后异或和为$j$的方案数 那么$f[i][j] = f[i-1][j$ $\hat{}$ $k]$,满足$k$为不大于$m$的质数 ...
- 每天一个Linux命令(10)cp命令
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...
- Spoj SUBLEX - Lexicographical Substring Search
Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...
- Matrix.LookAtLH()和Matrix.LookAtRH()所表达的涵义
function lookAtLH(eye:Vector3D, at:Vector3D, up:Vector3D) 一个摄像机矩阵可有由三个部分组成:摄像机位置.目标位置以及摄像机上下方.对应的就是上 ...
- Scala各种符号含义;scala =>符号含义总结
符号: <- :for循环 -> :map映射 => :匿名函数 Int= :scala函数 _ :通配符 https://blog.csdn.net/bon_mot/article ...
- swift初探(供objective c开发人员參考)
6月初的wwdc苹果推出了一门新的开发语言swift.系统10.9.3以上安装xcode6 beta版就可以体验swift. 苹果公司做了尽可能多的努力让这门语言迅速成为一个工业级的有用编程语言,而不 ...
- 利用Python,四步掌握机器学习
为了理解和应用机器学习技术,你需要学习 Python 或者 R.这两者都是与 C.Java.PHP 相类似的编程语言.但是,因为 Python 与 R 都比较年轻,而且更加“远离”CPU,所以它们显得 ...
- 【共享单车】—— React后台管理系统开发手记:UI菜单各个组件使用(Andt UI组件)
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- Gacutil.exe(全局程序集缓存工具)
全局程序集缓存 .NET Framework (current version) 其他版本 安装有公共语言运行时的每台计算机都具有称为全局程序集缓存的计算机范围内的代码缓存.全局程序集缓存中存储了专门 ...
- Windows服务器SYSTEM权限Webshell无法添加3389账户情况突破总结
转自:http://bbs.blackbap.org/thread-2331-1-1.html 近好多Silic的朋友在Windows下SYSTEM权限的php webshell下添加账户,但是却无法 ...