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

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 , 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个边的有向图。

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

思路:

枚举每一个节点,将该节点的入度减去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';
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/11620866.html

时间: 2024-08-28 14:21:38

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)的相关文章

Legal or Not(拓扑排序判环)

http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5788    Accepted Submission(s): 2678 Problem Description ACM-DIY is a large QQ group w

LightOJ1003---Drunk(拓扑排序判环)

One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let me share his ideas a bit. I am expressing in my wo

HDU 3342 Legal or Not(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目: Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lc

拓扑排序判环

拓扑排序的核心就是每次找入度为0的点,进入输出队列 ,然后将与此点相连的节点入度减1重复做以上操作.当做n-1 次后还有点没进输出队列 那么这些点就是环上的 因为环上的各点入度都为1 没有0的 就不能更新.也就是说拓扑排序一遍之后,如果是DAG所有点都恰好入队一次如果有环,那么一定存在没有入队的点. 例题: Legal or NotTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr

HDU1811 拓扑排序判环+并查集

HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈,我的博客里面其他几篇拓扑排序判圈的套路一样.但是这道题与他们不同的的是在大小关系里面存在一种 "="的关系,这就意味的那些序号不同的点,实际上是一个点.共享入度和出度.我们可以通过并查集将他们合并,合成一个点.这里说一下如何判断信息不完全.我们早先在做拓扑排序,多种排列方式的时候,按照字

Almost Acyclic Graph Codeforces - 915D

以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中任意一个简单环(点不重复),枚举断开其上每一条边即可(共最多n条边) 复杂度O(n*(n+m)) 注意:不能用拓扑排序找出在任意环上的点再找任意环,因为拓扑排序后入度不为0的不一定是环上的点(比如可能是某个点,没有出边,仅有一条入边,是某个环上的点引出的)(曾经错了) 1 #include<cstd

[hiho1174]拓扑排序一(拓扑排序判环)

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 由于今天上课的老师讲的特别无聊,小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi,你这学期有选什么课么? 小Hi:挺多的,比如XXX1,XXX2还有XXX3.本来想选YYY2的,但是好像没有先选过YYY1,不能选YYY2. 小Ho:先修课程真是个麻烦的东西呢. 小Hi:没错呢.好多课程都有先修课程,每次选课之前都得先查查有没有先修.教务公布的先修课程记录都是好多年前的,不但有重复的信息,好像很多都不正确了. 小Ho:课程

POJ 1094 Sorting It All Out(拓扑排序判环)

题目链接:http://poj.org/problem?id=1094 题目: Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D

HDU 4324 Triangle LOVE(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 题目: Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possi