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!
题意:给一个有向图,就是判断任意两点是否可以连通(a, b只要a可以到b 或者b到a就可以)
题解:BFS+搜素优化。其实在比赛时我就是XJB写了个BFS+优化暴力,没想到过了,Orz.
#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]])
{
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;
scanf("%d",&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!
");
else printf("Light my fire!
");
}
return 0;
}