HDU6165-FFF at Valentine

来源:互联网 发布:建筑工程预算软件 编辑:程序博客网 时间:2024/05/21 14:42

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~sccint 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;}/*35 51 22 32 43 54 53 31 22 33 15 51 22 33 13 44 5*/