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出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...
随机推荐
- Unity3D研究院之IOS本地消息通知LocalNotification的使用(六十七)
http://www.xuanyusong.com/archives/2632 现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器去推送 ...
- SoC嵌入式软件架构设计之六:API设计方法
在嵌入式系统中,驱动都是以API的方式提供给应用进行调用.这里介绍嵌入式系统的API设计和管理方法. 驱动在系统中会按模块进行分类,比如按键驱动.LCD驱动.文件系统.card驱动.I2C驱动等等:每 ...
- Android Studio+SVN配置生成apk文件
Android Studio 是谷歌推出一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发 ...
- wamp通过phpMyAdmin修改登录密码
初始安装wamp后,默认mysql是没有密码的,这个时候如果想要修改密码,可以按照以下步骤进行: 第一.打开phpMyAdmin,看到界面如图所示: 第二.通过导航找到“用户”,再找到“编辑权限”进行 ...
- Centos下安装JDK、Maven和Git
原文地址:https://github.com/eacdy/spring-cloud-book/blob/master/3%20%E4%BD%BF%E7%94%A8Docker%E6%9E%84%E5 ...
- margin: 0px auto; center 行类 块级
<html> <head> <title> biaoti </title> </head> <body style="bor ...
- [Android Bug] ListView中Header, Footer无法隐藏(gone)的问题
ListView中Header.Footer View应该是会应该遇到, 比如说,滚动到底部时,自动开始加载: 对于一些应用市场,会在Header中加上ViewFlipper做应用推荐(滚动的那种,好 ...
- http Referrer-Policy
Referrer-Policy: no-referrer Referrer-Policy: no-referrer-when-downgrade Referrer-Policy: origin Ref ...
- go实现定时功能两种方法
1:timer 学习自:https://studygolang.com/articles/2479 timer1 := time.NewTimer(time.Second * 2) //此处在等待ch ...
- C++语言基础(19)-模板的显式具体化
应用背景: 例如有下面的函数模板,它用来获取两个变量中较大的一个: template<class T> const T& Max(const T& a, const T&a ...