这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了。

随便画个图都可以卡掉我的解法。(不知道在想什么)


这道题的正解是拓扑排序。

朴素的想法是对所有边都跑一次拓扑,但这样$O(m(n+m))$会炸,于是可以有下面的优化。

我们找到所有入度不为零的点,然后把他们每一个都删掉一条边跑一遍拓扑排序。

那么这样就可以优化到$O(n(n+m))$了,稳得一批。


AC代码如下:

1935ms 1356kb

 #include<bits/stdc++.h>

 using namespace std;

 namespace StandardIO {

     template<typename T>inline void read (T &x) {
x=;T f=;char c=getchar();
for (; c<''||c>''; c=getchar()) if (c=='-') f=-;
for (; c>=''&&c<=''; c=getchar()) x=x*+c-'';
x*=f;
} template<typename T>inline void write (T x) {
if (x<) putchar('-'),x*=-;
if (x>=) write(x/);
putchar(x%+'');
} } using namespace StandardIO; namespace Solve { const int N=; int n,m;
vector<int>graph[N];
int indeg[N];
queue<int>q; inline bool toposort (int n) {
int temp[N],size=;
memcpy(temp,indeg,sizeof(indeg));
while (!q.empty()) q.pop();
for (register int i=; i<=n; ++i) {
if (temp[i]==) q.push(i),size++;
}
while (!q.empty()) {
int v=q.front();q.pop();
for (register int i=; i<graph[v].size(); ++i) {
int to=graph[v][i];
temp[to]--;
if (temp[to]==) q.push(to),size++;
}
}
return size>=n;
} inline void solve () {
read(n),read(m);
for (register int i=; i<=m; ++i) {
int x,y;
read(x),read(y);
indeg[y]++;
graph[x].push_back(y);
}
for (register int i=; i<=n; ++i) {
if (indeg[i]!=) {
indeg[i]--;
if (toposort(n)) {
puts("YES");
return;
}
indeg[i]++;
}
}
puts("NO");
}
} using namespace Solve; int main () {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
solve();
}

题解 CF915D 【Almost Acyclic Graph】的更多相关文章

  1. CF915D Almost Acyclic Graph

    题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, ...

  2. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  3. algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)

    Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...

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

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  5. D. Almost Acyclic Graph 判断减一条边能不能得到DAG

    D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. CodeForces 915D Almost Acyclic Graph

    Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...

  7. 【Educational Codeforces Round 36 D】 Almost Acyclic Graph

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...

  8. 拓扑排序-有向无环图(DAG, Directed Acyclic Graph)

    条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一 ...

  9. Almost Acyclic Graph CodeForces - 915D (思维,图论)

    大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...

随机推荐

  1. C#压缩和解压缩字节(GZip)

    标题:C#压缩和解压缩字节(GZip的使用) 作用:此类在 .NET Framework 2.0 版中是新增的.提供用于压缩和解压缩流的方法和属性.定义:表示 GZip 数据格式,它使用无损压缩和解压 ...

  2. html5plus 从相册选择图片后获取图片的大小

    plus.gallery.pick(function (filePath) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) ...

  3. c++类模板初探

    #include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...

  4. 字符串格式化输出、while循环、运算符、编码

    1.字符串格式化输出 %占位符: %s => 字符串 %d=>整数型 %%=>转义 普通的% %()不能多,不能少,一一对应 f"{}"大括号里的内容一般都放变量 ...

  5. Linux快速入门打开你的学习之道

    Linux快速入门打开你的学习之道 相信看到这篇文章的你一定是想要学习Linux,或者已经在学习Linux的人了,那我们就可以一起探讨一下,学习Linux如何快速入门呢? 首先,希望大家弄清楚自己为什 ...

  6. pandas 1 基本介绍

    import numpy as np import pandas as pd pd.Series() 构造数据 s = pd.Series([1, 3, 5, np.nan, 44, 1]) prin ...

  7. iOS技术栈-Swift版

    在网上没找到基于Swift的技术栈介绍,或者技术已经不适用,所以自己画了一个,后面的博客中会重新整理基于Swfit的UI控件介绍和常用第三方库地图

  8. Linux学习总结(14)——Linux权限控制

    linux中,权限的学习是必不可少的,不论是作为一名运维工程师或者是单一的管理者,学习好linux中的权限控制,你就可以保护好自己的隐私同时规划好你所管理的一切. 权限的学习是很多的,不要认为自己已经 ...

  9. Qt 学习之路问题

    Qt5 cannot include the file QMainWindow add "greaterThan(QT_MAJOR_VERSION, 4): QT += widgets&qu ...

  10. Laravel-错误调试与记录日志

    Laravel-错误调试与记录日志 标签(空格分隔): php 错误调试 配置 修改/config/app.php 'debug' => env('APP_DEBUG', true), 开启de ...