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

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

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

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <cstdio>
  6. #include <queue>
  7. #define REP(i,a,n) for(int i=a;i<=n;++i)
  8. #define pb push_back
  9. using namespace std;
  10.  
  11. const int N = 510;
  12. int n, m, ok;
  13. vector<int> g[N];
  14. int vis[N], s[N], deg[N], a[N];
  15.  
  16. int topo() {
  17. queue<int> q;
  18. REP(i,1,n) if (!a[i]) q.push(i);
  19. int r = 0;
  20. while (!q.empty()) {
  21. ++r;
  22. int x=q.front();q.pop();
  23. for (int y:g[x]) {
  24. if (!--a[y]) q.push(y);
  25. }
  26. }
  27. return r==n;
  28. }
  29.  
  30. int main() {
  31. scanf("%d%d", &n, &m);
  32. REP(i,1,m) {
  33. int u, v;
  34. scanf("%d%d", &u, &v);
  35. g[u].pb(v),++deg[v];
  36. }
  37. REP(i,1,n) if (deg[i]) {
  38. memcpy(a,deg,sizeof deg);
  39. --a[i];
  40. if (topo()) return puts("YES"),0;
  41. }
  42. puts("NO");
  43. }

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. c++编程规范的纲要和记录 (转)

    这是一本好书, 可以让你认清自己对C++的掌握程度. 看完之后,给自己打分,我对C++了解多少? 答案是不足20分. 对于我自己是理所当然的问题, 就不提了, 记一些有启发的条目和细节: (*号表示不 ...

  2. SNMP学习笔记之iReasoning MIB Browser

    0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...

  3. vc++引用外部dll时报error LNK2019: 无法解析的外部符号

    初学cpp,因为之前装linux下各种软件的时候,知道LD_LIBRARY_PATH可以指定动态库的目录.今天在vc集成log4cpp的时候,编译main时报error LNK2019: 无法解析的外 ...

  4. 如何在 Linux 中将文件编码转换为 UTF-8

    一个文件:vim 文件名.txt输入 :e ++enc=gbk 强制用gbk打开输入 :w ++enc=utf8 转换到utf8保存. 多个文件:for i in *.txt; do iconv -f ...

  5. 08:Python数据分析之pandas学习

    1.1 数据结构介绍 参考博客:http://www.cnblogs.com/nxld/p/6058591.html 1.pandas介绍 1. 在pandas中有两类非常重要的数据结构,即序列Ser ...

  6. 20145225 《网络对抗》逆向及Bof基础实践

    实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...

  7. VirtualBox 安装XP虚拟机需要注意的问题

    1.首先要有xp系统的镜像文件 http://pan.baidu.com/s/1i4xrwdJ 2.新建虚拟机,并安装 http://www.pczhishi.cn/shipin/107.html 3 ...

  8. Python3基础 os mkdir 创建一层文件夹 在有父目录的情况下创建子目录

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. strerror函数的总结

    定义函数:char * strerror(int errnum); 函数说明:strerror()用来依参数errnum 的错误代码来查询其错误原因的描述字符串, 然后将该字符串指针返回. 返回值:返 ...

  10. 解决Android Studio Conflict with dependency 'com.android.support:support-annotations'报错

    解决Android Studio Conflict with dependency 'com.android.support:support-annotations'报错 在Android Studi ...