1. 程式人生 > >codeforces Round 36 D. Almost Acyclic Graph(dfs+拓撲排序判環)

codeforces Round 36 D. Almost Acyclic Graph(dfs+拓撲排序判環)

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 ≤ 5001 ≤ 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.


【分析】

賽時沒有A掉這道題。之後看群裡討論說,找到一個環,然後在這個環上dfs,列舉刪掉遍歷到的邊,再拓撲排序判環。

今天寫了個程式碼不知道為啥奇蹟般的A了。直接dfs這個圖,列舉刪除遍歷到的邊 同時判環。

【程式碼】

#include<bits/stdc++.h>
using namespace std;
struct NODE{
    int from,to;
    int next;
    NODE(int f=0,int t=0,int ne=0){
        from=f;to=t;next=ne;
    }
}edge[220000];
int head[520];
int inn[520],in[520];//入度
int vis[520];
int n,m;
int flag=0;
int check()//拓撲排序判環
{
    int q[520],tail=0;
    for(int i=1;i<=n;i++)//入度為0的點入隊
        if(!in[i])
            q[tail++]=i;
    for(int i=0;i<tail;i++)
    {
        int s=q[i];//此起點的邊全刪
        for(int j=head[s];j!=-1;j=edge[j].next)
        {
            in[edge[j].to]--;
            if(!in[edge[j].to])//新的入度為0的點
            {
                q[tail++]=edge[j].to;
            }
        }
    }
    return n==tail;
}
void dfs(int u)
{
    if(flag)return;
    for(int k=head[u];~k;k=edge[k].next)if(!vis[edge[k].to])
    {
        int t=edge[k].to;
        vis[t]=1;
        for(int i=1;i<=n;i++)in[i]=inn[i];
        in[t]--;
        if(check()){flag=1;break;}
        dfs(t);
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        for(int i=0;i<m;i++)
        {
            int f,t;
            scanf("%d%d",&f,&t);
            in[t]++;
            inn[t]++;
            edge[i]=NODE(f,t,head[f]);
            head[f]=i;//鏈式前向星
        }
        memset(vis,0,sizeof(vis));
        flag=0;
        for(int i=1;i<=n;i++)dfs(i);
        if(flag)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}