topic:
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).
The 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 from u to v).
If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.
The meaning of the question: Given a directed graph, if at most one edge is arbitrarily deleted from it. Ask if it is possible to finally make the graph acyclic.
Idea: We know topological sorting to judge whether there is a cycle, that is, to continuously delete the points whose in-degree is 0 (only pay attention to the in-degree of the node when deleting!), and finally compare the relationship between the number of deletions and the point.
In this problem, at most one edge is deleted, and the change caused by deleting an edge is the in-degree of a point minus one. So we can enumerate the in-degree minus one of each point, and then run topological sort to determine whether there is still a ring.
Code:
#include<bits/stdc++.h> #define sd(x) scanf("%d",&x) #define ss(x) scanf("%s",x) #define sc(x) scanf("%c",&x) #define sf(x) scanf("%f",&x) #define slf(x) scanf("%lf",&x) #define slld(x) scanf("%lld",&x) #define me(x,b) memset(x,b,sizeof(x)) #define pd(d) printf("%d\n",d); #define plld(d) printf("%lld\n",d); #define eps 1.0E-8 // #define Reast1nPeace typedef long long ll; using namespace std; const int maxn = 510; int n,m; vector<int> G[maxn]; int in1[maxn]; int in2[maxn]; bool topsort(){ queue<int> q; for(int i = 1 ; i<=n ; i++){ if(!in2[i]) q.push(i); } int sum = 0; while(!q.empty()){ int now = q.front(); q.pop(); sum++; for(int i = 0 ; i<G[now].size() ; i++){ int to = G[now][i]; in2[to]--; if(!in2[to]) q.push(to); } } return sum==n; } int main(){ #ifdef Reast1nPeace freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin>>n>>m; for(int i = 1 ; i<=m ; i++){ int u,v; cin>>u>>v; G[u].push_back(v); in1[v]++; } topsort(); memcpy(in2,in1,sizeof(in1)); if(topsort()){ cout<<"YES"<<endl; } else{ bool flag = 0; for(int i = 1 ; i<=n ; i++){ if(in1[i]){ memcpy(in2,in1,sizeof(in1)); in2 [i] -; if(topsort()){ flag = 1; break; } } } if(flag){ cout<<"YES"<<endl; } else{ cout<<"NO"<<endl; } } return 0; }