• codeforces982F


    The Meeting Place Cannot Be Changed

     CodeForces - 982F 

    Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping.

    Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements.

    Input

    The first line of the input contains two integers nn and mm (2n1052≤n≤105, 2m51052≤m≤5⋅105) — the number of intersections and the number of directed roads in Braginsk, respectively.

    Each of the next mm lines contains two integers uiui and vivi (1ui,vin1≤ui,vi≤n, uiviui≠vi) — the start and finish of the ii-th directed road. It is guaranteed that the robbers can move along the roads indefinitely.

    Output

    Print a single integer kk — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print 1−1.

    Examples

    Input
    5 6
    1 2
    2 3
    3 1
    3 4
    4 5
    5 3
    Output
    3
    Input
    3 3
    1 2
    2 3
    3 1
    Output
    1

    Note

    In the first example the robbers can move, for example, along the following routes: (1231)(1−2−3−1), (3453)(3−4−5−3), (1234531)(1−2−3−4−5−3−1). We can show that if Petr chooses the 33-rd intersection, he will eventually meet the robbers independently of their route.

    sol : 题意是说让你找到所有环的交点,但我想了很久感觉非常不可做。。。

    看了题解很想A掉出题人:先思考如何判断-1,如果有两个环的交集为0,就puts-1,高潮来了,如果超过0.9秒还没找到那个点,就puts-1

    在想如何找到那个点,暴力枚举那个点,强制不能经过,如果有环说明那个点不合法,其他不在环上的点也都不合法

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005,M=500005;
    int n,m;
    #define FIND_TIME clock()/(double)CLOCKS_PER_SEC
    namespace Pic
    {
        bool No[N];
        int Bo[N];
        int tot=0,Next[M],to[M],head[N];
        struct Point
        {
            int Out,Id;
        }P[N];
        inline bool cmp_Out(Point p,Point q)
        {
            return p.Out<q.Out;
        }
        inline void add(int x,int y)
        {
            Next[++tot]=head[x];
            to[tot]=y;
            head[x]=tot;
        }
        bool Flag;
        inline void dfs(int x)
        {
            Bo[x]=1;
            int i;
            for(i=head[x];i;i=Next[i])
            {
                if(!Bo[to[i]]) dfs(to[i]);
                else if(Bo[to[i]]==1)
                {
                    Flag=1;
                }
                if(Flag) return;
            }
            Bo[x]=2;
        }
        inline bool Check(int x)
        {
            int i,j;
            memset(Bo,0,sizeof Bo);
            Bo[x]=2;
            Flag=0;
            for(i=1;i<=n;i++) if(!Bo[i])
            {
                dfs(i);
                if(Flag)
                {
                    for(j=1;j<=n;j++) if(Bo[j]!=1) No[j]=1;
                    return false;
                }
            }
            return true;
        }
        inline void Solve()
        {
            int i;
            for(i=1;i<=m;i++)
            {
                if(i<=n) P[i].Id=i;
                int x,y; R(x); R(y);
                add(x,y); P[x].Out++;
            }
            sort(P+1,P+n+1,cmp_Out);
            for(i=1;i<=n;i++)
            {
                if(FIND_TIME>0.9) break;
                if((!No[P[i].Id])&&Check(P[i].Id))
                {
                    Wl(P[i].Id);
                    return;
                }
            }
            puts("-1");
        }
    }
    int main()
    {
        int i;
        R(n); R(m);
        Pic::Solve();
        return 0;
    }
    /*
    Input
    5 6
    1 2
    2 3
    3 1
    3 4
    4 5
    5 3
    Output
    3
    
    Input
    3 3
    1 2
    2 3
    3 1
    Output
    1
    */
    View Code
  • 相关阅读:
    js用button激活 Alert 元素关闭按钮的交互功能
    js中的$符号代表什么
    js中#代表什么
    amazeui学习笔记--js插件(UI增强)--警告框Alert
    amazeui学习笔记--css(常用组件16)--文章页Article
    amazeui学习笔记--css(常用组件15)--CSS动画Animation
    amazeui学习笔记--css(常用组件14)--缩略图Thumbnail
    amazeui学习笔记--css(常用组件13)--进度条Progress
    amazeui学习笔记--css(常用组件12)--面板Panel
    amazeui学习笔记--css(常用组件11)--分页Pagination
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10725706.html
Copyright © 2020-2023  润新知