Cactus
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 |
Sample Output
YES |
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的更多相关文章
- 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 ...
- 仙人掌(cactus)
仙人掌(cactus) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 在冲刺清华集训(THUSC) !于是它开始研究仙人掌,它想来和你一起分享它最近研究的 ...
- 【BZOJ】【1023】【SHOI2008】cactus仙人掌图
DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...
- 1023: [SHOI2008]cactus仙人掌图 - BZOJ
Description如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的回路 ...
- Cactus入门
这是一个WebProject,有关Cactus用法详见本文测试用例 首先是web.xml <?xml version="1.0" encoding="UTF-8&q ...
- Cactus借助Jetty测试Servlet
这是一个WebProject,但不需要web.xml,因为用不到它 首先是待测试的LoginServlet.java package com.jadyer.servlet; import java.i ...
- bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列
1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1141 Solved: 435[Submit][ ...
- Codeforces 231E - Cactus
231E - Cactus 给一个10^5个点的无向图,每个点最多属于一个环,规定两点之间的简单路:从起点到终点,经过的边不重复 给10^5个询问,每个询问两个点,问这两个点之间有多少条简单路. 挺综 ...
- bzoj千题计划113:bzoj1023: [SHOI2008]cactus仙人掌图
http://www.lydsy.com/JudgeOnline/problem.php?id=1023 dp[x] 表示以x为端点的最长链 子节点与x不在同一个环上,那就是两条最长半链长度 子节点与 ...
随机推荐
- RMQ问题第一弹
今天,我给大家分享一下我在学习 RMQ 问题过程中对该问题的理解. RMQ (Range Minimum/Maximum Query ):中文名为"区间最值查询".RMQ 问题指的 ...
- css预处理语言--让你的css编写更加简单方便
CSS预处理语言之一-------LESS Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 可以运行在 Nod ...
- ui-router
学习历程:1 ng-router --> 2 location --> 3 $location --> 4 promise --> 5 html5 history -- ...
- ng-model值字符串转数值型(convertToNumber directive)
<select ng-model="model.id" convert-to-number> <option value="0">Zer ...
- angular 学习笔记
每天进步一点点,学习笔记 笔记来自 angular权威指南 如果想要屏蔽浏览器对表单的默认验证行为,可以在表单元素上添加 novalidate 标记. 而按钮标签则完全忽略 hr e f 属性,并不 ...
- 深入理解计算机系统chapter8
进程轮流使用处理器 父进程调用fork来创建一个新的子进程 回收子进程 waitpid/wait 非本地跳转:
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- 【小程序】微信小程序实现各种特效实例
写在前面 最近在负责一个微信小程序的前端以及前后端接口的对接的项目,整体上所有页面的布局我都已经搭建完成,里面有一些常用的特效,总结一下,希望对大家和我都能有所帮助 实例1:滚动tab选项卡 先看一下 ...
- [VirtualBox] 1、NAT模式下端口映射
1.VirtualBox中有4中网络连接方式 VirtualBox中有4中网络连接方式:NAT.Bridged Adapter.Internal.Host-only Adapter,VMWare中有三 ...
- ArcGIS RunTime SDK for Android之Features and graphics
今天是我开通博客园的第一天,希望以后可以多在博客园上分享自己的学习心得,记录自己的学习历程.最近在学习ArcGIS RunTime SDK for Android,所以第一篇随笔就从这里来吧.官网的教 ...