codeforces915D. Almost Acyclic Graph(拓扑)

版权声明:Amove? https://blog.csdn.net/Amovement/article/details/88023459

                                       D. Almost Acyclic Graph

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.

Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).

Input

The first line contains two integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively.

Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ nu ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).

Output

If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.

Examples

input

Copy

3 4
1 2
2 3
3 2
3 1

output

Copy

YES

input

Copy

5 6
1 2
2 3
3 2
3 1
2 1
4 5

output

Copy

NO

Note

In the first example you can remove edge , and the graph becomes acyclic.

In the second example you have to remove at least two edges (for example,  and ) in order to make the graph acyclic.

一、原题地址

点我传送

 

二、大致题意

给出n个点,m条单向边,现在最多去掉其中一条边,询问图是否无环。输出YES或NO

三、大致思路

  最简单的思路是枚举每一条边去掉,然后跑拓扑判环,但是这样复杂度就达到了O(  m*(n+m)  ),数值达到了1e7的级别,挑战常数能力挤一挤说不定能过。

但是可以采取枚举点的做法,对于每个点我们考虑它度数 -1 的情况,相当于去掉了一条指向这个点的边,然后此时跑拓扑判环,这样就达到了我们的目的,因为实际上哪条边需要删除并不是我们想知道的,我们只需要知道是否可行。这样的复杂度就是O(  n*(n+m))了吧。

四、代码

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<stack>
#include<bitset>
using namespace std;
#define PI 3.14159265
const int inf=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ull;


int n,m;
int ind[505],Tind[505];
vector<int>e[505];

bool Topu()
{
    int now=0;
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        if(ind[i]==0)
        {
            q.push(i);now++;
        }
    }
    while(!q.empty())
    {
        int t=q.front();q.pop();
        for(int i=0;i<e[t].size();i++)
        {
            int to =e[t][i];
            ind[to]--;
            if(ind[to]==0)
            {
                q.push(to);now++;
            }
        }
    }
    return (now==n);
}

void initAndRead()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)Tind[i]=ind[i]=0;

    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        e[u].push_back(v);
        Tind[v]++;
    }
}

void work()
{
    memcpy(ind,Tind,sizeof(ind));
    if(Topu())
    {
        printf("Yes\n");
        exit(0);
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)ind[j]=Tind[j];
        if(ind[i]!=0)ind[i]--;
        if(Topu())
        {
            printf("YES\n");
            exit(0);
        }
    }
    printf("NO\n");
    return ;
}

int main()
{
    initAndRead();
    work();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Amovement/article/details/88023459