Almost Acyclic Graph Codeforces - 915D
以前做过的题都不会了。。。。
此题做法:优化的暴力
有一个显然的暴力:枚举每一条边试着删掉
注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是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的更多相关文章
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- Almost Acyclic Graph CodeForces - 915D (思维,图论)
大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- 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 ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- D. Almost Acyclic Graph 判断减一条边能不能得到DAG
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CodeForces 915D Almost Acyclic Graph
Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...
- codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...
随机推荐
- 64位CentOs7源码安装mysql-5.6.35过程分享
首先安装依赖包,避免在安装过程中出现问题 [root@bogon liuzhen]# yum -y install gcc gcc-c++[root@bogon liuzhen]# yum -y in ...
- Appium basic UI check cases_from sample
@Test public void testUIComputation() throws Exception { // populate text fields with values populat ...
- 距特征之k阶距概念
k阶原点距和k阶中心距各是说明什么数字特征 http://www.cnblogs.com/emanlee/archive/2011/04/25/2028628.html 二阶中心距,也叫作方差,它告诉 ...
- Wordpress播客网站搭建
- HDU1300 Pearls —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-1300 Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- MongoDB C++ gridfs worked example
使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html #include <mongoc.h> #i ...
- 出现Insufficient space for shared memory file错误解决
今天在linux下敲命令,出现上面的错误,原来是临时文件目录(/tmp)下的空间不够了,df一看/下100%了.
- Android studio 构建太慢
/*********************************************************************************** * Android studi ...
- 【AC自动机&&Trie图】积累
以前KMP和后缀系列(主要是后缀数组,后缀自动机),都刷了一定数量的题,但是对于AC自动机,却有些冷落,罪过. 但是我感觉,在蓝桥杯比赛中AC自动机出现的概率比后缀系列大,简单的会考匹配,稍难一点会考 ...
- macbook pro 自带和用户后装的jdk的路径
苹果系统已经包含完整的J2SE,其中就有JDK和JVM(苹果叫VM).当然如果要升级JDK,那当然要自己下载安装了. 在MAC系统中,jdk的安装路径与windows不同,默认目录是:/System/ ...