这道题我第一次的想法是直接判环的数量,然而事实证明实在是太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. asp.net HTTP请求过程

    http://blog.csdn.net/zxxSsdsd/article/details/51909860

  2. solarwinds之网络发现

    1.  首先需要添加网络发现   2.  使用public   3.  添加主机   4.  网络地址选择   5.  默认下一步   6.  运行发现   7.  扫描结构如下   8.  下一步 ...

  3. 用SqlDataReader返回多个结果集

    using System; using System.Data; using System.Data.SqlClient; namespace Northwind { class Program { ...

  4. LaTex的几种数学符号字体以及相关说明

    \mathrm is the normal upright Roman font \mathnormal is the normal math italic font: $\mathnormal{a} ...

  5. VB创建文件夹

    If Dir("D:\Program Files\AutoCAD 2006\Express\", vbDirectory) = "" Then '判断文件夹是否 ...

  6. keepalive安装配置

    安装 Centos7.4 yum install keepalived 配置 Master服务器配置 [root@wsjy-proxy01 keepalived]# cat keepalived.co ...

  7. 紫书 例题8-19 UVa 12265 (扫描法+单调栈)

    首先可以用扫描法处理出一个height数组, 来保存从当前行开始, 每一个格子可以向上延伸的最大长度. 这种"延伸"的问题用扫描法, 因为往往这个时候可以利用前一次的结果来更新当前 ...

  8. 题解 洛谷 P4047 【[JSOI2010]部落划分】

    我觉得几乎就是一道最小生成树模板啊... 题解里许多大佬都说选第n-k+1条边,可我觉得要这么讲比较容易理解 (虚边为能选的边,实边为最小生成树) 令n=5,k=2,(1,3)<(1,2)< ...

  9. NYIST 1108 最低的惩罚

    最低的惩罚 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 那么现在问题就来了... 给你N(1=<N<=15)个任务,每个任务有一个截止完成时间t(1= ...

  10. TODOList 多线程交互、RCP、事物控制、数据倾斜、HBase数据同步性

    TODOList 多线程交互.RCP.事物控制.数据倾斜.HBase数据同步性 TODO List thread.join()如何互相之间通知? 线程池何时最后运行完成? MemCache性能要优于R ...