HDU6165-FFF at Valentine

FFF at Valentine

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

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
2017 Multi-University Training Contest - Team 9

题目大意:给出一个图,问任意两点之间是否存在一条路径可达。
解题思路:用tarjan求出强连通分量,然后缩点,拓扑排序,看每一层是否只存在一个入度为0的点。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
const int MAXN=2e5+5;
const int MAXM=5e5+5;
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],bel[MAXN];//bel数组的值是1~scc
int index,top;
int scc;//强连通分量的个数
bool instack[MAXN];
int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
//num数组不一定需要,结合实际情况
int n,m;

struct Edge
{
    int from,to,nxt;
}edge[MAXM];

void addedge(int u,int v)
{
    edge[tot].from=u;
    edge[tot].to=v;
    edge[tot].nxt=head[u];
    head[u]=tot++;
}

void tarjan(int u)
{
    int v;
    low[u]=dfn[u]=++index;
    sta[top++]=u;
    instack[u]=true;
    for(int i=head[u];i!=-1;i=edge[i].nxt)
    {
        v=edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[u]>low[v]) low[u]=low[v];
        }else if(instack[v]&&low[u]>dfn[v])
        low[u]=dfn[v];
    }
    if(low[u]==dfn[u])
    {
        scc++;
        do
        {
            v=sta[--top];
            instack[v]=false;
            bel[v]=scc;
            num[scc]++;
        }while(v!=u);
    }
}

void solve(int n)
{
    memset(dfn,0,sizeof(dfn));
    memset(instack,false,sizeof(instack));
    memset(num,0,sizeof(num));
    index=scc=top=0;
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i]) tarjan(i);
    }
}

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}

int in[MAXN];
vector<int> G[MAXN];
void suodian()
{
    memset(in,0,sizeof(in));
    for(int i=1;i<=scc;i++) G[i].clear();
    for(int i=0;i<m;i++)
    {
        int u=bel[edge[i].from];
        int v=bel[edge[i].to];
        if(u!=v)
        {
            G[u].push_back(v);
            in[v]++;
        }
    }
    int cnt=0,p;
    for(int i=1;i<=scc;i++)
    {
        if(in[i]==0) {cnt++;p=i;}
    }
    if(cnt>=2) printf("Light my fire!\n");
    else
    {
        queue<int> q;
        while(!q.empty()) q.pop();
        q.push(p);
        bool flag=true;
        while(!q.empty())
        {
            int fs=q.front();
            q.pop();
            int du=0;
            int sz=G[fs].size();
            for(int i=0;i<sz;i++)
            {
                int to=G[fs][i];
                in[to]--;
                if(in[to]==0)
                {
                    du++;
                    q.push(to);
                }
            }
            if(du>=2) {flag=false;break;}
        }
        if(flag) 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);
        init();
        int u,v;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        solve(n);
//        cout<<scc<<endl;
//        for(int i=1;i<=n;i++)
//        {
//            cout<<bel[i]<<endl;
//        }
//        cout<<num[1]<<" "<<num[2]<<endl;
        suodian();
    }
    return 0;
}
/*
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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值