hdoj Cactus 3594 (强连通分量)
来源:互联网 发布:python idle 下载 编辑:程序博客网 时间:2024/05/21 17:28
Cactus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1566 Accepted Submission(s): 723
Problem Description
1. It is a Strongly Connected graph.
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.
There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.
There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
Input
The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
Output
For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.
Sample Input
240 11 22 02 33 20 040 11 22 33 01 30 0
Sample Output
YESNO//题意:给n个点,任意条边,问是否每一条边都只属于一个环。仙人掌图性质如下1.没有横向边;
2.图中没有桥;
3.每个点的反向边和low[v]值比dfn[u]小的子节点v的数量和小于2;
思路:直接按题意来,判断图是否是强连通图且没有边属于两个或两个以上个环。对于边u -> v,在更新v的low值后即v已经出现在u所在的SCC里面,若v的low值不等于它的深度优先数,那么可以判断u -> v属于不同的环。#include<stdio.h>#include<string.h>#include<stack>#include<vector>#define N 20010#define M 50100#include<algorithm>using namespace std;struct zz{int from;int to;int next;}edge[M];int head[N],edgenum;int low[N];int dfn[N],dfs_clock;bool instack[N];int scc_cnt,flag;int n;stack<int>s;void add(int u,int v){zz E={u,v,head[u]};edge[edgenum]=E;head[u]=edgenum++;}void tarjan(int u,int fa){int v;low[u]=dfn[u]=++dfs_clock;s.push(u);instack[u]=true;for(int i=head[u];i!=-1;i=edge[i].next){v=edge[i].to;if(!dfn[v]){tarjan(v,u);low[u]=min(low[u],low[v]);}else if(instack[v]){low[u]=min(low[u],dfn[v]);if(low[v]!=dfn[v])//一条边属于两个或两个以上的环flag=1;}}if(low[u]==dfn[u]){scc_cnt++;while(1){v=s.top();s.pop();instack[v]=false;if(v==u)break;}}}void find(int l,int r){memset(low,0,sizeof(low));memset(dfn,0,sizeof(dfn));memset(instack,false,sizeof(instack));dfs_clock=scc_cnt=flag=0;for(int i=l;i<=r;i++)if(!dfn[i])tarjan(i,-1);}void solve(){find(0,n-1);if(scc_cnt==1&&flag==0)printf("YES\n");elseprintf("NO\n");}int main(){int t;int x,y;scanf("%d",&t);while(t--){scanf("%d",&n);memset(head,-1,sizeof(head));edgenum=0;while(scanf("%d%d",&x,&y),x|y){add(x,y);}solve();}return 0;}
0 0
- hdoj Cactus 3594 (强连通分量)
- HDOJ 题目3594 Cactus(强连通,判仙人掌图)
- HDU 3594 Cactus(强连通)
- HDU 3594 Cactus 强连通
- HDU 3594 Cactus 【强连通】
- HDOJ 题目3836 Equivalent Sets(强连通分量)
- HDOJ 1269 迷宫城堡 (Tarjan + 强连通分量)
- HDOJ 1269 迷宫城堡-(强连通分量)
- 【强连通分量】 HDOJ 1269 迷宫城堡
- 强连通分量 (转)
- 强连通分量(桥)
- 强连通分量(kosaraju)
- 强连通分量(Tarjan)
- 强连通分量(模板)
- 强连通分量(方法)
- hdoj Equivalent Sets 3836 (强连通分量 最少添加几条边使图强连通)
- HDU 3594 Cactus 强连通判断仙人掌图
- HDOJ 题目1827 Summer Holiday(强连通分量,缩点)
- @Responsebody与@RequestBody @RequestMapping @PathVariable获取请求路径变量
- 对时间进行格式化,例如刚刚、几秒前、几分钟前、几小时前、昨天、前天
- HDOJ 1598 find the most comfortable road(并查集+贪心)
- Linux下如何批量建立文件连接
- java加密技术(十一) 认证相关
- hdoj Cactus 3594 (强连通分量)
- zabbix 2.4 报错汇总
- public、private、protected、Friendly的区别与作用域,以及不写时的区别?
- 彻底解决mysql中文乱码的办法
- QT图片有彩到灰
- connect永远阻塞线程及解决方案
- ArcGIS教程:按属性提取 (空间分析)
- mysql varchar
- [Mysql] Limit分页性能陷阱