大意: 给定无向图, 求是否能删除一条边后使图无环

直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可

复杂度$O(n(n+m))$

#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
using namespace std; const int N = 510;
int n, m, ok;
vector<int> g[N];
int vis[N], s[N], deg[N], a[N]; int topo() {
queue<int> q;
REP(i,1,n) if (!a[i]) q.push(i);
int r = 0;
while (!q.empty()) {
++r;
int x=q.front();q.pop();
for (int y:g[x]) {
if (!--a[y]) q.push(y);
}
}
return r==n;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,m) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),++deg[v];
}
REP(i,1,n) if (deg[i]) {
memcpy(a,deg,sizeof deg);
--a[i];
if (topo()) return puts("YES"),0;
}
puts("NO");
}

Almost Acyclic Graph CodeForces - 915D (思维,图论)的更多相关文章

  1. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  2. Almost Acyclic Graph Codeforces - 915D

    以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中 ...

  3. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  4. 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 ...

  5. Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列

    Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...

  6. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  7. D. Almost Acyclic Graph 判断减一条边能不能得到DAG

    D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. CodeForces 915D Almost Acyclic Graph

    Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...

  9. codeforces 915D Almost Acyclic Graph 拓扑排序

    大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...

随机推荐

  1. python六剑客:map()、lambda()、filter()、reduce()、推导类表、切片

    一:map():映射 map()有两个参数,一个函数,一个序列,序列中每一个元素都会做为参数传给前边的函数,然后生成新的列表, 第二个参数必须用一个序列:元祖,列表,字符串 >>> ...

  2. pythoy的configparser模块

    生成配置文件的模块 DEFAULT块,在以块为单位取块的值时,都会出现 import configparser config = configparser.ConfigParser() #相当于生成了 ...

  3. P4336 [SHOI2016]黑暗前的幻想乡

    P4336 [SHOI2016]黑暗前的幻想乡 矩阵树定理(高斯消元+乘法逆元)+容斥 ans=总方案数 -(公司1未参加方案数 ∪ 公司2未参加方案数 ∪ 公司3未参加方案数 ∪ ...... ∪ ...

  4. 20145225唐振远《网络对抗》Exp4 恶意代码分析

    20145225唐振远<网络对抗>Exp4 恶意代码分析 基础问题回答 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪 ...

  5. noip 2012 提高组 day2 部分题解

    这道题有多种解法,我用的是扩展欧几里得算法求到的答案 #include<iostream> #include<fstream> #include<cstdio> u ...

  6. MySQL timespan设置 sql_mode设置

    Timespan设置: 在MySQL5.7版本中创建表 CREATE TABLE `investor_seat` ( `id` int(11) NOT NULL AUTO_INCREMENT , `i ...

  7. Linux 文件的权限

    备注 : -rw-r--r-- 第一个“-”不算 ,三个一组 这个就是 644   二.使用chown命令更改文件拥有者 在 shell 中,可以使用chown命令来改变文件所有者.chown命令是c ...

  8. 筛选出sql 查询结果中 不包含某个字符

    select * from table1 where patindex('%关键字%' , aa) = 0 select * from table1 where charindex('关键字' , a ...

  9. v-bind绑定属性样式

    一.class的四种绑定方式 1.布尔值的绑定方式 <div id="demo"> <span v-bind:class="{'class-a':isA ...

  10. Sass常用写法

    Sass使用变量,变量以$开头 $bgcolor:#f40; background-color:$bgcolor; 如果变量需要嵌套在字符串当中,就需要写在#{}之中 $direction:left; ...