2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- 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 ...
- 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− ...
随机推荐
- 电脑CPU开机上电后的第一条指令
结合上面的文章,CPU上电后第一条指令是通过CS:IP来指定的,CPU厂家会给其初始值,对于386处理器来说,CPU第一条指令地址是 0xFFFFFFF0 这里会有一个问题,CPU怎么能一上来就去0x ...
- 固态盘经常性蓝屏处理方法(WIN7/8)
型号:intel 520S 大小;120G 我的是笔记本,这段时间辞职,有了时间折腾自己的电脑系统了,想装WIN8来着,PE下直接把固态盘的分区都干掉了,分了C,D(40G,剩下的空间),安装阶段完美 ...
- VIM 命令收藏
1.vim#在命令行中输入vim,进入vim编辑器2.i#按一下i键,下端显示 --INSERT--#插入命令,在vim中可能任意字符都有作用3.Esc#退出i(插入)命令进行其它命令使用4.:r f ...
- 再看select语句
select语句是整个sql中输出的最后一条语句,这条语句是在最后输出的结果集合上做计算, 这些计算都包括啥东西呢?对每个结果集合做插值计算,在做完group by和where子句之后,那么就是一个一 ...
- Memcache CAS协议介绍及使用
1.什么是CAS 所谓CAS,check and set,在写操作时,先检查是否被别的线程修改过. 基本原理非常简单,一言以蔽之,就是"版本号".每个存储的数据对象,多有一个版本号 ...
- Flyway学习笔记
Flyway做为database migration开源工具,功能上像是git.svn这种代码版本控制.google搜索database migration,或者针对性更强些搜索database mi ...
- 安装pycharm软件后,打开robot framework怎么默认用pycharm打开
1.打开ride.py的属性,修改打开方式
- 【刷题】BZOJ 1901 Zju2112 Dynamic Rankings
Description 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[j]中第k小的数是 ...
- PHP 字符串数组按照拼音排序的问题
拼音排序的规则: 字符串包括特殊字符.数字.英文字符.中文字符等等,排序结果要求,特殊字符排在第一梯队,将其按照首个字符ascii码表进行排序,数字字符排在第二梯队,将首个字符数字按照数字大小排序,英 ...
- Java应用中使用ShutdownHook友好地清理现场
在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码.Java中得ShutdownHook提供了比较好的方案. JDK在1.3之后提 ...