poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
意甲冠军:给定一个有向图有m单向边缘。免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求
弱联通重量。
算法:
缩点求强连通分量。然后又一次建图。推断新图是否是一条单链,即不能分叉,假设分叉了就会存在不可达的情况。
怎么推断是否是单链呢?
就是每次入度为0的点都仅仅有一个,即每次队列里仅仅有一个点。
( o(╯□╰)o。。。。
。好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去!
)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#define maxn 1010
#define maxm 20010 using namespace std; struct node
{
int to,next;
}edge[maxm],edge2[maxm];
int head[maxn],cnt,n;
int clk,top,s[maxn],scc,dfn[maxn],low[maxn],belong[maxn];
bool instack[maxn],vis[maxn];
int head2[maxn],cnt2,in[maxn]; typedef pair<int,int> PII;
vector<PII> xx;
queue<int> q; void add(int x,int y)
{
edge[cnt].to = y;
edge[cnt].next = head[x];
head[x] = cnt++;
} void add2(int x,int y)
{
edge2[cnt2].to = y;
edge2[cnt2].next = head2[x];
head2[x] = cnt2++;
} void dfs(int x)
{
dfn[x] = low[x] = clk++;
s[top++] = x;
instack[x] = true;
for(int i=head[x];i!=-1;i = edge[i].next)
{
int u = edge[i].to;
if(dfn[u]==-1)
{
dfs(u);
low[x] = min(low[u],low[x]);
}
else if(instack[u])
{
low[x] = min(low[x],dfn[u]);
}
}
if(low[x]==dfn[x])
{
int u;
scc++;
do
{
u = s[--top];
instack[u]=false;
belong[u] = scc;
}while(u!=x);
}
} void tarjan()
{
memset(dfn,-1,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
clk = top = scc = 0;
for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
dfs(i);
}
} bool topo()
{
memset(vis,0,sizeof(vis));
int c = 0;
while(!q.empty())
q.pop();
for(int i=1;i<=scc;i++)
{
if(!in[i])
{
c++;
q.push(i);
}
}
if(c>1) return false;
while(!q.empty())
{
int u = q.front();
q.pop();
if(vis[u]) continue;
vis[u] = true;
c = 0;
for(int i=head2[u];i!=-1;i=edge2[i].next)
{
int v = edge2[i].to;
in[v]--;
if(!in[v])
{
q.push(v);
c++;
}
}
if(c>1)
return false;
}
return true;
} int main()
{
int m,a,b,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
xx.clear();
cnt = 0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
xx.push_back(make_pair(a,b));
}
tarjan();
memset(head2,-1,sizeof(head2));
cnt2 = 0;
for(int i=0;i<xx.size();i++)
{
int u = xx[i].first,v = xx[i].second;
if(belong[u]!=belong[v])
{
add2(belong[u],belong[v]);
in[belong[v]]++;
}
}
if(topo()) printf("Yes\n");
else printf("No\n");
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
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 ...
随机推荐
- 谈谈android反编译和防止反编译的方法(转)
谈谈android反编译和防止反编译的方法(转) android基于java的,而java反编译工具很强悍,所以对正常apk应用程序基本上可以做到100%反编译还原. 因此开发人员如果不准备开源自己的 ...
- poj3414(bfs)
题目链接:http://poj.org/problem?id=3414 题意:给你两个容器 A B 问是否能够经过有限的步骤倒水,得到容量为 C 的水,输出最小的步数,同时输出每一步的操作.如果不能 ...
- Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程
Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程 引言:原本机子上已经装好了win7+Ubuntu Kylin 由win7引导,而不是Ubuntu的grub引导的双系统(安装 ...
- HttpClient 网络优化
HttpClient 网络优化 尽管Android官网推荐在2.3及后续版本中使用HttpURLConnection作为网络开发首选类,但在连接管理和线程安全方面,HttpClient还是具有很大优势 ...
- 【JAVA学习】“-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java执行參数(转)
年轻代 年老代概念 http://jefferent.iteye.com/blog/1123677 JVM的堆的内存, 是通过以下面两个參数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后 ...
- CSharp Oracle 登陆
=======后台Oracle存储过程================ 1.创建表 --判读表存在先删除begin EXECUTE IMMEDIATE 'DROP TABLE student'; ...
- Git在下搭建下环境的工具
(本文稿来自:http://www.open-open.com/news/view/55387) Git是一个快速,可扩展的,分布式的版本控制系统.Git服务器起初是专为Linux开发,后来移植至Wi ...
- OCP读书笔记(19) - 数据库空间管理
传输表空间:将linux下的数据库中的test表空间传输到windows平台下的数据库 在传输表空间前,先确定一下源库与目标数据库字符集一致: select * from nls_database_p ...
- Android Application plugin
在网易云阅读App上看到了插件管理功能,刚好自己也需要以插件的模式来扩展已有的功能,于是研究了一下,下面是一张网易云阅读App提供的插件模式,只需下载相应的插件就扩展了相应的功能,非常方便 ...
- hdu5115(区间dp)
n头狼排成一列,每头狼有两个属性,基础攻击力和附加攻击力, 第i只狼的基础攻击力是ai,附加攻击力是b(i-1) + b(i+1) 消灭一只狼,受到的伤害为基础攻击力+附加攻击力. 问消灭所有的狼受到 ...