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 ...
随机推荐
- Django REST framework(官方说明文档翻译)(1快速开始 )
http://www.django-rest-framework.org/tutorial/quickstart/ 第一部分:快速开始 我们将创建一个简单的api接口,用来给admin用户查看及编辑系 ...
- poj3254(状态压缩DP)
poj3254 题意 给出一个01矩阵,1表示当前这个位置可以放牛,要求放牛的方案保证牛不能左右或上下相邻,求方案数. 分析 dp[S][i]: 表示到 i 行时的状态S(用二进制数表示),那么状态转 ...
- Requirement Analysis
BRD:Business Requirements Document,商业需求文档.这是产品声明周期中最早的问的文档,再早就应该是脑中的构思了,其内容涉及市场分析,销售策略,盈利预测等,通常是和老大们 ...
- Struts2笔记--文件上传
Servlet 3.0规范的HttpServletRequest已经提供了方法来处理文件上传但这种上传需要在Servlet中完成.而Struts2则提供了更简单的封装. Struts2默认使用的是Ja ...
- Linux查看哪些进程用了Swap分区
如果系统的物理内存用光了,则会用到swap.系统就会跑得很慢,但仍能运行;如果Swap空间用光了,那么系统就会发生错误.通常会出现“application is out of memory”的错误,严 ...
- 利用mkfs.ubifs和ubinize两个工具制作UBI镜像
转:http://blog.sina.com.cn/s/blog_9452251d01015z9h.html 有了mkfs.ubifs和ubinize两个工具后,就可以制作UBIFS镜像了,具体步骤如 ...
- Redis的数据类型之String
Redis主要支持的数据类型有5种:String ,Hash ,List ,Set ,和 Sorted Set. Redis数据类型String string类型在redis中是最常见的类型,valu ...
- Ext JS 5 gpl版本 官方原版的下载方法
先进入官网: 然后在导航的Products中选择Sencha Ext JS,会看到以下页面: 这时候不要单击Download按钮,而是要单击导航中的DETAILS,页面切换后,就可在底部看到GPL版本 ...
- Windows API常用函数
转自:http://www.cnblogs.com/xiashengwang/p/4026259.html .NET中虽然类库很强,但还是有些时候功能有限,掌握常用的api函数, 会给我们解决问题提供 ...
- Window mode
D3D window mode:Upper left (0,0)是左上角 OGL window mode:Lower left(0,0)是左下角 nvn API nvn::Device::SetWin ...