Almost Acyclic Graph CodeForces - 915D

博客围绕有向图展开,给定一个含n个顶点和m条边的有向图,允许最多移除一条边,判断能否使图变为无环图。介绍了输入输出格式,通过示例说明情况,最后给出AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Almost Acyclic Graph

CodeForces - 915D

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值