HDU 3594.Cactus 仙人掌图
Cactus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2206 Accepted Submission(s): 1039
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).
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.
#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 仙人掌图的更多相关文章
- 【BZOJ】【1023】【SHOI2008】cactus仙人掌图
DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...
- bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列
1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1141 Solved: 435[Submit][ ...
- bzoj千题计划113:bzoj1023: [SHOI2008]cactus仙人掌图
http://www.lydsy.com/JudgeOnline/problem.php?id=1023 dp[x] 表示以x为端点的最长链 子节点与x不在同一个环上,那就是两条最长半链长度 子节点与 ...
- SHOI2008 cactus仙人掌图 和 UOJ87 mx的仙人掌
cactus仙人掌图 题目描述 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一 ...
- hdu 3594 Cactus /uva 10510 仙人掌图判定
仙人掌图(有向):同时满足:1强连通:2任何边不在俩个环中. 个人理解:其实就是环之间相连,两两只有一个公共点,(其实可以缩块),那个公共点是割点.HDU数据弱,网上很多错误代码和解法也可以过. 个人 ...
- HDU 3594 Cactus (强连通+仙人掌图)
<题目链接> <转载于 >>> > 题目大意: 给你一个图,让你判断他是不是仙人掌图. 仙人掌图的条件是: 1.是强连通图. 2.每条边在仙人掌图中只属于一个 ...
- HDU 3594 Cactus 有向仙人掌图判定
题意 给出一个有向图,并给出仙人掌图的定义 图本身是强连通的 每条边属于且只属于一个环 判断输入的图是否是强连通的. 分析 杭电OJ上的数据比较弱,网上一些有明显错误的代码也能AC. 本着求真务实的精 ...
- 1023: [SHOI2008]cactus仙人掌图 - BZOJ
Description如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的回路 ...
- [BZOJ]1023 cactus仙人掌图(SHOI2008)
NOIP后的第一次更新嗯. Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在 ...
随机推荐
- Python 语言之 map/reduce
1.相关文献 大名鼎鼎的Google论文<MapReduce: Simplified Data Processing on Large Clusters> 对应的中文翻译<MapRe ...
- sql server 2016 附加 其它目录的数据库
如果数据库不在默认目录,那么需要将 mdf所在目录或者 mdf文件 添加 用户 [NT SERVICE\MSSQLSERVER]的创建权限,否则会提示没有权限, 具体详见: https://docs. ...
- <亲测>centos7通过yum安装JDK1.8(实际上是openjdk)
centos7通过yum安装JDK1.8 安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...
- 测试WCF遇到的一些问题
win7+iis7 1.localhost访问bad request错误. 主机地址不要指定为127.0.0.1.设置为”全部未分配“. 2.错误 500.19(由于权限不足而无法读取配置文件)的问题 ...
- Scrapy实战篇(一)之爬取链家网成交房源数据(上)
今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...
- nginx配置分发Tomcat服务,负载均衡
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中瓦片资源越来越多,如果提高瓦片的访问效率是一个需要解决的 ...
- sql使用实例
将另一表中的合计值保存到指定字段,并将空值赋0 update ShopInfo set JLRunningWater =(select COALESCE(sum(v.TotalMoney),0) as ...
- wampserver_x86_3.0.6 允许外网访问配置教程
1.打开wamp目录下的apache配置文件中的httpd.conf 用可以看行数的编辑器打开 大概244行: 改为 <Directory /> AllowOverride none Re ...
- sqlserver数据库镜像运行模式
运行模式: 从大层面来说,SQL Server镜像只有两种模式:高安全模式和高性能模式.两种模式的主要区别在于在事务提交后的操作.可以从图1-1中查看运行模式. 在高性能模式下,主体服务器不需要等待镜 ...
- mongo官方企业版安装及数据库授权使用
通过安装.deb包的方式,系统是Ubuntu 16.04 1. Import the public key used by the package management system.(导入包管理系统 ...