HDU 6165 FFF at Valentine
题目大意:给出一个有向图,问你这个图中是否对于任意两点\(u,v\),都至少满足\(u\to v\)(\(u\)可到达\(v\),下同)或\(v\to u\)中的一个。
一看就是套路的图论题,我们先把边连起来。
考虑一个很基本的性质:在一个强连通分量的点两两可达
于是肯定先Tarjan缩一波点。然后我们得到了一个DAG
接下来就是考虑是否有两个点(当然是缩点之后的了)互不可达。
这个可以直接跑一边拓扑排序。然后看一下是否在某个时刻有两个点的入度为零即可。
CODE
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
const int N=1005;
struct edge
{
int to,next;
}e[N*6],ne[N*6];
int head[N],nhead[N],t,n,m,x,y,cnt,tot,sum,dfn[N],low[N],col[N],stack[N],ru[N],q[N],top;
bool vis[N];
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
}
inline void add(int x,int y)
{
e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt;
}
inline void nadd(int x,int y)
{
ne[++cnt].to=y; ne[cnt].next=nhead[x]; nhead[x]=cnt;
}
inline void clear(void)
{
memset(head,-1,sizeof(head)); memset(e,-1,sizeof(e));
memset(nhead,-1,sizeof(nhead)); memset(ne,-1,sizeof(ne));
memset(dfn,0,sizeof(dfn)); memset(vis,0,sizeof(vis));
memset(col,0,sizeof(col)); memset(ru,0,sizeof(ru));
cnt=tot=sum=top=0;
}
inline int min(int a,int b)
{
return a<b?a:b;
}
inline void Tarjan(int now)
{
dfn[now]=low[now]=++tot; vis[now]=1; stack[++top]=now;
for (register int i=head[now];i!=-1;i=e[i].next)
if (!dfn[e[i].to]) Tarjan(e[i].to),low[now]=min(low[now],low[e[i].to]);
else if (vis[e[i].to]) low[now]=min(low[now],dfn[e[i].to]);
if (dfn[now]==low[now])
{
vis[now]=0; col[now]=++sum;
while (now!=stack[top])
{
vis[stack[top]]=0; col[stack[top--]]=sum;
} --top;
}
}
inline bool top_sort(void)
{
register int i,H=0,T=0;
for (i=1;i<=sum;++i)
if (!ru[i]) q[++T]=i;
while (H<T)
{
if (T-H>1) return 0;
int now=q[++H];
for (i=nhead[now];i!=-1;i=ne[i].next)
if (!(--ru[ne[i].to])) q[++T]=ne[i].to;
}
return 1;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
register int i,j; read(t);
while (t--)
{
clear(); read(n); read(m);
for (i=1;i<=m;++i)
read(x),read(y),add(x,y);
for (i=1;i<=n;++i)
if (!dfn[i]) Tarjan(i);
for (cnt=0,i=1;i<=n;++i)
for (j=head[i];j!=-1;j=e[j].next)
if (col[i]!=col[e[j].to]) nadd(col[i],col[e[j].to]),++ru[col[e[j].to]];
if (top_sort()) puts("I love you my love and our love save us!"); else puts("Light my fire!");
}
return 0;
}
HDU 6165 FFF at Valentine的更多相关文章
- 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- HDU 6170 FFF at Valentine(强联通缩点+拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165 题意:给你一个无环,无重边的有向图,问你任意两点,是否存在路径使得其中一点能到达另一点 解析:强 ...
- FFF at Valentine(强连通分量缩点+拓扑排序)
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 【2017 Multi-University Training Contest - Team 9】FFF at Valentine
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6165 [题意] 一张有向图,n个点,m条边,保证没有重边和自环.询问任意两个点能否满足任何一方能够到达另 ...
- hdu 6165
虽然题解上说缩点然后判断入度就可以了,然后比赛的时候瞎暴力过了. #include <iostream> #include <cstring> #include <str ...
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 6693 Valentine's Day (概率)
2019 杭电多校 10 1003 题目链接:HDU 6693 比赛链接:2019 Multi-University Training Contest 10 Problem Description O ...
随机推荐
- LDA背景资料
[https://zhuanlan.zhihu.com/p/30226687] LDA模型的前世今生 在文本挖掘中,有一项重要的工作就是分析和挖掘出文本中隐含的结构信息,而不依赖任何提前标注的信息.L ...
- jquery判断checkBox的checked
jquery判断checked的三种方法:.attr('checked): //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false.prop('c ...
- virtualenv 的使用
首先,我们用pip安装virtualenv: 一.使用与启动: $ pip3 install virtualenv 然后,假定我们要开发一个新的项目,需要一套独立的Python运行环境,可以这么做: ...
- Spring MVC 的工作原理
引自:https://www.cnblogs.com/xiaoxi/p/6164383.html SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器Dispat ...
- SQL取最大值编码(自动编码)
SQL取最大值编码(自动编码) 用途 : 使用SQL语法做出自动编码效果,例如将单号自动+1后,产生该笔单号 Table说明 SQL语法 SELECT 'A'+REPLICATE('0',7-len( ...
- PHP APP端支付宝支付
应业务需求,做了支付宝支付和微信支付,今天分享一下手机端app支付宝支付对接流程,实际开发过程是前后端分离,前端调用后端API接口,实现功能返回数据,我所用的跨挤啊为TP5,大致可以分为四步: 1.在 ...
- Windows 下配置 Apache 支持 https
1.打开cmd ,输入 F: // 切换到Apache安装路径,我的Apache安装目录在 F盘 2.cd F:\Apache\bin 3.set "openssl_conf = F:\A ...
- java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.demoDao.getXXX;
java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...
- 17秋 软件工程 第六次作业 Beta冲刺 Scrum5
17秋 软件工程 第六次作业 Beta冲刺 Scrum5 各个成员冲刺期间完成的任务 世强:完成APP端相册.部员管理.手势签到模块: 陈翔:完成Scrum博客.总结博客,完成超级管理员前后端对接: ...
- c++引用lib和dll的方法总结
C++ 调用.lib的方法: 一: 隐式的加载时链接,有三种方法 1 LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中"Add F ...