HDU 6165 FFF at Valentine -单向连通图

FFF at Valentine

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


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
i
Input starts with an integer T (T≤120), denotingtenumber 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!
 

题意:
n点m条边的有向图,是否任意两点间都存在一条路径使得v->u或者u->v可通
解题思路
首先做出连通图的判断,然后在连通图上按照连通分支缩图,然后拓扑排序,当入度为0的点>1说明不行

图的连通分支数模板详细注释:http://blog.csdn.net/ly59782/article/details/65442078


#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

const int maxn = 1000+10;
int n,m;
vector<int> G[maxn];
vector<int> e[maxn];
int pre[maxn];///第一次进入dfs层数
int lowlink[maxn];///能追溯到的dfs层最小的跟祖先的pre
int sccno[maxn];///当前点属于第几个连通分量
int indegree[maxn];///入度
int dfsLock;///dfs层数
int sccCnt;///连通分两个数

stack<int> s;

///连通图的个数
void dfs(int u){
    pre[u] = lowlink[u] = ++dfsLock;
    s.push(u);
    for(int i = 0;i<G[u].size();++i){
        int v = G[u][i];
        if(!pre[v]){
            dfs(v);
            lowlink[u] = min(lowlink[u],lowlink[v]);
        }
        else if(!sccno[v]){
            lowlink[u] = min(lowlink[u],pre[v]);
        }
    }
    if(lowlink[u] == pre[u]){
        sccCnt ++;
        while(1){
            int x = s.top();s.pop();
            sccno[x] = sccCnt;
            if(x==u) break;
        }
    }
}

int topsort(){
    int cnt = 0;
    queue<int> q;
    ///注意新图由于从连通分支而来,所以编号从1开始到sccCnt
    for(int i = 1;i<=sccCnt;++i){
        if(!indegree[i]){
            cnt++;
            q.push(i);
        }
    }
    if(cnt>1) return cnt;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        cnt = 0;///注意之前的点就不算了
        for(int i = 0;i<e[u].size();++i){
            int v = e[u][i];
            indegree[v]--;
            if(!indegree[v]){
                cnt++;
                q.push(v);
            }
            if(cnt>1) return cnt;
        }
    }
    return cnt;
}

int main()
{
    int t;
    scanf("%d",&t);
    while (t > 0){
        t--;
        scanf("%d%d",&n,&m);

        ///初始化
        for(int i = 0;i<n;++i){
            G[i].clear();
            e[i].clear();
        }
        while(!s.empty()) s.pop();
        memset(pre,0,sizeof(pre));
        memset(lowlink,0,sizeof(lowlink));
        memset(sccno,0,sizeof(sccno));
        memset(indegree,0,sizeof(indegree));
        dfsLock = sccCnt = 0;

        int u,v;
        for(int i = 0;i<m;++i){
            scanf("%d%d",&u,&v);
            G[u-1].push_back(v-1);
        }
        for(int i = 0;i<n;++i){
            if(!pre[i]) dfs(i);
        }
        ///缩图
        for(int i = 0;i<n;++i){
            for(int j = 0;j<G[i].size();++j){
                int g1 = sccno[i];///当前点属于第几个连通分支
                int g2 = sccno[G[i][j]];///与他相连的点属于第几个连通分支
                if(g1!=g2){
                    indegree[g2]++;///把每一个连通分支当成一个点,重新建图
                    e[g1].push_back(g2);
                }
            }
        }
        int num = topsort();
        if(num > 1) puts("Light my fire!");
        else puts("I love you my love and our love save us!");


    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值