Cactus

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

Total Submission(s): 1580    Accepted Submission(s): 730

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
 
Sample Output
YES
NO
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define M 100000+20
int low[M],dfn[M];
bool Instack[M];
int sccno[M],head[M];
int scc_cnt,cnt,dfs_clock;
int n,flog;
stack<int>s;
vector<int>G[M];
vector<int>scc[M];
struct node
{
int u,v;
int next;
}edge[M*2];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
flog=0;
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
int a,b;
scanf("%d",&n);
while(scanf("%d%d",&a,&b),a||b)
{
add(a,b);
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;
Instack[u]=true;
s.push(u);
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
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])
flog=1;
}
}
if(low[u]==dfn[u])
{
scc_cnt++;
for(;;)
{
v=s.top();
s.pop();
Instack[v]=false;
scc[scc_cnt].push_back(v);
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));
memset(sccno,0,sizeof(sccno));
scc_cnt=dfs_clock=0;
for(int i=0;i<n-1;i++)
{
if(!dfn[i])
tarjan(i,-1);
}
}
void slove()
{
if(scc_cnt==1&&flog==0)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
getmap();
find(0,n-1);
slove();
}
}

hdoj--3594--Cactus(tarjan)的更多相关文章

  1. 【BZOJ4331】[JSOI2012]越狱老虎桥(Tarjan)

    [BZOJ4331][JSOI2012]越狱老虎桥(Tarjan) 题面 BZOJ 然而BZOJ是权限题QwQ 洛谷 题解 先求出所有割边,那么显然要割掉一条割边. 如果要加入一条边,那么显然是把若干 ...

  2. 【BZOJ2208】[JSOI2010]连通数(Tarjan)

    [BZOJ2208][JSOI2010]连通数(Tarjan) 题面 BZOJ 洛谷 题解 先吐槽辣鸡洛谷数据,我写了个\(O(nm)\)的都过了. #include<iostream> ...

  3. 浅谈强连通分量(Tarjan)

    强连通分量\(\rm (Tarjan)\)             --作者:BiuBiu_Miku \(1.\)一些术语   · 无向图:指的是一张图里面所有的边都是双向的,好比两个人打电话 \(U ...

  4. {part1}DFN+LOW(tarjan)割点

    什么是jarjan? 1)求割点 定义:在无向连通图中,如果去掉一个点/边,剩下的点之间不连通,那么这个点/边就被称为割点/边(或割顶/桥). 意义:由于割点和割边涉及到图的连通性,所以快速地求出割点 ...

  5. HDU 3594 Cactus(仙人掌问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3594 题意: 一个有向图,判断是否强连通和每条边只在一个环中. 思路: 仙人掌问题. 用Tarjan算法判断强连 ...

  6. HDU 3594 Cactus (强连通分量 + 一个边只能在一个环里)

    题意:判断题目中给出的图是否符合两个条件.1 这图只有一个强连通分量 2 一条边只能出现在一个环里. 思路:条件1的满足只需要tarjan算法正常求强连通分量即可,关键是第二个条件,我们把对边的判断转 ...

  7. 【BZOJ】1051: [HAOI2006]受欢迎的牛(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1051 这题还好-1A了..但是前提还是看了题解的 囧.....一开始认为是并查集,oh,不行,,无法 ...

  8. POJ 3177 Redundant Paths(Tarjan)

    题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf ...

  9. hdu 4635 Strongly connected (tarjan)

    题意:给一个n个顶点m条弧的简单有向图(无环无重边),求最多能够加入多少条弧使得加入后的有向图仍为简单有向图且不是一个强连通图.假设给的简单有向图本来就是强连通图,那么输出-1. 分析: 1.用tar ...

  10. poj1236 Network of Schools【强连通分量(tarjan)缩点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html  ---by 墨染之樱花 [题目链接]http://poj.org/pr ...

随机推荐

  1. IE之css3效果兼容

    一.兼容css阴影效果(ie滤镜) 1.Shadow,阴影 .shadow { -moz-box-shadow: 3px 3px 4px #000; -webkit-box-shadow: 3px 3 ...

  2. java集合的学习笔记

    不知不觉也到了java集合这一章的学习,这因该是挺重要的一个章节,因为所有的程序都离不开数据,而一个良好的数据结构和算法应该是程序的灵魂吧. 今天对自己所初步了解的做一个总结: 数据结构是计算机存储. ...

  3. 问题集锦 ~ CSS

    #button标签点击后出现点边框 input  {outline: none;} button::-moz-focus-inner {border:  none;}

  4. python os 模块常用操作

    python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...

  5. Android--XML页面的编写

           五个页面  代码如下:    图片资源链接: https://pan.baidu.com/s/1jIoTDGE //  第一个 <RelativeLayout xmlns:andr ...

  6. IM系统中如何保证消息的可靠投递(即QoS机制)

      消息的可靠性,即消息的不丢失和不重复,是im系统中的一个难点.当初qq在技术上(当时叫oicq)因为以下两点原因才打败了icq:1)qq的消息投递可靠(消息不丢失,不重复)2)qq的垃圾消息少(它 ...

  7. 【Oracle】三种方式查看SQL语句的执行计划

    查看执行计划的方式有三种: EXPLAIN PLAN .V$SQL_PLAN .SQL*PLUS AUTOTRACE 1.EXPLAIN PLAN: 显示执行相应语句时可以使用的理论计划 读取执行计划 ...

  8. 科学存储数据格式-HDF5

    HDF数据格式 Hierarchical Data Format,可以存储不同类型的图像和数码数据的文件格式,并且可以在不同类型的机器上传输,同时还有统一处理这种文件格式的函数库.大多数普通计算机都支 ...

  9. python编写简单的html登陆页面(2)

    1  在python编写简单的html登陆页面(1)的基础上在延伸一下: 可以将动态分配数据,实现页面跳转功能: 2  跳转到新的页面:return render_template('home1.ht ...

  10. php 单例模式与常驻服务

    运行机制使得每个PHP页面被解释执行后,所有的相关资源都会被回收.也就是 说,PHP在语言级别上没有办法让某个对象常驻内存.在PHP中,所有的变量都是页面级的,无论是全局变量,还是类的静态成员,都会在 ...