题解 CF915D 【Almost Acyclic Graph】
这道题我第一次的想法是直接判环的数量,然而事实证明实在是太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】的更多相关文章
- CF915D Almost Acyclic Graph
题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- 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 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- D. Almost Acyclic Graph 判断减一条边能不能得到DAG
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CodeForces 915D Almost Acyclic Graph
Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...
- 【Educational Codeforces Round 36 D】 Almost Acyclic Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...
- 拓扑排序-有向无环图(DAG, Directed Acyclic Graph)
条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一 ...
- Almost Acyclic Graph CodeForces - 915D (思维,图论)
大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...
随机推荐
- 让placeholder中的默认文字居中,或者缩进多少像素
直接给input或者textarea的样式加texta-align:center; <input type="" name="" id="&qu ...
- 【原创】使用Kettle的一些心得和经验
用kettle做etl也有段时间了,遇到很多问题,总结了一下. [关于版本的问题] kettle常用的版本有4.1和4.4,对于4.1版本: 1.该版本的兼容性有点差,在某些机器上运行会启动失败,或者 ...
- asp.net 连接字符串的多种写法
一.使用OleDbConnection对象连接OLE DB数据源 1.连接Access 数据库 Access 2000: “provider=Microsoft.Jet.Oledb.3.5;Data ...
- Debian/Linux 下无线网卡驱动的安装
我的 PC 型号是 Acer V3-572G, 安装了 Debian 后, 发现只能通过有线网络上网, 无法识别无线网卡, 以下是解决的过程(不局限于此型号 PC): 在命令行键入 lspci , 得 ...
- n行m列矩阵顺时针填写1~n*m
程序效果图如下: 程序参考代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- mysql5.7 安装方法 (跟旧的不一样了)
MySQL 5.7发布之后很多网友都在说,打开想安装文件夹.但是文件夹中没有DATA目录, 没有mysqly默认库.启动不了数据库,那是因为5.7的数据库的初始化方法和之前的初始化不一样了. 首先这里 ...
- 国庆 day 7 上午
思路:模拟,set记录一下. #include<set> #include<cstdio> #include<cstring> #include<iostre ...
- APP-午饭去哪吃
走到这个快节奏的城市中.部门聚餐.朋友吃饭这些都是日常生活中时有发生的事情,往往吃的东西都是千篇一律,图的也仅仅剩下的是环境了.那么.非常纠结常常去的地方,怎么办呢?来吧.我们随机摇一个吧! wate ...
- YTUOJ-计算该日在本年中是第几天(用户自己定义类型)
题目描写叙述 定义一个结构体变量(包含年.月.日).编写一个函数days,由主函数将年.月.日传递给函数days,计算出该日在本年中是第几天并将结果传回主函数输出. 输入 年月日 输出 当年第几天 例 ...
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记3 Xcode、Auto Layout及MVC
继续上一话中的计算器Demo.上一话讲到类必须被初始化.类中的属性也必须被初始化,所以你不能仅仅声明而不给它一个处置,那么问题来了,我们从storyboard中拖拽的@IBOutlet为什么仅仅有声明 ...