FFF at Valentine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 104 Accepted Submission(s): 45
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.
∙ 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!”
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条有向边的图,问你这个图能否任取两个点都能够使得其中一个点找到另外一个点。
思路:
Tarjan缩点染色,得到DAG图,然后暴搜判定。DAG图的点数肯定小于等于原图的点数,不知道直接爆搜怎样,反正缩点染色之后暴力跑跑了200+ms.....
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
bool can[1050][1050];
vector<int>mp[1500];
vector<int>mp2[1500];
int stack[1500];
int color[1550];
int low[1550];
int vis[1550];
int dfn[1550];
int n,m;
int sig,cnt,tt;
void Tarjan(int u)
{
vis[u]=1;
dfn[u]=low[u]=cnt++;
stack[++tt]=u;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(vis[v]==0)Tarjan(v);
if(vis[v]==1)low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u])
{
sig++;
do
{
color[stack[tt]]=sig;
vis[stack[tt]]=-1;
}
while(stack[tt--]!=u);
}
}
void Dfs(int u,int root)
{
can[root][u]=true;
for(int i=0;i<mp2[u].size();i++)
{
int v=mp2[u][i];
if(vis[v]==0)
{
vis[v]=1;
Dfs(v,root);
}
}
}
void Slove()
{
cnt=1,sig=0,tt=-1;
memset(stack,0,sizeof(stack));
memset(color,0,sizeof(color));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
mp2[i].clear();
if(vis[i]==0)Tarjan(i);
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<mp[i].size();j++)
{
int u=color[i];
int v=color[mp[i][j]];
if(u!=v)
{
mp2[u].push_back(v);
}
}
}
for(int i=1;i<=sig;i++)
{
for(int j=1;j<=sig;j++)
{
can[i][j]=false;
}
}
for(int i=1;i<=sig;i++)
{
for(int j=1;j<=sig;j++)vis[j]=0;
Dfs(i,i);
}
int flag=0;
for(int i=1;i<=sig;i++)
{
for(int j=1;j<=sig;j++)
{
if(i==j)continue;
if(can[i][j]==true||can[j][i]==true)continue;
else flag=1;
}
}
if(flag==0)printf("I love you my love and our love save us!\n");
else printf("Light my fire!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
}
Slove();
}
}