【HDU-6165】解题报告(BFS,搜索优化)

原始题目

FFF at Valentine

  • Time Limit: 6000/3000 MS (Java/Others)
  • Memory Limit: 65536/65536 K (Java/Others)
  • Total Submission(s): 1703
  • Accepted Submission(s): 722

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

Recommend

liuyiding

题目大意

给定一个有向图,问任意两点之间是否连通

解题思路

暂时没有其他(正常)想法,SHL写了一个暴力BFS+优化,拿来学习一下。

#解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
vector<int> v[1010];
int Flag[1010][1010],vis[1010];
int ans;
void bfs(int st)
{
queue<int> q;
while(!q.empty())
q.pop();
q.push(st);
memset(vis,0,sizeof vis);
vis[st] = 1;
while(!q.empty())
{
int u = q.front();q.pop();
for(int i = 0; i < v[u].size(); i++)
{
if(vis[v[u][i]] == 0)
{
vis[v[u][i]] = 1;
q.push(v[u][i]);
if(Flag[st][v[u][i]] == 0)
{
Flag[st][v[u][i]] = Flag[v[u][i]][st] = 1;
ans++;
}
}
}
}
}

void Init()
{
for(int i=0;i<1010;i++) v[i].clear();
memset(Flag,0,sizeof Flag);
ans=0;
}

int main()
{
int T,n,m;cin>>T;
while(T--)
{
Init();
scanf("%d%d",&n,&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
for(int i = 1; i <= n; i++)
bfs(i);
if(ans == n*(n-1)/2)
printf("I love you my love and our love save us!\n");
else
printf("Light my fire!\n");
}
return 0;
}

收获与反思

待补充