以前做过的题都不会了。。。。

此题做法:优化的暴力

有一个显然的暴力:枚举每一条边试着删掉

注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES)

因此找出图中任意一个简单环(点不重复),枚举断开其上每一条边即可(共最多n条边)

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

注意:不能用拓扑排序找出不能被排序的点来找环,因为拓扑排序后入度不为0的不一定是环上的点(比如可能是某个点,没有出边,仅有一条入边,是某个环上的点引出的)(曾经错了)

 #include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#define pb push_back
using namespace std;
int n,m,aa,bb;
vector<int> e[],ann;
queue<int> q;
int ma[],in[],st;
bool fl,ok[];
namespace Tarjan
{
int s[],dfn[],low[],dfc,top,sccnum[],sccc,sz[];
void dfs(int u)
{
dfn[u]=low[u]=++dfc;
s[++top]=u;
for(auto v:e[u])
{
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!sccnum[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
sccc++;
while(top&&s[top]!=u) sccnum[s[top--]]=sccc;
sccnum[s[top--]]=sccc;
}
}
void work()
{
int i;
for(i=;i<=n;i++)
if(!dfn[i])
dfs(i);
for(i=;i<=n;i++)
sz[sccnum[i]]++;
for(i=;i<=sccc;i++)
if(sz[i]>)
{
fl=;
st=i;
break;
}
for(i=;i<=n;i++)
if(sccnum[i]==st)
ok[i]=;
for(i=;i<=n;i++)
if(sccnum[i]==st)
{
st=i;
break;
}
}
}
int main()
{
int i,j,a,b,u;
scanf("%d%d",&n,&m);
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
e[a].pb(b);
}
Tarjan::work();
if(!fl) {puts("YES");return ;}
for(u=st;;)
{
ann.pb(u);
if(ma[u]) break;
ma[u]=ann.size();
for(auto v:e[u])
if(ok[v])
{
u=v;
break;
}
}
for(j=ma[ann[ann.size()-]]-;j<ann.size()-;j++)
{
aa=ann[j];bb=ann[j+];
for(i=;i<=n;i++) in[i]=;
for(i=;i<=n;i++)
for(auto v:e[i])
{
if(i==aa&&v==bb) continue;
in[v]++;
}
for(i=;i<=n;i++)
if(!in[i])
q.push(i);
while(!q.empty())
{
u=q.front();q.pop();
for(auto v:e[u])
{
if(u==aa&&v==bb) continue;
in[v]--;
if(!in[v]) q.push(v);
}
}
fl=;
for(i=;i<=n;i++)
if(in[i])
fl=;
if(fl)
{
puts("YES");
return ;
}
}
puts("NO");
return ;
}

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 (思维,图论)

    大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...

  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. Android SDK更新失败的解决方案(原创)

    笔者在搭建好Android环境后,进行Android的SDK更新下载升级,乌龟的速度,更让人生气的是到了85%的进度时,直接timeout,循环3次无果.查阅相关资料,原来是Google的服务器遭遇了 ...

  2. acd - 1427 - Nice Sequence(线段树)

    题意:一个由n个数组成的序列(序列元素的范围是[0, n]).求最长前缀 j .使得在这个前缀 j 中对于随意的数 i1 < i2.都满足随意的 m <= j.i1 在前 m 个数里出现的 ...

  3. VC FTP服务器程序分析(三)

    CControlSocket类的分析,CControlSocket类的内容比较多,为什么呢.因为通信控制命令的传输全部在这里,通信协议的多样也导致了协议解析的多样. 1.OnReceive  其大致说 ...

  4. js 中继承的几种方式

    继承的方式一共有三种: 一.原型继承 通过prototype   来实现继承. function Person(name,age) { this.name=name; this.age=age; } ...

  5. 【转载】Android Studio简单设置

    界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings --> Appearance --> Theme ,选择 Darcula 主题即可 ...

  6. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  7. hadoop datanode启动失败(All directories in dfs.data.dir are invalid)

    由于hadoop节点的磁盘满了,导致节点死掉,今天对其进行扩容.首先,将原节点的数据拷贝到目标节点下,从而避免数据的丢失,但是在执行hadoop_daemon.sh start datanode后没有 ...

  8. Python里的一些注释规范

    写代码注释是一件很重要的事情,如果你写的一段函数给别人调用那么往往都需要配上一些基本的注释.写好代码可以让别人容易阅读你的代码.试想一 下:如果你在github上面找到一段你想要的代码,这段代码有20 ...

  9. 用mkdirs创建目录

    import java.io.*; class a { public static void main(String args[]) { createDir("c:/fuck"); ...

  10. [Java]手动编译Java

    1.安装JDK 2.编写 Example.java 程序放到C 盘 public class Example { public static void main(string[] args) { sy ...