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

此题做法:优化的暴力

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

注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是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. sessionFactory的创建和四种查询方式

    1,关于sessionFactory的创建 5.0版本之前,下面这种方式在5.0及之后,可能会出问题,建议修改为5.0之后的方式 // 实例化Configuration Configuration c ...

  2. python dictionary的遍历

    d = {'x':1, 'y':3, 'z':2} for k in d:    print d[k] 直接遍历k in d的话,遍历的是dictionary的keys. 2 字典的键可以是任何不可变 ...

  3. bash shell parameter expansion

    1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...

  4. Lambda 闭包 匿名 函数 类

    深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法) - _Luc_ - 博客园 https://www.cnblogs.com/figure9/p/java-8 ...

  5. Service-level agreement

    Service-level agreement - Wikipedia https://en.wikipedia.org/wiki/Service-level_agreement

  6. MySQL alter语句

    1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL  COMMENT '注释说明' 3: ...

  7. linux命令启动服务(tomcat服务或者jar包)

    启动tomcat: 1.方式一:直接启动 ./startup.sh 2.方式二:nohup ./startup.sh & 启动后,关闭当前客户端连接,重新启动一个查看是 否已经启动 启动jar ...

  8. FZU1686 神龙的难题 —— Dancing Links 可重复覆盖

    题目链接:https://vjudge.net/problem/FZU-1686 Problem 1686 神龙的难题 Accept: 812    Submit: 2394 Time Limit: ...

  9. codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)

    题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...

  10. VC++读写文件

    目录 第1章读写文件    1 1.1 API    1 1.2 低级IO    1 1.2.1 文件序号    1 1.2.2 文本文件与二进制文件    1 1.3 流IO    2 1.4 Un ...