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. verilog 代码分析与仿真

    verilog 代码分析与仿真 注意:使用vivado 自带的仿真工具, reg和wire等信号需要赋予初始值 边沿检测 module signal_test( input wire cmos_pcl ...

  2. 轻量应用服务器 访问jsp页面就直接下载的问题

    本地localhost 运行可以 用自己的ip不行.出现这个问题实质原因就是Tomcat服务器就没有起到作用,运行不了jsp文件.这个是核心.去排查错误!网上查了好几天了根本没有解决我的我的问题. 1 ...

  3. ionic3 热更新发布步骤记录

    1.安装基本框架npm install -g ionic@latest npm install -g cordova ionic 验证版本号 ionic –version cordova -versi ...

  4. aspose.cells 插入图片

    ,,"d:\\1.jpg"); Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[iIndex]; pic.Placeme ...

  5. 如何在idea里面新建一个maven项目,然后在这个maven项目里创建多个子模块

    如何在idea里面配置maven我这里就不多说了 先新建一个maven项目作为总的管理项目 不用勾选什么,直接下一步 这样子一个普通的maven项目就创建成功了. 因为这个项目是用来管理多个子模块的, ...

  6. [java,2017-05-04] 合并word文档

    import java.io.File; import com.aspose.words.Document; import com.aspose.words.ImportFormatMode; pub ...

  7. 面向的phthon2+3 的场景,Anaconda 安装+环境配置+管理

    standard procedure in pyCharm for creating environment when Anaconda installed Create a conda env vi ...

  8. kettle实现多表同步

    本样例实现源库的所有表到目标库的同步sqlserver=>mysql(目标表存在表结构则同步),总调度如下: 由于复制记录到结果保存了多个表名,存在多个值,在高级选择对每个输入行执行一次进行循环 ...

  9. 10 dict嵌套与升级

    dic = { 'name':['alex','wusir','taibai'], 'py9':{ ', 'learm_money':19800, 'addr':'CBD', }, 'age':21 ...

  10. js 模拟css3 动画1

    <html> <head> <title> javaScript缓动入门 </title> </head> <body> < ...