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. String类的构造方法(2)

    写了常见的几个而已. 1:new 一个String类的时候系统会自动传一个空构造 public String(); 注意: 当对象初始化是 null时 和 对象是 "" 时,两者是 ...

  2. 指定路径下建立Access数据库并插入数据

    今天刚刚开通博客,想要把我这几天完成小任务的过程,记录下来.我从事软件开发的时间不到1年,写的不足之处,还请前辈们多多指教. 上周四也就是2016-04-14号上午,部门领导交给我一个小任务,概括来讲 ...

  3. 深入理解计算机系统chapter8

    进程轮流使用处理器 父进程调用fork来创建一个新的子进程 回收子进程 waitpid/wait 非本地跳转:

  4. eclipse导入源码

    1.window-----preferences 2.java---installed jres(点击不用展开)---选中使用的jar包-----editor 3.选中rt.jar ------sou ...

  5. Maven在Windows中的配置以及IDE中的项目创建

    Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...

  6. C#最基本的小说爬虫

    新手学习C#,自己折腾弄了个简单的小说爬虫,实现了把小说内容爬下来写入txt,还只能爬指定网站. 第一次搞爬虫,涉及到了网络协议,正则表达式,弄得手忙脚乱跑起来效率还差劲,慢慢改吧. 爬的目标:htt ...

  7. 对Item中定时器的理解

    一.Diamond介绍 Diamond主要提供持久配置的发布和订阅服务,最大特点是结构简单,稳定可靠. 主要的使用场景:TDDL使用Diamond动态切换数据库,动态扩容等:业务使用Diamond推送 ...

  8. 【原创】流程引擎的网关(遵循BPMN2.0)设计总结

    概述 BPMN 2.0是什么呢?业务流程模型注解(Business Process Modeling Notation - BPMN)是 业务流程模型的一种标准图形注解.这个标准 是由对象管理组(Ob ...

  9. 实例化vue之前赋值html元素导致事件失效

    先实例化Vue对象,再操作其他对象,Vue对象声明时会渲染html容器内的所有元素, 会导致元素事件失效或dom元素重新创建,所以涉及html元素的对象都要在实例化Vue之后执行. 下面是简要的例子, ...

  10. C#分布式事务解决方案-TransactionScope

    引用一下别人的导读:在实际开发工作中,执行一个事件,然后调用另一接口插入数据,如果处理逻辑出现异常,那么之前插入的数据将成为垃圾数据,我们所希望的是能够在整个这个方法定义为一个事务,Transacti ...