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)看起来不超时 但是实现了以后发现 ...
随机推荐
- 对JS闭包的理解
闭包,是JS里很重要的一个概念,也是相对来讲不太容易理解的一个东西,不过即使难理解,我们也要迎难而上啊,嘿嘿,网上有很多文章都在讲闭包,我在看JS设计模式的时候,书里也着重讲了闭包,但是书里官方的的确 ...
- 集群环境搭建-SSH免密码登陆(二)
1.打开sshd配置 命令: vi /etc/ssh/sshd_config 找到以下内容,并去掉注释符”#“ RSAAuthentication yes PubkeyAuthentication y ...
- 秒懂单链表及其反转(reverse)
什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...
- sed替换变量
今天在写脚本时用到了sed,我用sed替换xml文件中的变量.一般在sed 中替换都用单引号,如下边 sed -i ‘s/10/1000/g’ test.xml但是如果需要把1000改成变量,如sed ...
- xunit inlinedata classdata memberdata
https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdat ...
- import data from excel to sql server
https://www.c-sharpcorner.com/article/how-to-import-excel-data-in-sql-server-2014/ 需要注意的是,第一次是选择sour ...
- BZOJ_1812_[Ioi2005]riv_树形DP
BZOJ_1812_[Ioi2005]riv_树形DP Description 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了 ...
- 【POJ 1655】 Balancing Act
[题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...
- 哈希表的C实现(一)
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...
- Android 开发:由模块化到组件化
在Android SDK一文中,我们谈到模块化和组件化,现在我们来聊聊组件化开发背后的哪些事.最早是在广告SDK中应用组件化,但是同样适用于普通应用开发 以下高能,请做好心理准备,看不懂请发私信来交流 ...