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

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

复杂度$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. jsonp 方式处理跨域前后端代码如何配合?

    JSONP(JSON with Padding)(json   数据填充)只支持GET请求 是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题. 跨域产生原因是浏览器的同源策略.( ...

  2. 2016NOI冬令营day1

    感冒了!!!: ( 上午听 picks 讲多项式导论(所有内容均不考)只听懂了那个O(n1.585)的多项式乘法算法 : ( 安装好了弹幕!太厉害了(有电脑的都在刷弹幕) :  ( 中午吃的不错 : ...

  3. bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单 ...

  4. rabbitmq架构简介(包括集群)

    总的来说,rabbitmq使用erlang语言编写,其架构类似于servlet容器运行servlet应用,底层是erlang VM.然后是erlang节点,上面是应用.如下所示: 每个MQ中运行的应用 ...

  5. shell分析http日志

    http状态码1字头----信息,服务器收到请求,需要请求者继续执行操作2字头----成功,操作被成功接收并处理3字头----重定向,需要进一步的操作以完成请求4字头----客户端错误,请求包含语法错 ...

  6. IDEA开发常用快捷键

    以下快捷键均为初始快捷键,可以进行自定义. 查询快捷键CTRL+N   查找类 CTRL+SHIFT+N  查找文件 CTRL+SHIFT+ALT+N 查找类中的方法或变量 CIRL+B   找变量的 ...

  7. BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟

    Description Recently, the cows have been competing with strings of balanced  parentheses and compari ...

  8. Oracle SQL Developer 中配置JDBC驱动程序连接

    此博客仅作为自己备忘,没有丝毫技术含量.把Postgres的JDBC驱动程序放在Oracle SQL Developer的JDBC的文件夹下,然后安装Oracle SQL Developer,添加新连 ...

  9. 对某项目中Vuex用法的分析

    上周五刚发布一个线上版本,趁着新的需求和bug还没到来,决定分析一下正在维护的一个使用Vue 2.0 开发的后台管理系统中Vuex部分代码.这部分代码不是我写的,加上我一直在“使用”现成的而不是“搭建 ...

  10. UVa 10003 切木棍(区间DP+最优矩阵链乘)

    https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...