FFF at Valentine(强连通分量缩点+拓扑排序)
FFF at Valentine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 730 Accepted Submission(s): 359
Problem Description

At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, Boss of FFF Group caught both of them, and locked them into two separate cells of the jail randomly. But as the saying goes: There is always a way out , the lovers made a bet with LSH: if either of them can reach the cell of the other one, then LSH has to let them go.
The jail is formed of several cells and each cell has some special portals connect to a specific cell. One can be transported to the connected cell by the portal, but be transported back is impossible. There will not be a portal connecting a cell and itself, and since the cost of a portal is pretty expensive, LSH would not tolerate the fact that two portals connect exactly the same two cells.
As an enthusiastic person of the FFF group, YOU are quit curious about whether the lovers can survive or not. So you get a map of the jail and decide to figure it out.
Input
∙Input starts with an integer T (T≤120), denoting the number of test cases.
∙For each case,
First line is two number n and m, the total number of cells and portals in the jail.(2≤n≤1000,m≤6000)
Then next m lines each contains two integer u and v, which indicates a portal from u to v.
Output
If the couple can survive, print “I love you my love and our love save us!”
Otherwise, print “Light my fire!”
Sample Input
Sample Output
Source
2017 Multi-University Training Contest - Team 9
//题意:给出一个有向图,问是否任意两点都可以有,至少从其中一点到另一点可行的路径
//题解:首先想到的是好像是问是否是强连通图,然后看清题后发现并不是,求出连通分量缩点后变为有向无环图后,只需要确定,有唯一的拓扑排序的结果即可
# include <cstring>
# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N = ;
const int M = ;
/**************************/
struct Edge
{
int to;
int nex;
}edge[M*];
int n,m,realm,scc,Ddex;
int hlist[N],hlist2[N];
int dfn[N],low[N],belong[N];
bool instk[N];
stack<int> stk;
int indu[N]; void addedge(int u,int v)
{
edge[realm] = (Edge){v,hlist[u]};
hlist[u]=realm++;
}
void addedge2(int u,int v)
{
edge[realm] = (Edge){v,hlist2[u]};
hlist2[u]=realm++;
} void Init_tarjan()
{
Ddex=;scc=;
memset(dfn,,sizeof(dfn));
memset(instk,,sizeof(instk));
} void tarjan(int u)
{
dfn[u]=low[u]=++Ddex;
stk.push(u); instk[u]=;
for (int i=hlist[u];i!=-;i=edge[i].nex)
{
int v = edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if (instk[v])
low[u] = min(low[u],dfn[v]);
}
if (dfn[u]==low[u])
{
scc++;
while(){
int p = stk.top(); stk.pop();
instk[p]=;
belong[p]=scc;
if (u==p) break;
}
}
} void build()
{
memset(hlist2,-,sizeof(hlist2));
memset(indu,,sizeof(indu));
for (int i=;i<=n;i++)
{
for (int j=hlist[i];j!=-;j=edge[j].nex)
{
int x = belong[i];
int y = belong[edge[j].to];
if (x!=y)
{
addedge2(x,y);
indu[y]++;
}
}
}
} int topo()
{
queue<int> Q;
for (int i=;i<=scc;i++)
if (indu[i]==) Q.push(i);
if (Q.size()!=) return ;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=hlist2[u];i!=-;i=edge[i].nex)
{
int v = edge[i].to;
indu[v]--;
if (indu[v]==)
Q.push(v);
}
if (Q.size()>) return ;
}
return ;
} int main()
{
int T = scan();
while (T--)
{
memset(hlist,-,sizeof(hlist));
realm=;
scanf("%d%d",&n,&m);
for (int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
}
Init_tarjan();
for (int i=;i<=n;i++)
if (!dfn[i])
tarjan(i);
build();//建新图
if (topo())//拓扑
printf("I love you my love and our love save us!\n");
else
printf("Light my fire!\n");
}
return ;
}
FFF at Valentine(强连通分量缩点+拓扑排序)的更多相关文章
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
- poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6170 FFF at Valentine(强联通缩点+拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165 题意:给你一个无环,无重边的有向图,问你任意两点,是否存在路径使得其中一点能到达另一点 解析:强 ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- 【差分约束系统】【强连通分量缩点】【拓扑排序】【DAG最短路】CDOJ1638 红藕香残玉簟秋,轻解罗裳,独上兰舟。
题意: 给定n个点(点权未知)和m条信息:u的权值>=v的权值+w 求点权的极小解和极大解(无解则输出-1) 极小解即每个点的点权可能的最小值 极大解即每个点的点权可能的最大值 题解: 差分约束 ...
- 【强连通分量缩点】【拓扑排序】【dp预处理】CDOJ1640 花自飘零水自流,一种相思,两处闲愁。
题意: 在n个点m条边的有向图上,从1出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...
随机推荐
- SqlServer--百度百科
SQL是英文Structured Query Language的缩写,意思为结构化查询语言.SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为 ...
- scp命令使下用
先说下背景把,使用ubuntu,没有windows以下到xshell软件管理一下vps.正瞅着须要下载一个chrome(chrominum真的不好用).此刻大谷歌正墙外呢,无奈挂着ss firefox ...
- WP8滑动条(Slider)控件的使用
1. <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinit ...
- C语言学习笔记(五) 数组
数组 数组的出现就是为了解决大量同类型数据的存储和使用的问题: 数组的分类:一维数组.二维数组. 一维数组:为多个变量连续分配存储控件:所有的变量的数据类型必须相同:所有变量所占的字节大小必须相等: ...
- redis内存分析(转)
背景 线上经常遇到用户想知道自己 Redis 实例中数据的内存分布情况.为了不影响线上实例的使用,我们一般会采用 bgsave 生成 dump.rdb 文件,再结合 redis-rdb-tools 和 ...
- Atitit.软件开发的非功能性需求attilax 总结
Atitit.软件开发的非功能性需求attilax 总结 1. 运行环境约束:用户对软件系统运行环境的要求. 1 2. 兼容性 2 3. 7.6 数据库 database (imp by ati) ...
- random实现验证码功能
直接上代码: #-*- coding: utf-8 -*- #一个简单的验证码程序 import random #定义一个全局变量,初始值为空字符串 checkcode = '' for i in r ...
- 转载-好用的linux软件合集
音频 Airtime – Airtime 是一款用于调度和远程站点管理的开放广播软件 Ardour – 在 Linux 上录音,编辑,和混音 Audacious – 开源音频播放器,按你想要的方式 ...
- 分分钟学会一门语言之Python篇
转自:http://www.code123.cc/1049.html Python 是 90 年代初由 Guido Van Rossum 创立的.它是当前最流行的程序语言之一.它那纯净的语法令我一见倾 ...
- 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题
Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...