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

3
5 5
1 2
2 3
2 4
3 5
4 5
 
 
3 3
1 2
2 3
3 1
 
5 5
1 2
2 3
3 1
3 4
4 5

Sample Output

Light my fire!
I love you my love and our love save us!
I love you my love and our love save us!

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(强连通分量缩点+拓扑排序)的更多相关文章

  1. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  2. 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. ...

  3. 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 ...

  4. 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 ...

  5. HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. HDU 6170 FFF at Valentine(强联通缩点+拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165 题意:给你一个无环,无重边的有向图,问你任意两点,是否存在路径使得其中一点能到达另一点 解析:强 ...

  7. 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:  ...

  8. 【差分约束系统】【强连通分量缩点】【拓扑排序】【DAG最短路】CDOJ1638 红藕香残玉簟秋,轻解罗裳,独上兰舟。

    题意: 给定n个点(点权未知)和m条信息:u的权值>=v的权值+w 求点权的极小解和极大解(无解则输出-1) 极小解即每个点的点权可能的最小值 极大解即每个点的点权可能的最大值 题解: 差分约束 ...

  9. 【强连通分量缩点】【拓扑排序】【dp预处理】CDOJ1640 花自飘零水自流,一种相思,两处闲愁。

    题意: 在n个点m条边的有向图上,从1出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...

随机推荐

  1. 你是那种仅仅看《XXXXX从入门到精通》的程序猿吗?

    我一開始又要废话一番了. 实际上上了大学以后.你常常会在网上,在和别人的交流里,在老师的课堂上.反复听到一些书,比方黄仁宇的<万历十五年>.王小波"时代三部曲".村上春 ...

  2. jQuery 创建html

    jQuery 创建html

  3. Easypack容器系列之:Nexus 3:Docker私库

    Nexus作为私库管理最为流行的工具之中的一个,用于包的管理和Docker镜像管理的私库管理场景中非经常常使用.Easypack利用最新版本号的oss版Nexus作为基础镜像用于提供相似服务. 本文将 ...

  4. Photoshop之学习笔记(2) - 常用快捷键

    1.1024x768常用桌面分辨率2.点阵图(像素图).矢量图3.PPI 分辨率  DPI 打印输出的分辨率4.选框工具5.拾色器(默认H恢复色条 色相)6.Ctrl+D 取消选框工具7.Ctrl+S ...

  5. 【DB2】报错:-30090 25000 指定的操作对远程执行失败

    场景描述: 数据库:DB_1,DB_2 现在在DB_1中建立NICKNAME为CST_INFO_NICK,并且该别名指向数据库DB_2的CST_INFO表,在DB_1中建立存储过程,该存储过程需要  ...

  6. sql分组最大值相关

    房产表tf_estate_card,利润中心组profit_group_code,资产号main_assets_number,原值original_value 查出每个利润中心组的最大原值及其资产号 ...

  7. Python归并排序(递归实现)

    为什么归并排序如此有用?1. 快捷和稳定归并排序成为⼀一个非常棒的排序算法主要是因为它的快捷和稳定.它的复杂度即使在最差情况下都是O(n log n).而快速排序在最差情况下的复杂度是O(n^2),当 ...

  8. 李洪强经典面试题48-C语言

    可能碰到的iOS笔试面试题(4)--C语言   C语言,开发的基础功底,iOS很多高级应用都要和C语言打交道,所以,C语言在iOS开发中的重要性,你懂的.里面的一些问题可能并不是C语言问题,但是属于计 ...

  9. fstream之seekp/seekg/ios::ate/ios::app

    在程序开发中,IO处理无处不在,经常会在代码中遇到特殊的IO处理需求 1.描述 需求:如果文件不存在则创建,存在则打开,然后先读取文件的末行,然后在文件末尾写入. 代码: #include <i ...

  10. Linux下查看分区UUID

    有两种方法: 1.#:blkid 2.ls -l /dev/disk/by-uuid/