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. Oauth2.0(五):Authorization Code 授权

    Authorization Code 方式适用于有自己的服务器的应用.之所以叫这个名字,是因为流程中引入了一个叫做 authorization code 的东西.这个东西是一个一次性的临时凭证,用来换 ...

  2. Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等

    下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...

  3. Java并发编程、内存模型与Volatile

    http://www.importnew.com/24082.html  volatile关键字 http://www.importnew.com/16142.html  ConcurrentHash ...

  4. @ControllerAdvice -- 处理异常示例

    Class : SessionInterceptor package com.estate.web.filter; import javax.annotation.Resource; import j ...

  5. 【GIS】ArcGIS JS 4.X

    require(["esri/Map", "esri/views/SceneView", "esri/TileLayer/TdtMapLayer/Td ...

  6. MySQL on Linux 部署手册

    1. 背景 MySQL为开源数据库,因此可以基于源码实现安装.基于源码安装有更多的灵活性.也就是说我们可以针对自己的硬件平台选用合适的编译器来优化编译后的二进制代码,根据不同的软件平台环境调整相关的编 ...

  7. NFS 常见报错

    问题:客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主属组都为nobody解决方法:这种情况会出现在 centos6 或 NFS 4版本中,只要在挂载的时候加上 -o nfsve ...

  8. 原创:Eclipse安装Eclipse Color Themes插件后,编辑器背景颜色被改变

    如题,卸载Eclipse Color Themes插件后,背景颜色还是白色,蛋疼,修改.metadata\.plugins\org.eclipse.core.runtime\.settings中的or ...

  9. C语言内存使用的常见问题及解决之道

    一  前言 本文所讨论的“内存”主要指(静态)数据区.堆区和栈区空间(详细的布局和描述参考<Linux虚拟地址空间布局>一文).数据区内存在程序编译时分配,该内存的生存期为程序的整个运行期 ...

  10. 【truffle】Error: `truffle init` no longer accepts a project template name as an argument.

    下载范例工程时候.使用命令报错 truffle init webpack 错误如下: Error: `truffle init` no longer accepts a project templat ...