天天看點

Hdu 6165 FFF at Valentine【Tarjan強連通+暴搜】 FFF at Valentine

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

Hdu 6165 FFF at Valentine【Tarjan強連通+暴搜】 FFF at Valentine

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條有向邊的圖,問你這個圖能否任取兩個點都能夠使得其中一個點找到另外一個點。

思路:

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();
    }
}