HDU 6165 FFF at Valentine

来源:互联网 发布:学生证办理 淘宝 编辑:程序博客网 时间:2024/06/02 05:27

FFF at Valentine

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


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
35 51 22 32 43 54 53 31 22 33 15 51 22 33 13 44 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
 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6170 6169 6168 6167 6166 

题意:

FFF团长把一对情侣给抓住了,他们被困在监狱中两个不同的地方,当监狱中存在一个地方可以只用一条路径就可达到其他所有地方的时候才可以获救,这n(2<=n<=1000)个地方只能从一个地方到达另一个地方不能返回(有向图),如果他们能获救输出"I love you my love and our love save us!",否则输出"Light my fire!"。(好像是这样的样子,英语渣加上题意晦涩难懂,感觉有错的样子?)

思路:

先对有向图缩点,之后建立新图,如果不存在入度为0的点他们不能获救,我们对这个图的根节点进行dfs看看是否能否用一条路径就可达到其他所有的点,如果可以则获救,不然就是无法获救。

实际上常数优秀n^2也是可做的。

示例程序

#include <bits/stdc++.h>using namespace std;struct jj{    int v,next;}w[16000],w1[500000];int h[1050],h1[1050],numw,numw1,dfn[1050],low[1050],id[1050],vis[1050],cnt,deep,de[1050],flag;int maxdeep;stack<int>s;void insert(int u,int v){    w[numw].v=v;    w[numw].next=h[u];    h[u]=numw++;}void insert1(int u,int v){    w1[numw1].v=v;    w1[numw1].next=h1[u];    h1[u]=numw1++;}void tanjan(int pos){    int i,v;    dfn[pos]=low[pos]=++deep;    vis[pos]=1;    s.push(pos);    for(i=h[pos];i!=-1;i=w[i].next)    {        v=w[i].v;        if(dfn[v]==0)        {            tanjan(v);            low[pos]=min(low[pos],low[v]);        }        else if(vis[v]==1)        {            low[pos]=min(dfn[v],low[pos]);        }    }    if(dfn[pos]==low[pos])    {        cnt++;        do        {            v=s.top();            s.pop();            vis[v]=0;            id[v]=cnt;        }while(v!=pos);    }}void dfs(int pos,int deep){    int i;    maxdeep=max(deep,maxdeep);    for(i=h1[pos];i!=-1;i=w1[i].next)    {        dfs(w1[i].v,deep+1);    }}int main(){    int t,n,m,i,i1,u,v,pos;    scanf("%d",&t);    while(t--)    {        scanf("%d %d",&n,&m);        while(s.empty()==0)        {            s.pop();        }        memset(h,-1,sizeof(h));        memset(h1,-1,sizeof(h1));        memset(dfn,0,sizeof(dfn));//时间戳数组        memset(vis,0,sizeof(vis));//是否在栈中标记数组        memset(de,0,sizeof(de));//入度数组        numw=0;        numw1=0;        deep=0;        flag=0;        cnt=0;        for(i=1;m>=i;i++)        {            scanf("%d %d",&u,&v);            insert(u,v);        }        for(i=1;n>=i;i++)//缩点        {            if(dfn[i]==0)            {                tanjan(i);            }        }        for(i=1;n>=i;i++)//建立新图        {            for(i1=h[i];i1!=-1;i1=w[i1].next)            {                if(id[i]!=id[w[i1].v])                {                    de[id[w[i1].v]]++;                    insert1(id[i],id[w[i1].v]);                }            }        }        int a=0;        for(i=1;cnt>=i;i++)        {            if(de[i]==0)            {                pos=i;                a++;            }        }        if(a==1)//如果只有一个入度为0的点(根)        {            maxdeep=0;            dfs(pos,1);        }        if(maxdeep==cnt&&a==1)        {            printf("I love you my love and our love save us!\n");        }        else        {            printf("Light my fire!\n");        }    }    return 0;}
原创粉丝点击