转载请注明出处,http://blog.csdn.net/Bule_Zst/article/details/77499994
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6165
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!
题目大意:给一个有向图,问任意两点之间是否可达(单向可达即可)
方法:
纯暴力dfs即可
首先对每个点深搜可达的点,然后存储到数组中,最后遍历数组,如果发现两点双向都不可达,则输出false
connect[i][j]为true表示i可到j
代码:
// @Team : nupt2017team12
// @Author : Zst
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define MOD 1000000007
#define CLR(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define pb push_back
#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )
const int N = 1000+7;
const int M = 6000+7;
bool connect[N][N];
int head[N];
int tot;
struct edge
{
int to;
int next;
}E[M];
bool vis[N];
void addEdge( int u, int v )
{
E[tot].to = v;
E[tot].next = head[u];
head[u] = tot++;
}
int n, m;
void dfs( int a )
{
vis[a] = true;
for( int i = head[a]; i != -1; i = E[i].next ) {
int v = E[i].to;
if( vis[v] )
continue;
dfs( v );
}
}
int main()
{
// freopen( "1005.txt", "r", stdin );
int w;
scanf( "%d", &w );
while( w-- ) {
tot = 0;
scanf( "%d%d", &n, &m );
CLR( connect, false );
CLR( head, -1 );
FOR( i, 1, m ) {
int a, b;
scanf( "%d%d", &a, &b );
addEdge( a, b );
}
FOR( i, 1, n ) {
CLR( vis, false );
vis[i] = true;
dfs( i );
FOR( j, 1, n ) {
if( vis[j] ) {
connect[i][j] = true;
}
}
}
bool fail = false;
FOR( i, 1, n ) {
FOR( j, i+1, n ) {
if( connect[i][j] == false && connect[j][i] == false ) {
fail = true;
break;
}
}
if( fail ) {
break;
}
}
if( fail ) {
printf( "Light my fire!\n");
} else {
printf( "I love you my love and our love save us!\n");
}
}
return 0;
}