ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

2019-10-03 21:53:46  阅读:325  来源: 互联网

标签:判环 const Almost Graph Len int edge include define


Almost Acyclic Graph

CodeForces - 915D

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

Copy

3 41 22 33 23 1

output

Copy

YES

input

Copy

5 61 22 33 23 12 14 5

output

Copy

NO

Note

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

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

题意:

给你有一个n个点,m个边的有向图。

问是否可以只删除一个边,使整个图无环。

思路:

枚举每一个节点,将该节点的入度减去1,先不用管删除的是哪个边,删除一个终点是i节点的边的影响就是i的入度减去1.

然后通过拓扑排序在\(O(n+m)\) 的时间复杂度里可以判断出一个有向图是否有环。

所以整体的时间复杂度是\(O(n*(n+m))\)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
// const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

const int maxn = 510;
const int maxm = 3e5 + 10;
struct edge {
    int to, from, nxt;
} edges[maxm];

int n, ind[maxn];
int in[maxn];
int head[maxn], cnt;
// 初始化
void init(int _n)
{
    n = _n, cnt = -1;
    for (int i = 1; i <= n; i++) { head[i] = -1, ind[i] = 0; }
}
// 加边
void addedge(int u, int v)
{
    edges[++cnt].from = u;
    edges[cnt].to = v;
    edges[cnt].nxt = head[u];
    head[u] = cnt;
    ind[v]++;
}

bool go()
{
    queue<int> Q;
    for (int i = 1; i <= n; i++) {
        if (ind[i] == 0) { Q.push(i); }
    }
    cnt = 0;
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        cnt++;
        for (int i = head[u]; i != -1; i = edges[i].nxt) {
            int v = edges[i].to;
            if (--ind[v] == 0) { Q.push(v); }
        }
    }
    return cnt == n;
}

int m;
int x, y;

int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    du2(n, m);
    init(n);
    while (m--) {
        du2(x, y);
        addedge(x, y);
        in[y]++;
    }
    int isok = 0;
    repd(i, 1, n) {
        if (in[y]) {
            memcpy(ind, in, sizeof(in));
            ind[i]--;
            if (go()) {
                isok = 1;
                break;
            }
        }
    }
    if (isok) {
        puts("YES");
    } else {
        puts("NO");
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



标签:判环,const,Almost,Graph,Len,int,edge,include,define
来源: https://www.cnblogs.com/qieqiemin/p/11620866.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有