这道题我第一次的想法是直接判环的数量,然而事实证明实在是太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. CSS3-----transform 转换

    transforn  可以转换元素,其中主要属性有:rotate() / skew() / scale() / translate()以下4种. transform:rotate():旋转:其中“de ...

  2. vue 事件参数传$event打印当前组件

    <template> <div class="hello"> <button v-on:click.once="getinfo($event ...

  3. django框架-DRF工程之认证功能

    1.在Rest framework中进行了一系列的封装,这个认证功能也是被封装到在DRF工程中的一种,想要使用,首先需要在配置文件中进行相应的配置 REST_FRAMEWORK = { ’DEFAUL ...

  4. nodejs 守护进程运行

    有四种方法: 1.forever forver start  bin/www 2.pm2 pm2 strat bin/www 3.node自身进程保护 nohup node /bin/www  > ...

  5. Docker学习总结(4)——Docker镜像与容器命令

    Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机).bare metal. ...

  6. jquery.validate动态更改校验规则 【转】

    有时候表单中有多个字段是相互关联的,以下遇到的就是证件类型和证件号码的关联,在下拉框中选择不同的证件类型,证件号码的值的格式都是不同的,这就需要动态的改变校验规则. <!DOCTYPE html ...

  7. 洛谷 P1033 自由落体

    P1033 自由落体 题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算公 ...

  8. java 自定义实现base64编码转换

    1.base64编码转换 所谓base64编码,即按照规则把字符转化为"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 ...

  9. MapReduce 的类型与格式【编写最简单的mapreduce】(1)

    hadoop mapreduce 中的map 和reduce 函数遵循下面的形式 map: (K1, V1) → list(K2, V2) reduce: (K2, list(V2)) → list( ...

  10. 微软100题第51题:和为n连续正数序列

    题目:输入一个正数n,输出全部和为n连续正数序列.比如输入15,因为1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. 方法一:记录序列长度.推断首项是否满足条 ...