hdu--6165--FFF at Valentine

FFF at Valentine

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 415    Accepted Submission(s): 206


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个点,m条边的有向图,没有重边和自环,问 图中任意两个点能否满足任意一方到达另外一方。

解题思路:

用一个二维数组dsi【x】【y】,表示两个点是否连通,如果连通,则dsi【x】【y】为true,否则为false。枚举题目中给出的所有点,进行深搜,搜出所有能够与他连通的点,把dis置为true,枚举完成后,检查任意两个点有没有互相不连通的,(dis【x】【y】==false&&dis【y】【x】==false)。如果有,答案就为:Light my fire!。如果没有这样的两个点,说明任意两个点都能连通,答案为:I love you my love and our love save us!。

代码:

 C++ Code 
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
#include<bits/stdc++.h>
using  namespace std;
const  int maxn =  1005;
vector< int>edge[maxn];
bool vis[maxn], dis[maxn][maxn];
int n, m, pos;
void dfs( int u)
{
    vis[u] =  true;
    dis[pos][u] =  true///搜到得点都能pos连通,
     for( int i =  0; i < edge[u].size(); i++)
    {
         int v = edge[u][i];
         if(vis[v]) continue;
        dfs(v);
    }
}
int main()
{
     int T;
    scanf( "%d", &T);
     while(T--)
    {
        scanf( "%d%d", &n, &m);
        memset(edge,  0sizeof(edge));
        memset(dis,  falsesizeof(dis));
         for( int i =  1; i <= m; i++)
        {
             int u, v;
            scanf( "%d%d", &u, &v);
            edge[u].push_back(v);
        }
         int flag =  1;
         for( int i =  1; i <= n; i++)
        {
            memset(vis,  falsesizeof(vis));
            pos = i;
            dfs(i);
        }
         for( int i =  1; i <= n; i++)
             for( int j = i +  1; j <= n; j++)
                 if(dis[i][j] ==  false && dis[j][i] ==  false)
                {
                    flag =  0;
                     break;
                }
         if(!flag) printf( "Light my fire!\n");
         else printf( "I love you my love and our love save us!\n");
    }
     return  0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值