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. android自定义动画

    前一篇说了实现过程,这次来写一个自己简单实现的3d动画 先来属性声明配置,方便使用xml 文件来定制动画 <!-- 有些类型其实是没必要的,只是实例代码,为了更具有代表性 --> < ...

  2. WPA/WPA2加密破解

    WPA/WPA2无线密码破解这里主要介绍两种方法:穷举PIN码.穷举密码 穷举PIN码(共11000种组合)破解速度快,需要一些条件(路由器支持WPS.QSS功能),路由器信号良好.无论密码多复杂,条 ...

  3. 【转】开源中国上看到的一个vim的自动配置的好东西,分享下

    https://www.oschina.net/p/onekey-to-vim-ide 变量有高亮,竖行上有直线定位,对python的支持效果更佳,从事C/C++开发的程序员使用也不错.

  4. Qt 打开文件的默认路径 QFileDialog::getOpenFileName()

    为了说明QFileDialog::getOpenFileName()函数的用法,还是先把函数签名放在这里:   QString QFileDialog::getOpenFileName (       ...

  5. Spark组件

    1,Application application(应用)其实就是用spark-submit提交的程序.比方说spark examples中的计算pi的SparkPi.一个application通常包 ...

  6. JSON依赖的选择

    json-lib 源码:https://github.com/aalmiray/Json-lib/ 最新版本:2.4 不再更新 <dependency> <groupId>ne ...

  7. vue-chat项目之重构与体验优化

    前言 vue-chat 也就是我的几个月之前写的一个基于vue的实时聊天项目,到目前为止已经快满400star了,注册量也已经超过了1700+,消息量达2000+,由于一直在实习,没有时间对它频繁地更 ...

  8. 《算法导论》学习总结 — XX.第23章 最小生成树

    一.什么叫最小生成树 一个无向连通图G=(V,E),最小生成树就是联结所有顶点的边的权值和最小时的子图T,此时T无回路且连接所有的顶点,所以它必须是棵树. 二.为什么要研究最小生成树问题 <算法 ...

  9. nginx配置文件作用介绍

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  10. linux视频之media媒体框架

    linux视频媒体(kernel层分析)主要包括三个文件: (/drivers/media/media-device.c ,  /drivers/media/media-devnode.c , /dr ...