Cactus

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 56 Accepted Submission(s): 34
 
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
 
Author
alpc91
 
Source
2010 ACM-ICPC Multi-University Training Contest(16)——Host by NUDT
 
Recommend
zhengfeng
 
/*
题意:给你一个强连通图,如果每条边都只属于一个圈,那么这个图就是仙人掌图。让你判断一下是不是仙人掌图。 初步思路:tarjan遍历边,每次遇到以前的边的时候,这个环中每个边都标记一下如果这条边标记两次,那么就不是仙人掌图 */
#include<bits/stdc++.h>
#define N 20005
#define M 50005
using namespace std;
/**************************强连通模板******************************/
struct node{
int from,to,next;
}edge[M];
int tol,head[N],dfn[N],low[N],flag,Count,cnt,n;
bool visit[N],vis[N];
stack<int>S;
void add(int a,int b)
{
edge[tol].from=a;edge[tol].to=b;edge[tol].next=head[a];head[a]=tol++;
}
int min(int a,int b)
{
return a<b?a:b;
}
void tarjan(int u)
{
int j,v;
dfn[u]=low[u]=cnt++;
visit[u]=vis[u]=;
S.push(u);
for(j=head[u];j!=-;j=edge[j].next)
{
v=edge[j].to;
if(!visit[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
if(dfn[v]!=low[v]) flag=;
}
if(flag) return;
}
if(dfn[u]==low[u])
{
Count++;
do{
v=S.top();
S.pop();
vis[v]=;
}while(v!=u);
}
}
/**************************强连通模板******************************/
int main()
{
int i,ncase,a,b;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d",&n);
tol=;
memset(head,-,sizeof(head));
while(scanf("%d%d",&a,&b)!=EOF)
{
if(!a && !b) break;
add(a,b);
}
memset(visit,,sizeof(visit));
memset(vis,,sizeof(vis));
flag=;
Count=;
cnt=;
for(i=;i<n;i++)
if(!visit[i]) tarjan(i);
if(flag||Count!=) printf("NO\n");
else printf("YES\n");
}
return ;
}

Cactus的更多相关文章

  1. Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA

    E. Cactus   A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...

  2. 仙人掌(cactus)

    仙人掌(cactus) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 在冲刺清华集训(THUSC) !于是它开始研究仙人掌,它想来和你一起分享它最近研究的 ...

  3. 【BZOJ】【1023】【SHOI2008】cactus仙人掌图

    DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...

  4. 1023: [SHOI2008]cactus仙人掌图 - BZOJ

    Description如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的回路 ...

  5. Cactus入门

    这是一个WebProject,有关Cactus用法详见本文测试用例 首先是web.xml <?xml version="1.0" encoding="UTF-8&q ...

  6. Cactus借助Jetty测试Servlet

    这是一个WebProject,但不需要web.xml,因为用不到它 首先是待测试的LoginServlet.java package com.jadyer.servlet; import java.i ...

  7. bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列

    1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1141  Solved: 435[Submit][ ...

  8. Codeforces 231E - Cactus

    231E - Cactus 给一个10^5个点的无向图,每个点最多属于一个环,规定两点之间的简单路:从起点到终点,经过的边不重复 给10^5个询问,每个询问两个点,问这两个点之间有多少条简单路. 挺综 ...

  9. bzoj千题计划113:bzoj1023: [SHOI2008]cactus仙人掌图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1023 dp[x] 表示以x为端点的最长链 子节点与x不在同一个环上,那就是两条最长半链长度 子节点与 ...

随机推荐

  1. angular 如何获取使用filter过滤后的ng-repeat的数据长度

    在做项目的过程中,被产品要求在内容为空的过程中显示提示信息,然哦户内容使用ng-repeat循环输出的,并且使用了filter过滤.后在谷歌上找到解决方案,如下: ​之前代码如下显示: <ul& ...

  2. session写入memcache

    1 <?php 2 class MemSession{ 3 private static $handler = null; 4 private static $lifetime = null; ...

  3. this的四种绑定形式

    一 , this的默认绑定 当一个函数没有明确的调用对象的时候,也就是单纯作为独立函数调用的时候,将对函数的this使用默认绑定:绑定到全局的window对象. 一个例子 function fire ...

  4. 【京东详情页】——原生js爬坑之放大镜

    一.引言 在商城的详情页中,放大镜的功能是很常见的.这里京东详情页就要做一个仿放大镜的效果,预览如下: 二.实现原理 实际上,放大镜的实现是单纯用几个div,鼠标移入其中一个小图div,触发事件显示另 ...

  5. http://codeforces.com/contest/838/problem/A

    A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. BZOJ-4915-简单的数字题

    Description 对任意的四个不同的正整数组成的集合A={a_1,a_2,a_3,a_4 },记S_A=a_1+a_2+a_3+a_4,设n_A是满足a_i+a_j (1 ≤i<j≤4)| ...

  7. CMake安装(源码方式)

    CMake主页是 https://cmake.org/download/ 一.不指定安装目录方式(不需要配置环境变量) 1.安装必备包(存在的包不用卸载,yum会自动更新) yum install - ...

  8. iOS 11 & iPhone X 适配资料集

    本文主要简单谈谈并收集一些关于 iOS 11 & iPhone X 的适配及设计指南. iPhone X 众所周知,iPhone X 屏幕与其他的 iPhone 设备均不同,苹果称 iPhon ...

  9. JAVA常用API(Date、DateFormat、Calendar、System、Math、基本数据类型包装类)

    注:本文所有内容均属个人见解,如有错误望各位大佬好心指点批评,不胜感激 本章重点单词: parse:解析 format:格式化 pattern:模式 amount:数量 filed :领域 1.Dat ...

  10. Linux系列教程(二)——Linux系统安装(手把手学安装centos6.8)

    在上一篇博客我们简单的介绍了Linux系统的起源,这篇博客我们将通过图示一步一步教大家如何安装Linux系统.注意这里我们选择安装的Linux系统是其一种发行版本 CentOS,这里给大家普及一个概念 ...