这道题我第一次的想法是直接判环的数量,然而事实证明实在是太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. matlab张量工具初步

    最近从桑迪亚实验室下载了张量工具包.但是不太会用. 很多网上的方法, addpath(pwd) cd met; addpath(pwd) savepath M=ones(4,3,2); X=tenso ...

  2. 你不知道的JavaScript博文参考书籍

    you don't know js系列书籍是谷歌地图开发人员编写,内容非常好,四卷已收集齐全. 笔者打包上传到了CSDN,下载地址: http://download.csdn.net/detail/r ...

  3. (转载)ScratchView

    首页 我的管理 客户端 登录注册 首页 安卓组件中心 安卓代码分享 Swift专区 HTML5移动开发 视频中心 小程序 名称: ScratchView 作者: doliangzhe 来源: gith ...

  4. JS 前端 将图片转换为Base64利用H5 FileReader新特性

      file = document.getElementById("image"); var file=file.files[0]; var fileName=file.name; ...

  5. [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂

    Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...

  6. BZOJ 3881 [COCI2015]Divljak (Trie图+Fail树+树链的并+树状数组维护dfs序)

    题目大意: Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  7. linux磁盘管理与分区 转载

    原文:http://zhengjianglong.leanote.com/post/linux%E7%A3%81%E7%9B%98%E5%88%86%E5%8C%BA 一.基础知识 一块磁盘可以分为多 ...

  8. thinkphp 5.0整合phpsocketio完整攻略,绕坑

    使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...

  9. 数组中出现一次的两个数(三个数)& 求最后一位bit为1

    对于两个数,对于结果中,剩余bit1来异或区分. 下面的解法,非常精简: int lastBitOf1(int number) { ); } void getTwoUnique(vector<i ...

  10. hadoop-05-mysql修改密码

    hadoop-05-mysql修改密码 su root 1,service mysqld start 2,vi /var/log/mysqld.log #在这里面查找密码 3, mysql -uroo ...