Cactus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2206    Accepted Submission(s): 1039

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
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3593 3600 3599 3598 3595 
 
题意:判断是否是仙人掌图。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
int next;
};
edge es[M];
int cut,head[N];
int scc_cut=,dfs_clock=;
int pre[N],low[N];
int vis[N],fa[N];
stack<int>s;
void init()
{
cut=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
cut++;
es[cut].from=u,es[cut].to=v;
es[cut].next=head[u];
head[u]=cut;
}
bool findfa(int u,int pa)
{
while(fa[u]!=pa)
{
if(++vis[u]>) return false;
u=fa[u];
}
return true;
}
bool dfs(int u)
{
pre[u]=low[u]=++dfs_clock;
s.push(u);
for(int i=head[u]; i!=-; i=es[i].next)
{ int v=es[i].to;
if(!pre[v])
{
fa[v]=u;
if(!dfs(v)) return false;
low[u]=min(low[u],low[v]);
}
else
{
low[u]=min(low[u],pre[v]);
if(!findfa(u,v)) return false;
}
}
if(pre[u]==low[u])
{
if(++scc_cut>) return false;
while(!s.empty())
{
int v=s.top();
s.pop();
if(v==u) break;
}
}
return true;
}
bool solve(int n)
{
scc_cut=dfs_clock=;
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
for(int i=; i<n; i++)
if(!pre[i]&&!dfs(i)) return false;
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n,u,v;
scanf("%d",&n);
while(scanf("%d%d",&u,&v)&&!(u==&&v==))
addedge(u,v);
if(solve(n)) puts("YES");
else puts("NO");
}
return ;
}

强联通分量 仙人掌图

代码:

HDU 3594.Cactus 仙人掌图的更多相关文章

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

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

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

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

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

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

  4. SHOI2008 cactus仙人掌图 和 UOJ87 mx的仙人掌

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

  5. hdu 3594 Cactus /uva 10510 仙人掌图判定

    仙人掌图(有向):同时满足:1强连通:2任何边不在俩个环中. 个人理解:其实就是环之间相连,两两只有一个公共点,(其实可以缩块),那个公共点是割点.HDU数据弱,网上很多错误代码和解法也可以过. 个人 ...

  6. HDU 3594 Cactus (强连通+仙人掌图)

    <题目链接> <转载于 >>> > 题目大意: 给你一个图,让你判断他是不是仙人掌图. 仙人掌图的条件是: 1.是强连通图. 2.每条边在仙人掌图中只属于一个 ...

  7. HDU 3594 Cactus 有向仙人掌图判定

    题意 给出一个有向图,并给出仙人掌图的定义 图本身是强连通的 每条边属于且只属于一个环 判断输入的图是否是强连通的. 分析 杭电OJ上的数据比较弱,网上一些有明显错误的代码也能AC. 本着求真务实的精 ...

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

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

  9. [BZOJ]1023 cactus仙人掌图(SHOI2008)

    NOIP后的第一次更新嗯. Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在 ...

随机推荐

  1. 物化视图SQL

    物化视图SQL,如果没有结果集,就证明数据库中不存在物化视图 select a.owner,'' column_name,a.table_name,b.segment_name,b.segment_t ...

  2. json 中关于json数组跟json对象的区别

    原文地址:http://blog.csdn.net/lafengwnagzi/article/details/52789171 JSON 是存储和交换文本信息的语法 JSON 文本格式在语法上与创建 ...

  3. [编程小技巧]Notepad++中如何实现文本对比功能?

    1.打开Notepad++插件中心   2.安装Compare   3.按提示重启Notepad++     4.点击Compare比较临近的两个文件       5. 取消比较     6 Comp ...

  4. python函数嵌套定义

    python的这个特性是很特别的,与C#和C++都不一样.请看下面的例子 def outFun(): def innerFun_0():#1.在内部定义一个函数 print("i am fi ...

  5. CentOS7 安装kafka集群

    1. 环境准备 JDK1.8 ZooKeeper集群(参见本人博文) Scala2.12(如果需要做scala开发的话,安装方法参见本人博文) 本次安装的kafka和zookeeper集群在同一套物理 ...

  6. Servlet 教程 各个知识点简单概括

    Servlet 教程 http://www.ziqiangxuetang.com/servlet/servlet-cookies-handling.html Servlet 教程 Servlet 教程 ...

  7. nginx+python+windows 开始_02

    接上文:http://www.cnblogs.com/tacyeh/p/4790112.html 一.改造helloWorld.py import web urls = ('/', 'Home', ' ...

  8. xmanagr 注册机执行ubuntu 桌面程序,ubuntu无需安装 桌面环境

    Xshell 5 注册码: 690313-111999-999313Xftp 5 注册码:101210-450789-147200 Xmanager 5 注册码:101210-450789-14720 ...

  9. 如何解决python 图表中文显示乱码问题(matlplotlib 包)

    目前搜到的是,下载一个字体到程序路径,设置成默认字体.  https://blog.csdn.net/irene_loong/article/details/68955485 #图表显示中文设置 im ...

  10. python tp_ready函数分析

    int PyType_Ready(PyTypeObject *type) { PyObject *dict, *bases; PyTypeObject *base; Py_ssize_t i, n; ...