CodeForces 915D Almost Acyclic Graph(拓撲排序)
You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.
Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).
InputThe first line contains two integers n and m (2 ≤ n ≤ 500,1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively.
Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ n,u ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge fromu tov).
OutputIf it is possible to make this graph acyclic by removing at most one edge, print YES
3 4Output
1 2
2 3
3 2
3 1
YESInput
5 6Output
1 2
2 3
3 2
3 1
2 1
4 5
NONote
In the first example you can remove edge , and the graph becomes acyclic.
In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic.
這題居然是topsort……
大致題意就是給你一個圖,然後最短刪除一條邊,問你是否能夠把這個圖變成一個DAG有向無環圖。
暴力的話列舉所有的邊,然後再遍歷圖,複雜度為O(M*(N+M))果斷超時。自己畫出圖來找一些環的個數以及相互之間的重邊的關係,還是沒有能夠找到一個很好的規律。另一個方法就是,找出一個的環,然後標記環上的邊,列舉這些邊刪掉,看是否還有環。但是這個操作起來不好處理,本身求具體的環就要用tarjan,然後還要在上面修改標記下環上的邊……
正解是,既然刪掉一條邊對度的改變是是一個點入度減一,另一個點的出度減一,而拓撲排序正好可以判斷環而且與入度關聯,因此我們考慮列舉一個點,這個點關聯被刪掉的邊入度減一。我們把列舉的點入度減一,看看之後能否完整的做完拓撲排序,如果可以那麼就有解,否則沒有。那麼這個正確性如何理解呢?我們可以這樣想,少一條邊,相當於某個點入度減一,而這個點入度減一之後能否做完拓撲排序,則對應是否還有環,這樣就對應過來了。或者也可以理解為,列舉環上的點,讓他入度減一,相當於破掉與其關聯的一個環,再看是否有環。
具體見程式碼:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
#define N 510
using namespace std;
int n,m,d[N],a[N];
vector<int> g[N];
bool topsort(int x)
{
memcpy(a,d,sizeof(d));
int num=0; a[x]--; queue<int> q;
for(int i=1;i<=n;i++)
if (a[i]==0) q.push(i),num++;
while(!q.empty())
{
int i=q.front(); q.pop();
for(int j=0;j<g[i].size();j++)
if (!(--a[g[i][j]])) q.push(g[i][j]),num++;
}
return num==n;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v); d[v]++;
}
for(int i=1;i<=n;i++)
if (topsort(i))
{
puts("YES");
return 0;
}
puts("NO");
return 0;
}
相關推薦
CodeForces 915D Almost Acyclic Graph(拓撲排序)
D. Almost Acyclic Graph time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard
Almost Acyclic Graph(拓撲排序)
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outp
D. Almost Acyclic Graph【拓撲排序判環】
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes
codeforces-915D Almost Acyclic Graph(拓撲序判圈)
連結:http://codeforces.com/problemset/problem/915/D題意:在一個有向圖中,判斷能不能至多刪除一條邊使得該圖不含圈,n<=500, m<=1000
CodeForces 915D Almost Acyclic Graph
directed lmos pan NPU lis http int can edi Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each
【CodeForces 129 B】Students and Shoelaces(拓撲排序)
end owin clu namespace 每次 font order 單獨 when Anna and Maria are in charge of the math club for junior students. When the club gathers tog
Codeforces Round #460 (Div. 2) D. Substring(拓撲排序)
題目連結 題意:有一個n個點m條邊的有向圖,每個節點有一個字母,路徑的權值是路徑上相同字母的最大個數。求最大的路徑權值。 思路:因為只有26個字母,所以直接假設其中一個字母為相同字母數最大的字母。列舉每一個字母,通過拓撲排序找到最大權值,取其中的最大值就是答
POJ 2367:Genealogical tree(拓撲排序)
ostream oge lis limit return ember rand output together Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Sub
HDU 6073 Matching In Multiplication(拓撲排序)
str graph pop using bsp sum rod 沒有 script Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/5242
Sorting It All Out(拓撲排序)
stream ecif nco define 根據 判斷 values ges cond 題目: An ascending sorted sequence of distinct values is one in which some form of a less-tha
HDU 2647 Reward(拓撲排序)
empty case acm size col .cn ble ger dem 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 題目: Problem Description Dandelion‘s uncle
杭電 1285 確定比賽名次(拓撲排序)
hdu -h 整數 click tro hit pro set 接下來 http://acm.hdu.edu.cn/showproblem.php?pid=1285
題解報告:hdu 2647 Reward(拓撲排序)
一個 may lan 應該 遇到 net acm http nes 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion‘s uncle is a boss of
HDU - 4324 Triangle LOVE(拓撲排序)
斷圖 如何判斷 targe con ack algorithm scanf 否則 rst https://vjudge.net/problem/HDU-4324 題意 每組數據一個n表示n個人,接下n*n的矩陣表示這些人之間的關系,輸入一定滿足若A不喜歡B則B一定喜歡A
圖論學習二之Topological Sort(拓撲排序)
src info directed com 遞歸 ica -- 遞歸版 拓撲 拓撲排序 Topological-Sort• 對一個有向無環圖G進行 拓撲排序, 是將G中所有 頂點排成一個線性序列, 使 對 於 圖 中 任 意 弧 <u,
HDOJ 2647(拓撲排序)
nbsp sort pan front push hdoj 2647 std cst class #include <iomanip> #include <cstdio> #include <cmath> #include <cs
HDOJ 2094(拓撲排序)
hdoj %d esp ems mat () include ack sin 1 #include <iomanip> 2 #include <iostream> 3 #include <cstdio> 4 #include &
HDOJ 3342(拓撲排序)
pan pac ack pop pso [] sizeof inb emp #include <iomanip> #include <iostream> #include <cstdio> #include <cmath>
hihoCoder - 1870 Jin Yong’s Wukong Ranking List (拓撲排序)(2018ICPC北京A)
時間限制:1000ms 單點時限:1000ms 記憶體限制:512MB 描述 Jin Yong was the most famous and popular Chinese wuxia (The one who fight bad people by his Wuko
08-圖8 How Long Does It Take (25 分)(拓撲排序) 中國大學MOOC-陳越、何欽銘-資料結構-2018秋
08-圖8 How Long Does It Take (25 分) Given the relations of all the activities of a project, you are supposed to find the earliest complet