题目链接:http://codeforces.com/contest/915/problem/D

题目大意:

  给出一个\(n\)个结点\(m\)条边的有向图(无自环、无重边,2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000) ),问能否通过删除其中的某一条边,使得该图无环。

知识点:  拓扑排序、DFS

解题思路一:

  由于结点数不多,所以可以枚举每个入度不为\(0\)的点,删去通向它的一条边(即使其入度减一),再跑拓扑排序判断有没有环。

AC代码一:

 #include <bits/stdc++.h>

 using namespace std;
const int maxn = , maxm = ;
vector<int> G[maxn];
int in[maxn],tin[maxn],stac[maxn]; bool topo(int n){
int top=,ending=;
for(int i=;i<=n;i++){
if(tin[i]==)
stac[ending++]=i;
}
while(top<ending){
int now=stac[top];
for(int i=;i<G[now].size();i++){
tin[G[now][i]]--;
if(tin[G[now][i]]==) stac[ending++]=G[now][i];
}
top++;
}
return ending>=n;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
in[v]++;
G[u].push_back(v);
}
for(int i=;i<=n;i++){
if(in[i]!=){
for(int j=;j<=n;j++) tin[j]=in[j];
tin[i]--;
if(topo(n)) return *puts("YES");
}
}
return *puts("NO");
}

解题思路二:

  先找出一个简单环,然后枚举删除该环上的每一条边,再跑拓扑排序判断还有没有环。

AC代码二:

#include <bits/stdc++.h>
using namespace std;
const int maxn = ; vector<int> G[maxn];
int mark[maxn],in[maxn],tin[maxn],last[maxn];
int cnt, loop[maxn];//cnt记录环上的结点数
bool dfs(int rt,int la){
last[rt]=la;
mark[rt]=; //访问过的结点,mark=1;
for(int i=;i<G[rt].size();i++){
if(mark[G[rt][i]]==){ //没有访问过的结点, mark=0
if(dfs(G[rt][i],rt)) return true;
}
if(mark[G[rt][i]]==){
int now=rt;
cnt=;
while(now!=G[rt][i]&&now!=-){
loop[cnt++]=now;
now=last[now];
}
loop[cnt++]=G[rt][i];
return true;
}
}
mark[rt]=-; //访问过并且会走到死路的结点,mark=-1
return false;
}
int stac[maxn];
bool topo(int n,int from,int to){//拓扑排序检查是否有环
int top=,ending=;
for(int i=;i<=n;i++){
if(tin[i]==)
stac[ending++]=i;
}
while(top<ending){
int now=stac[top];
for(int i=;i<G[now].size();i++){
if(now==from&&G[now][i]==to) continue;
tin[G[now][i]]--;
if(tin[G[now][i]]==) stac[ending++]=G[now][i];
}
top++;
}
return ending>=n;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
in[v]++; //记录入度
}
for(int i=;i<=n;i++){
if(mark[i]==){
if(dfs(i,-)) break;
}
}
if(!cnt) return *puts("YES");
for(int i=;i<cnt;i++){
for(int j=;j<=n;j++) tin[j]=in[j];
tin[loop[(i+)%cnt]]--;
if(topo(n,loop[i],loop[(i+)%cnt])) return *puts("YES");
}
return *puts("NO");
}

CF915D Almost Acyclic Graph的更多相关文章

  1. 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 ...

  2. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  3. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  4. D. Almost Acyclic Graph 判断减一条边能不能得到DAG

    D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. 题解 CF915D 【Almost Acyclic Graph】

    这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m( ...

  6. CodeForces 915D Almost Acyclic Graph

    Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...

  7. 拓扑排序-有向无环图(DAG, Directed Acyclic Graph)

    条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一 ...

  8. Almost Acyclic Graph CodeForces - 915D (思维,图论)

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

  9. Almost Acyclic Graph Codeforces - 915D

    以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中 ...

随机推荐

  1. 徐州H

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...

  2. IDEA 之 ERROR:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]

    问题描述:在使用IDEA对JSTL进行测试时出现error:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core] ...

  3. JavaScript正则表达式及jQuery回顾

    JavaScript 正则表达式,用于规定在文本中检索的内容. 一.定义正则表达式: rep = /\d+/; // js定义正则.(python定义正则:re模块 rep = "\d+&q ...

  4. Centos7下查询jdk安装路径

    今天一个小实验需要安装jdk,用命令java -version查询了一下,原来Centos7自带OpenJDK的环境,但是需要手动配置/etc/profile文件,于是开始找java的安装路径.... ...

  5. 怎么避免写出慢SQL

    在大多数实际的系统中,慢 SQL 消耗掉的数据库资源,往往是正常 SQL 的几倍.几十倍甚至几百倍. 怎样才能在开发阶段尽量避免写出慢 SQL 呢? 估算数据量 慢 SQL 对数据库的影响,是一个量变 ...

  6. golang之reflect

    reflect,反射. 利用reflect,可以得到一个struct的相关信息. package main import ( "fmt" "reflect" ) ...

  7. LeetCode 572. 另一个树的子树 | Python

    572. 另一个树的子树 题目来源:https://leetcode-cn.com/problems/subtree-of-another-tree 题目 给定两个非空二叉树 s 和 t,检验 s 中 ...

  8. KMP+Tire树(模板)

    \(\color{Red}{KMP板子}\) #include <bits/stdc++.h> using namespace std; const int maxn=1e6+9; int ...

  9. 设计模式(Java语言)- 建造者模式

    前言 在日常的生活中,我们可以经常看到建造者模式的影子.比如,建造房子,那么房子就是一个产品,房子由门,窗,墙,地板等部门组成.然后包工头在建造房子的时候就根据设计好的图纸来建造,但是包工头并不是亲自 ...

  10. Spring官网阅读 | 总结篇

    接近用了4个多月的时间,完成了整个<Spring官网阅读>系列的文章,本文主要对本系列所有的文章做一个总结,同时也将所有的目录汇总成一篇文章方便各位读者来阅读. 下面这张图是我整个的写作大 ...