1. 程式人生 > >Almost Acyclic Graph CodeForces

Almost Acyclic Graph CodeForces

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 ≤ n, u ≠ 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

3 4
1 2
2 3
3 2
3 1

Output

YES

Input

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

Output

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.

AC程式碼

//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (1+r)/2
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 142857
const int maxn = 505;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int n, m, s, t;
int len;
struct edge
{
    int u, v;
    int next;
};
edge G[100100];
int h[maxn];
int vis[maxn];
vector<int> vec;
bool flag;
int star;
void init()
{
    for(int i = 0; i < maxn; i++){h[i] = -1;}
}
bool dfs(int x, int v)
{
    if(vis[v] == 2)
    {
        star = v;
        return true;
    }
    vis[v] = 2;
    for(int i = h[v]; i != -1; i = G[i].next)
    {
        if(i != x)
        {
            int to = G[i].v;
            if(vis[to] == 0)
            {
                if(dfs(x, to)) return true;
            }
            else if(vis[to] == 2)
            {
                star = to;
                return true;
            }
        }
    }
    vis[v] = 1;
    return false;
}
bool dfsa(int x, int v)
{
    if(vis[v] == 2)
        return true;
    vis[v] = 2;
    for(int i = h[v]; i != -1; i = G[i].next)
    {
        if(i != x)
        {
            int to = G[i].v;
            if(vis[to] == 0)
            {
                vec.pb(i);
                if(dfsa(x, to)) return true;
                vec.pop_back();
            }
            else if(vis[to] == 2)
            {
                vec.pb(i);
                return true;
            }

        }
    }
    vis[v] = 1;
    return false;
}
int main()
{
	//freopen("out.txt", "w", stdout);
	while(~sdd(n, m) && (n||m))
    {
        init();
        len = 0;
        int ta, tb;
        for(int i = 0; i < m; i++)
        {
            sdd(ta, tb);
            G[len].u = ta;
            G[len].v = tb;
            G[len].next = h[ta];
            h[ta] = len++;
        }
        flag = false;
        ms(vis, 0);
        for(int i = 1; i <= n ; i++)
        {
            if(dfs(-1, i))
            {
                flag = true;
                break;
            }
        }
        if(!flag)
        {
            puts("YES");
            return 0;
        }
        ms(vis, 0);
        dfsa(-1, star);
        for(int i = 0; i < vec.size(); i++)
        {
            ms(vis,0);
            flag = false;
            for(int j = 1; j <= n; j++)
            {
                if(dfs(vec[i], j))
                {
                    flag = true;
                    break;
                }
            }
            if(!flag) break;
        }
        if(!flag)
            puts("YES");
        else
            puts("NO");
    }
	return 0;
}