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出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...
随机推荐
- poj 1426 Find The Multiple (bfs 搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18012 Accepted: 729 ...
- ajax乱码解决总结
第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8来send数据是不会 ...
- 使用LoadRunner监控Apache的步骤 (转)
一.Apache上的设置 打开<Apache Installation>/conf/httpd.conf,进行如下修改: 1. 设置允许查看Apache运行状态的主机 # # Allow ...
- MATLAB 的数据类型
在MATLAB中有15种基本的数据类型: 8种整型数据类型.单精度浮点型(float).双精度浮点型(double).逻辑型(logical).字符串型(char).单元数组型(cell).结构体类型 ...
- CoffeeScript 学习笔记
1.什么叫 CoffeeScript CoffeeScript 是一种新的编程语言,构建于 JavaScript 之上.CoffeeScript 提供了一种简洁的语法,对 Python 或 Ruby ...
- 自己学Docker:4.開始了解Docker的工作模式
上一章在学习中有2个疑问: 怎样保存我们在容器里的改动? 假设apt-get假设不能安装时,怎样在Docker中安装软件? 关于run创建的容器问题 对于第一个问题.原来每次运行(当非root用户时, ...
- kafka分布式搭建
kafka分布式搭建 (192.168.230.129)master (192.168.230.130)slave1 (192.168.230.131)salve2 在master.slave1.sl ...
- 很实用的JQuery代码片段(转)
1 元素屏幕居中 jQuery.fn.center = function () { this.css("position","absolute"); this. ...
- 英语每日一句: What’s your point? 你究竟想说什么?
今天我们要学习的一句话是:What's your point? 你究竟想说什么?这句话在日常交流中非经常见,当对方说了非常多东西你仍不明确他究竟是什么意思时.你就能够问What's your poin ...
- Atitit.收银系统pos 以及打印功能的行业标准
Atitit.收银系统pos 以及打印功能的行业标准 1. ESC指令序列 Escape指令序列不同于ESC/POS指令 1 2. 打印标准OPOS POSPrinter 与 CashDrawer 驱 ...