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. Solr.NET快速入门(二)

    字典映射和动态字段 Solr dynamicFields可以根据用例不同地映射. 它们可以被"静态地"映射,例如,给定: <dynamicField name="p ...

  2. [转]C#使用Window图片查看器打开图片

    //建立新的系统进程 System.Diagnostics.Process process = new System.Diagnostics.Process(); //设置文件名,此处为图片的真实路径 ...

  3. strusts2_json

    引用别人的 Struts.xml <package name="default" extends ="json-default" > <act ...

  4. .apply和.call用法和区别

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...

  5. 如何把数值或者对象添加到ArrayList集合

    生成6个1~33之间的随机整数,添加到集合,并遍历 public class ArrayListDemo1 { public static void main(String[] args) { // ...

  6. jsoup解析页面

    package com.java.jsoup; /** * jsoup解析网页 * @author nidegui * @version 2019年4月29日 下午5:12:02 * */ impor ...

  7. Linux 命令查询系统负载信息

    linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长 时间,信息显示依次为:现在时间.系统已经运 ...

  8. nyoj8-一种排序

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...

  9. BJFU fudq的等式

    /* BJFU fudq的等式 http://101.200.220.237/contest/19/problem/118/ 数论 勒让德定理 二分答案 */ #include <cstdio& ...

  10. hdu 1868 水

    #include<stdio.h> int main() { int n,m,i,j,k; while(scanf("%d",&n)!=EOF) { k=2; ...