您的位置:首页 > 其它

HDU6165-FFF at Valentine

2017-08-22 21:35 363 查看


FFF at Valentine

                                                                  Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536
K (Java/Others)

                                                                                            Total Submission(s): 327    Accepted Submission(s): 155


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

 

题意:给定一个有向无环图,判断是否任意两点都能通过其中一点到达另外一点
解题思路:暴力对每个点bfs加点小优化即可(赛后发现不加优化1600ms也过了,优化600ms左右)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <unordered_map>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int n,m;
int s[1005],nt[6009],e[6009];
bool mp[1005][1005];

void bfs(int k)
{
for(int i=s[k];~i;i=nt[i])
{
int ee=e[i];
if(ee<k)
{
for(int i=1;i<=n;i++)
mp[k][i]|=mp[ee][i];
}
}
queue<int>q;
q.push(k);
while(!q.empty())
{
int pre=q.front();
q.pop();
for(int i=s[pre];~i;i=nt[i])
{
int ee=e[i];
if(!mp[k][ee])
{
mp[k][ee]=1;
q.push(ee);
}
}
}
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(s,-1,sizeof s);
int cnt=0;
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
nt[cnt]=s[u],s[u]=cnt,e[cnt++]=v;
}
memset(mp,false,sizeof mp);
for(int i=1;i<=n;i++) bfs(i);
int flag=1;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
if(!mp[i][j]&&!mp[j][i]) {flag=0;break;}
if(!flag) break;
}
if(flag) printf("I love you my love and our love save us!\n");
else printf("Light my fire!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: