POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径。
方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变成DAG。
现在问题就变成,如何判断DAG是否是单连通图——用拓扑排序——如果拓扑排序过程中出现1个以上入度为0的点那就不是单连通图,因为有2个入度0的点,那这两个点肯定都无法到达对方。
另外,注意题目没说给的图是连通的!。。
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<algorithm>
- using namespace std;
- #define MAXN 1111
- #define MAXM 6666
- struct Edge{
- int u,v,next;
- }edge[MAXM];
- int NE,head[MAXN];
- void addEdge(int u,int v){
- edge[NE].u=u; edge[NE].v=v; edge[NE].next=head[u];
- head[u]=NE++;
- }
- int belong[MAXN],bn,stack[MAXN],top;
- bool instack[MAXN];
- int dn,dfn[MAXN],low[MAXN];
- void dfs(int u){
- dfn[u]=low[u]=++dn;
- stack[++top]=u; instack[u]=;
- for(int i=head[u]; i!=-; i=edge[i].next){
- int v=edge[i].v;
- if(dfn[v]==){
- dfs(v);
- low[u]=min(low[u],low[v]);
- }else if(instack[v]){
- low[u]=min(low[u],dfn[v]);
- }
- }
- if(dfn[u]==low[u]){
- int v; ++bn;
- do{
- v=stack[top--];
- belong[v]=bn;
- instack[v]=;
- }while(u!=v);
- }
- }
- int deg[MAXN];
- bool toposort(){
- queue<int> que;
- for(int i=; i<=bn; ++i){
- if(deg[i]==) que.push(i);
- }
- if(que.size()>) return ;
- while(!que.empty()){
- int u=que.front(); que.pop();
- for(int i=head[u]; i!=-; i=edge[i].next){
- int v=edge[i].v;
- if(--deg[v]==) que.push(v);
- }
- if(que.size()>) return ;
- }
- return ;
- }
- int main(){
- int t,n,m,a,b;
- scanf("%d",&t);
- while(t--){
- NE=;
- memset(head,-,sizeof(head));
- scanf("%d%d",&n,&m);
- while(m--){
- scanf("%d%d",&a,&b);
- addEdge(a,b);
- }
- top=bn=dn=;
- memset(instack,,sizeof(instack));
- memset(dfn,,sizeof(dfn));
- for(int i=; i<=n; ++i){
- if(dfn[i]==) dfs(i);
- }
- int tmp=NE; NE=;
- memset(head,-,sizeof(head));
- memset(deg,,sizeof(deg));
- for(int i=; i<tmp; ++i){
- int u=belong[edge[i].u],v=belong[edge[i].v];
- if(u==v) continue;
- addEdge(u,v);
- ++deg[v];
- }
- if(toposort()) puts("Yes");
- else puts("No");
- }
- return ;
- }
POJ2762 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?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
- 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: ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- POJ2762 单向连通图(缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19552 ...
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 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 ...
- poj2762 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: 13040 ...
随机推荐
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)
Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...
- magic-encoding
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天页面跳转都出问题了,各种方法都试过了, log里说语法错误,问了pp,他说是汉字的原因...果 ...
- 在link的url里新增参数
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) <%= link_to image_tag("/images/icons/aaa. ...
- CSS 确定选中变红色
textarea:focus { border: 1px solid #f4645f; outline: none; } blockquote { border-left: 4px solid #f4 ...
- PHP很有用的一个函数ignore_user_abort ()
PHP很有用的一个函数ignore_user_abort () 2013-01-16 14:21:31| 分类: PHP | 标签:php 函数 |举报|字号 订阅 ignore_us ...
- rocksdb 编译安装 日志
Compilation RocksDB's library should be able to compile without any dependency installed, although w ...
- Scrapy and Selenium
How to scrapy js? scrapy结合webkit抓取js生成的页面 http://www.cnblogs.com/Safe3/archive/2011/10/19/2217965.ht ...
- less,sass,stylus配置和应用教程及三者比较
less,sass,stylus配置和应用教程及三者比较 Less 1. 定义: Less是CSS预处理语言,在css基础之上增加了诸如变量,混合(mix),继承,运算,函数等功能,LESS既可以运 ...
- k Sum | & ||
k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...
- php的socket通信(二)
案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...