FFF at Valentine HDU

来源:互联网 发布:数码宝贝网络侦探友情 编辑:程序博客网 时间:2024/05/21 06:54

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!

这个题我以为缩点之后,只要判断每个点的出度入读情况就可以了,发现一直wa,后来想到是要拓扑排序

#include<cstdio>#include<vector>#include<cstring>#include<algorithm>#include<stack>#include<iostream>#include<queue>#define N 1005using namespace std;int n,m;int pre[N];vector<int>graph[N];vector<int>g[N];stack<int>st;bool inS[N];bool vis[N];int findd(int x)//并查集{    return pre[x]==x?x:pre[x]=findd(pre[x]);}int DFN[N],low[N];int belong[N];//说明这个点属于哪一个连通分量int in[N];int countt,index;void init(int n){    for(int i=1;i<=n;i++)        pre[i]=i;    memset(DFN,0,sizeof(DFN));    memset(in,0,sizeof(in));    countt=0;    index=1;    for(int i=1;i<=n;i++)        graph[i].clear();    while(!st.empty())        st.pop();}void targan(int x)//标准的tarjan{    DFN[x]=low[x]=index++;    st.push(x);    inS[x]=true;    for(int i=0;i<graph[x].size();i++)    {        int v=graph[x][i];        if(!DFN[v])        {            targan(v);            low[x]=min(low[x],low[v]);        }        else            if(inS[v])                low[x]=min(low[x],DFN[v]);    }    if(DFN[x]==low[x])    {        int u;        while(st.top()!=x)        {            u=st.top();            st.pop();            inS[u]=false;            belong[u]=countt;        }        u=st.top();        st.pop();        inS[u]=false;        belong[u]=countt++;    }}bool work(){    if(countt==1)        return true;    for(int i=0;i<countt;i++)        g[i].clear();    int v;    for(int i=1;i<=n;i++)//为锁店以后的图建新图,实际上就是属于一个拓扑排序    {        for(int j=0;j<graph[i].size();j++)        {            v=graph[i][j];            if(belong[i]!=belong[v])            {                in[belong[v]]++;                g[belong[i]].push_back(belong[v]);            }        }    }    queue<int> que;    memset(vis,false,sizeof(vis));    for(int i=0;i<countt;i++)        if(!in[i])        {            que.push(i);            vis[i]=true;        }    if(que.size()>1)//不允许有两个端点        return false;    while(!que.empty())    {        int v=que.front();        que.pop();        for(int i=0;i<g[v].size();i++)        {            in[g[v][i]]--;            if(!in[g[v][i]])            {                vis[g[v][i]]=true;                que.push(g[v][i]);            }        }        if(que.size()>1)//如果多出了两个点说明有分支,不满足单链            return false;    }    return true;}int main(){    int t;    int x,y;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        init(n);        while(m--)        {            scanf("%d%d",&x,&y);            graph[x].push_back(y);            int xx=findd(x);            int yy=findd(y);            if(xx!=yy)                pre[xx]=yy;//并查集是保证最后缩点的图的基图是连通的        }        int num=0;        for(int i=1;i<=n;i++)            if(pre[i]==i)            {                num++;                if(num>1)                    break;            }        if(num>1)        {            printf("Light my fire!\n");            //cout<<"haha"<<endl;        }        else        {            for(int i=1;i<=n;i++)                if(!DFN[i])                    targan(i);            if(work())                printf("I love you my love and our love save us!\n");            else                printf("Light my fire!\n");            /*cout<<countt<<endl;            for(int i=1;i<=n;i++)                cout<<i<<" "<<belong[i]<<" "<<in[belong[i]]<<" "<<out[belong[i]]<<endl;*/        }    }    return 0;}
原创粉丝点击