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出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...
随机推荐
- Python 提取Twitter tweets中的元素(包括text, screen names, hashtags)
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-1 @author: guaguastd @name: ex ...
- Python——在Unicode和普通字符串之间转换
1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...
- ES标准中的相等比较算法 SameValue SameValueZero
1.相等比较算法 The Abstract Equality Comparison Algorithm (==) The Strict Equality Comparison Algorithm (= ...
- 由friend用法引出的声明与定义那些事儿
今天遇到了一个问题,大致描述一下就是有两个类A和B.我想达到如下效果:B是A的友元,同时A是B的类类型成员. 第一次尝试,在B.h中包含A.h,在A.h中包含B.h,在A类中声明friend clas ...
- Python 中实现装饰器时使用 @functools.wraps 的理由
Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过 ...
- 破解 zip 压缩包程序
目录 项目文件结构 代码实现过程 演示效果 代码地址如下:http://www.demodashi.com/demo/12021.html 项目文件结构 在当前目录有三个文件: 3-zipCrack. ...
- MongoDB笔记(二):MongoDB下Shell的基本操作
一.mongoDB与关系型数据库对比 对比项 mongoDB 关系型数据库(oracle.mysql) 表 集合List 二维表table 表的一行数 ...
- 美团HD(7)-添加取消搜索按钮
DJSelectCityViewController.m #pragma mark - UISearchBar 代理方法 /** SearchBar开始编辑 */ - (void)searchBarT ...
- linux/Documentation/kobject.txt
Everything you never wanted to know about kobjects, ksets, and ktypes Greg Kroah-Hartman <gregkh@ ...
- SpringBoot+MybatisPlus(多数据源和主从分离)
简介 dynamic-datasource-spring-boot-starter 基于 springBoot2.0. 它适用于读写分离,一主多从的环境. 主数据库使用 INSERT UPDATE D ...