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)看起来不超时 但是实现了以后发现 ...
随机推荐
- C++类中static修饰的函数的使用
//在C++中应该养成习惯:只用静态成员函数引用静态成员数据,而不引用非静态成员数据 #include <iostream>using namespace std;class st_inf ...
- 完美解决android显示gif
今天是周5啊.纠结了一天.android显示gif,没该控件 网上找开源项目 找到个viewgif.该作者在各大站点都在推荐自己的项目...好吧.用下吧. . . . 结果呢: 图片略微一大就 内存溢 ...
- 设置Android Studio工程布局文件的默认布局
每次创建新的工程后,布局文件的的布局总是ConstraintLayout,如何更改? 进入Android Studio安装目录,用文本编辑器打开文件plugins\android\lib\templa ...
- iOS中城市定位功能的实现
引入框架:CoreLocation .h文件 引入CoreLocation/CoreLocation.h @interface WeatherViewController :UIViewControl ...
- linq to xml There are multiple root elements.
添加xml结点的时候 var temp2 = temp1.Element("staticContent"); if (temp2 != null) { string str = & ...
- Web前端性能优化经验分享
最近一直有给新同学做前端方面的培训,也有去参与公司前端的招聘,所以把自己资料库里面很多高效且有用的知识做了些 规整分类,然后再分享一篇关于前端优化方面的总结.而且春节一过就又是招聘的高峰期了,在校的. ...
- javascript中的_return_false和return_true
1. [代码][JavaScript特效]代码 关于javascript中的 return false和return true 2008年12月22日 星期一 下午 09:31return 是 ...
- hiho Mission Impossible 6(模拟 未提交验证。。)
题意:模拟文本操作 思路:模拟 #include<iostream> #include<stdio.h> #include<string.h> using name ...
- I.MX6 Android /data 目录内容
/**************************************************************************** * I.MX6 Android /data ...
- html&css题
1.对WEB标准以及W3C的理解与认识?(1)web标准规范要求,书写标签必须闭合.标签小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率:(2)建议使用外链css和js脚本,从而达到结构与行为.结 ...