hdu6165 (缩点+拓扑排序)

FFF at Valentine

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


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!
 

Source
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N =1011;
struct edg
{
    int v,next;
} E[N*6];
int head[N],k;
void add(int u,int v)
{
    E[k].v=v;
    E[k].next=head[u];
    head[u]=k++;
}
int ans,dep;
int dfn[N],vis[N],low[N],in[N];
int belong[N];
stack<int>s;
vector<int>V[N];
void Tarjan(int x)
{
    dfn[x]=low[x]=++dep;
    vis[x]=1;
    s.push(x);
    for(int i=head[x]; i!=-1; i=E[i].next)
    {
        int v=E[i].v;
        if(!dfn[v])
        {
            Tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else
        {
            if(vis[v])
                low[x]=min(low[x],dfn[v]);
        }
    }
    if(low[x]==dfn[x])
    {
        ++ans;
        int f=-1;
        while(f!=x)
        {
            f=s.top();
            s.pop();
            vis[f]=0;
            belong[f]=ans;
        }
    }
    return ;
}
bool dfs(int x)//topo排序
{
    queue<int>q;
    q.push(x);
    while(!q.empty())
    {
        if(q.size()>1)
            return false;
        x=q.front();
        q.pop();
        for(int i=0; i<V[x].size(); i++)
        {
            int v=V[x][i];
            in[v]--;
            if(in[v]==0)
                q.push(v);
        }
    }
    return true;
}
void suodian(int n)
{
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    ans=dep=0;
    for(int i=1; i<=n; V[i].clear(),i++)
        if(!dfn[i])
            Tarjan(i);
    memset(in,0,sizeof(in));
    for(int i=1; i<=n; i++)
    {
        int u=belong[i];
        for(int j=head[i]; j!=-1; j=E[j].next)
        {
            int v=belong[E[j].v];
            if(u!=v)
            {
                V[u].push_back(v);
                in[v]++;
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        k=0;
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        suodian(n);
        int f=0,p;
        for(int i=1; i<=ans; i++)
        {
            if(in[i]==0)
            {
                f++;
                p=i;
            }
        }
        if(f==1)
        {
            if(dfs(p))
                printf("I love you my love and our love save us!\n");
            else printf("Light my fire!\n");
        }
        else printf("Light my fire!\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值