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

题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000) ),问能否通过删除其中的某一条边,使得该图无环. 知识点: 拓扑排序.DFS 解题思路一: 由于结点数不多,所以可以枚举每个入度不为\(0\)的点,删去通向它的一条边(即使其入度减一),再跑拓扑排序判断有没有环. AC代码一: #inclu…
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一个简单环,则欲删除的边一定经过该环.尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环. 拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环.换起始点直到找到环为止. 复杂度O(nm). #include<cstdio>…
Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths from given source to all other vertices. For a general weighted graph, we can calculate single source shortest distances in O(VE) time using Bellman–For…
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can…
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in…
这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m(n+m))$会炸,于是可以有下面的优化. 我们找到所有入度不为零的点,然后把他们每一个都删掉一条边跑一遍拓扑排序. 那么这样就可以优化到$O(n(n+m))$了,稳得一批. AC代码如下: 1935ms 1356kb #include<bits/stdc++.h> using namespace…
Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it. Can you make this graph acyclic by remo…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包括这条边,那么就可以,否则,某个环不包括那也没办法,自然就无解了. 这样枚举的边的数目最多是500(环最多N条边) 然后复杂度就是500*10万了. 足够过了 [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample…
条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一般用有向边指示顺序关系,运用于顺序关系. 例如,下面这个图: 显然是一个DAG图,1→4表示4的入度+1,4是1的邻接点, 代码表示:前者deg[4]++;后者用vector[1].push(4) 如何写出拓扑排序代码? 1.首先将边与边的关系确定,建立好入度表和邻接表. 2.从入度为0的点开始删除…
大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <cstdio> #include <queue> #define REP(i,a,…