hdu3594

Cactus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1131    Accepted Submission(s): 542

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).
 
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.
 
Output
For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.


 
Sample Input
2
4
0 1
1 2
2 0
2 3
3 2
0 0
4
0 1
1 2
2 3
3 0
1 3
0 0
题意:
1:强连通图 2:图中每条边只处于一个环内 怎么样判断一个边只在一个环内呢?其实在没有找到一个环之前的时候,有一个DFS的过程,在这个过程中每走到一个点,我们就记录一下它是有哪一个点走下来的 也就是这个点的上一层的点,等到我们找到环的时候,我们马上返回去查找,查找那个点是 out了两次以上,那么肯定有边处于两个或者两个以上环内,就不符合了
#include"stdio.h"
#include"string.h"
#include"queue"
#include"stack"
#include"iostream"
#include"stdlib.h"
#define M 20005
#define inf 999999999
using namespace std;
stack<int>q;
int head[M],dfn[M],low[M],use[M],belong[M],pre[M],sum[M],flag1;
int num,index,t,n;
struct st
{
int u,v,next;
}edge[M*3];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
int i;
dfn[u]=low[u]=++index;
q.push(u);
use[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
pre[v]=u;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
{
low[u]=min(low[u],dfn[v]);
for(int j=u;j!=v;j=pre[j])//当发现后向边指向祖先点的时候,
//就顺着环向上查找,把点记录在sum++中,若有点被记录了两次
//或两次以上,则返回,肯定有边至少在两个环内;
{
sum[j]++;
if(sum[j]>1)
{
flag1++;
break;
}
}
} }
if(dfn[u]==low[u])
{
num++;
int vv;
do
{
vv=q.top();
q.pop();
use[vv]=0;
belong[vv]=num;
}while(vv!=u);
}
}
void solve()
{
num=index=0;
int i;
memset(belong,-1,sizeof(belong));
memset(dfn,0,sizeof(dfn));
memset(use,0,sizeof(use));
memset(pre,0,sizeof(pre));
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
int x,y;
while(scanf("%d%d",&x,&y),x||y)
{
x++;
y++;
add(x,y);
}
flag1=0;
solve();
int flag=0;
for(i=1;i<=n;i++)
{
if(belong[i]!=belong[1])
{
flag++;
break;
}
}
if(flag)
printf("NO\n");
else
{
if(flag1)
printf("NO\n");
else
printf("YES\n");
} }
}

判断强联通图中每条边是否只在一个环上(hdu3594)的更多相关文章

  1. 【强联通图 | 强联通分量】HDU 1269 迷宫城堡 【Kosaraju或Tarjan算法】

      为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明 ...

  2. HDU2767 Proving Equivalences(加边变为强联通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. Tecplot如何提取三维图中某条线的数据【转载】

    转载自:http://blog.sina.com.cn/s/blog_9de422500102v9by.html 截取线所在的面Data.Extract .slice from Plane,显示如下窗 ...

  4. MySQL:判断逗号分隔的字符串中是否包含某个字符串 && 如何在一个以逗号分隔的列表中的一个字段中连接MySQL中的多对多关系中的数据

    需求:      sql语句中,判断以逗号分隔的字符串中是否包含某个特定字符串,类似于判断一个数组中是否包含某一个元素, 例如:判断 'a,b,c,d,e,f,g' 中是否包含 'a',sql语句如何 ...

  5. Tarjan 求图点强联通,桥的应用

    在图中求双联通和强联通分量是我们解决非树结构的图连通问题的利器 通过求求图的双联通和强联通分量能把图转化成DAG进行求解: 行走 Description 给出一个有向图,你可以选择从任意点出发走到任意 ...

  6. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  7. Tarjan 算法求割点、 割边、 强联通分量

    Tarjan算法是一个基于dfs的搜索算法, 可以在O(N+M)的复杂度内求出图的割点.割边和强联通分量等信息. https://www.cnblogs.com/shadowland/p/587225 ...

  8. HDU 4635 多校第四场 1004 强联通

    我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...

  9. HDU 4685 Prince and Princess(二分匹配+强联通分量)

    题意:婚配问题,但是题目并不要求输出最大匹配值,而是让我们输出,一个王子可以与哪些王妃婚配而不影响最大匹配值. 解决办法:先求一次最大匹配,如果有两个已经匹配的王妃,喜欢她们两个的有两个或者以上相同的 ...

随机推荐

  1. 使用vi时提示 write error in swap file

    今天使用vi 命令时,提示write error in swap file 查了下原因,磁盘空间不够 df -h 使用 rm -rf 文件名 ,删除不用的 使用 echo "" & ...

  2. Android开发学习笔记-GridView的动态显示

    1.添加GridItem布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  3. 【11-13】A股主要指数的市盈率(PE)估值高度

    全指材料(SH000987) - 2018-11-13日,当前值:12.4646,平均值:30.54,中位数:26.09865,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 内 ...

  4. okHttp3自用封装

    okHttp都已经出到3.2.0了,现在才开始要用到它,感觉自己好low~~ 根据平时自己的习惯,还是自己做一下封装,让代码撸起来更加顺畅一点! okhttp-3.2.0和okio-1.7.0就不多说 ...

  5. jquery组件WebUploader文件上传用法详解

    这篇文章主要为大家详细介绍了jquery组件WebUploader文件上传用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 WebUploader是由Baidu WebFE(FEX)团队开发的一 ...

  6. SELECT中常用的子查询操作

    MySQL中的子查询 是在MySQL中经常使用到的一个操作,不仅仅是用在DQL语句中,在DDL语句.DML语句中也都会常用到子查询. 子查询的定义: 子查询是将一个查询语句嵌套在另一个查询语句中: 在 ...

  7. 查找被占用的端口的服务并kill掉

    转自:http://blog.csdn.net/gsls200808/article/details/52456136 方法: C:\>netstat -ano|findstr 8000 TCP ...

  8. WebForm的初步认识

    嘿嘿,这里就简单的总结一下初步学习webform以及对他的认识,其实大家都认为webform很讨厌,因为好多都是给我们封装好的,而且现在好多的公司已经慢慢的从中逃离出来选择使用mvc架构,甚至好多的项 ...

  9. 基于VLAN的二三层转发

    [章节内容]1 MAC地址2 冲突域和广播域3 集线器.交换机.路由器   3.1 集线器   3.2 网桥和交换机   3.3 路由器4 VLAN   4.1 VLAN帧格式      4.1.1 ...

  10. #define #undef

    #include <stdio.h> int main( void ) { #define MAX 200 printf("MAX= %d\n",MAX); #unde ...