题目链接

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!

题意:

给定n个点,这n个点之间连有m条单向的路径,要求判断整个图中是否满足任意两点之间有路径可以到达。

(如果求点a与点b之间,只要a能够到达b,或则b能够到达a即可)

分析:

最终的意思还是判断任意两点之间是否由路径到达,最开始的时候用dij来写,调用n次,可能中间的哪一个细节没有考虑到,反正就是w。

然后改变了一下思路,从一个点出发,寻找所有的这个点能够达到的点,刚开始的时候肯定是只考虑它本身能够达到的,但这样是不够的,我们还要考虑所有的通过某些点可以到达的点。将所有的点都要走一遍。

代码:

#include<bits/stdc++.h>
using namespace std; const int N = 1000 + 5;
int dp[N][N];//可达标记
bool vis[N];
vector<int> son[N]; void dfs(int u,int fa)
{
dp[fa][u]=vis[u]=1;//记为可达
for(int i=0; i<son[u].size(); i++)///标记了从节点fa可以到达的使所有的节点,不仅仅是直接相连的,通过其他的点松弛过的也要标记
{
int v=son[u][i];
if(!vis[v])
{
dfs(v,fa);
}
}
} int main()
{
int t,m,n,u,v;
bool flag;
scanf("%d",&t);
while(t--)
{
flag=1;
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=0; i<=n; i++)
{
son[i].clear();
}
for(int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
dp[u][v]=1;///u到v有路径
son[u].push_back(v);///记录这个点可以达到的所有的点
}
for(int i=1; i<=n; i++)///从每一个点出发开始标记路径
{
memset(vis,0,sizeof(vis));
vis[i]=1;
dfs(i,i);
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
{
if(dp[i][j]||dp[j][i]) ///只要两点之间有一方能够到达另一方即可
continue;
else
{
flag=0;///都不能到达的话,做一个标记
break;
}
}
if(flag==0)
break;
}
if(flag)
{
puts("I love you my love and our love save us!");
}
else
{
puts("Light my fire!");
}
}
return 0;
}

2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  2. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  3. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  4. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  5. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  6. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  7. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  8. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  9. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

随机推荐

  1. TP 等框架在配置虚拟主机伪静态注意事项

    在配置虚拟主机的伪静态 .htaccess 文件时需要注意 ? 符号的添加与否处理 例如TP 框架来讲 无 ? 符号,在有的虚拟主机中必须这样配置,否则无法解析(试了多个公司的虚拟主机只要加了?符号就 ...

  2. SQL DATACOMPARE 实现两个数据库的同步处理.

    1. SQL DATACOMPARE 实现 两个数据库的同步 安装破解. 然后进行对比 然后进行 deploy 生成sql 等. 貌似很好用 但是前提是数据库是可用的... 这里面能够看到 生成的脚本 ...

  3. 第211天:git和github的区别和使用详解

    一.git 1.什么是git 它是一个源代码管理工具,在一个项目中,凡是由开发人员编写的都算是源代码,源代码有必要管理起来,让源代码可以被追溯,主要记录每次变更了什么,谁主导这次变化.人为的维护比较麻 ...

  4. Scalable IO in Java【java高效IO】

    第一次翻译,如有错误,请指正 1.Outline 大纲Scalable network services  高效网络服务 Event-driven processing  事件驱动处理 Reactor ...

  5. 解决MySQL Slave 触发 oom-killer

    最近经常有收到MySQL实例类似内存不足的报警信息,登陆到服务器上一看发现MySQL 吃掉了99%的内存,God ! 有时候没有及时处理,内核就会自己帮我们重启下MySQL,然后我们就可以看到 dme ...

  6. CentOS 7 上安装(LAMP)服务 Linux,Apache,MySQL,PHP

    介绍 LAMP 是现在非常流行的 WEB 环境, 是 Linux,Apache,MySQL,PHP 的缩写.数据存储在 MySQL 中,动态内容由 PHP 处理. 在本指南中,我们将演示如何在 Cen ...

  7. DAY2-Flask项目

    回顾: 1.安装pipenv虚拟运行环境,隔离项目 (启动:pipenv shell) 2.安装flask(pipenv install shell),查看项目依赖(pipenv graph) 3.查 ...

  8. js模块化的总结

    从前端打包的历史谈起 在很长的一段前端历史里,是不存在打包这个说法的.那个时候页面基本是纯静态的或者服务端输出的, 没有 AJAX,也没有 jQuery.Google 推出 Gmail 的时候(200 ...

  9. [BZOJ2502]清理雪道 有上下界网络流(最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  10. 【刷题】洛谷 P4319 变化的道路

    题目描述 小 w 和小 c 在 H 国,近年来,随着 H 国的发展,H 国的道路也在不断变化着 根据 H 国的道路法,H 国道路都有一个值 \(w\) ,表示如果小 w 和小 c 通过这条道路,那么他 ...