• 2017ACM暑期多校联合训练


    题目链接

    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!

    题意:

    给定n个点,这n个点之间连有m条单向的路径,要求判断整个图中是否满足任意两点之间有路径可以到达。

    (如果求点a与点b之间,只要a能够到达b,或则b能够到达a即可)

    分析:

    最终的意思还是判断任意两点之间是否由路径到达,最开始的时候用dij来写,调用n次,可能中间的哪一个细节没有考虑到,反正就是w。

    然后改变了一下思路,从一个点出发,寻找所有的这个点能够达到的点,刚开始的时候肯定是只考虑它本身能够达到的,但这样是不够的,我们还要考虑所有的通过某些点可以到达的点。将所有的点都要走一遍。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N = 1000 + 5;
    int dp[N][N];//可达标记
    bool vis[N];
    vector<int> son[N];
    
    void dfs(int u,int fa)
    {
        dp[fa][u]=vis[u]=1;//记为可达
        for(int i=0; i<son[u].size(); i++)///标记了从节点fa可以到达的使所有的节点,不仅仅是直接相连的,通过其他的点松弛过的也要标记
        {
            int v=son[u][i];
            if(!vis[v])
            {
                dfs(v,fa);
            }
        }
    }
    
    int main()
    {
        int t,m,n,u,v;
        bool flag;
        scanf("%d",&t);
        while(t--)
        {
            flag=1;
            memset(dp,0,sizeof(dp));
            scanf("%d%d",&n,&m);
            for(int i=0; i<=n; i++)
            {
                son[i].clear();
            }
            for(int i=1; i<=m; i++)
            {
                scanf("%d%d",&u,&v);
                dp[u][v]=1;///u到v有路径
                son[u].push_back(v);///记录这个点可以达到的所有的点
            }
            for(int i=1; i<=n; i++)///从每一个点出发开始标记路径
            {
                memset(vis,0,sizeof(vis));
                vis[i]=1;
                dfs(i,i);
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<i; j++)
                {
                    if(dp[i][j]||dp[j][i]) ///只要两点之间有一方能够到达另一方即可
                        continue;
                    else
                    {
                        flag=0;///都不能到达的话,做一个标记
                        break;
                    }
                }
                if(flag==0)
                    break;
            }
            if(flag)
            {
                puts("I love you my love and our love save us!");
            }
            else
            {
                puts("Light my fire!");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    锚点
    DOM
    background
    Vue前端路由
    JavaScript常用方法
    算法——dfs介绍
    时间复杂度和空间复杂度
    CSS定位(position)
    CSS三栏布局
    前端笔试高频知识点汇总
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7417201.html
Copyright © 2020-2023  润新知