大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <cstdio> #include <queue> #define REP(i,a,…
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…
以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中任意一个简单环(点不重复),枚举断开其上每一条边即可(共最多n条边) 复杂度O(n*(n+m)) 注意:不能用拓扑排序找出不能被排序的点来找环,因为拓扑排序后入度不为0的不一定是环上的点(比如可能是某个点,没有出边,仅有一条入边,是某个环上的点引出的)(曾经错了) #include<cstdio>…
[题目]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…
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间连一条边.排序完后,求得到图的最大独立集. 解释: 最初想到的是图的最大独立集,认为不能解,于是就没辙了... 然而应当仔细分析题目(不容易想到):题意很容易知道是在每一对逆序对间连一条边.也就是说,对于a[i],向a[i]右侧比其小的和a[i]左侧比其大的都要连一条边.也就是说,只要选了结点i,那…
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn vertices and mm edges. You have to write a number on each vertex of the graph. Each number should be 11, 22or 33. The graph becomes beautiful if for…
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…
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…
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现即使优化到不清空vis数组(时间戳标记),也仍然超时. 因为O(N*M)已经很接近时间复杂度上界,常数稍大就GG. 不过可以脑补一下取巧算法:在不超时的前提下,随机取K个边进行检验~~~.不过数据多了就非常容易GG.理论上还是可行的. 正解:从枚举边变为枚举点,删掉到达一个点的某条边可以认为是该点入…