CF915D Almost Acyclic Graph (thinking + topology)

If you want to determine whether the DAG, using topological sorting is a good choice, but this problem can delete an edge

If really silly difficult to enumerate Erase the top

We have to think, for the border erase, in fact, the degree of -1, and we know that can delete the complete topology, indicating success, so long as the enumeration point on the topology of the operation run again, will be able to AC

This transformation is very interesting, we have to think correctness, for a first ring, certainly to a certain situation because all of penetration are not 0, so the increase is not into the queue. As for the point on the ring, would then finish the rest edges on the topology of the ring, the penetration of -1, corresponding to

Ignoring the edge on the ring, on the success of the break, and if there are two rings on this point, it is still useless, because there is less complete penetration

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#define ull unsigned long long
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=1e5+10;
int in[N],in1[N];
vector<int> g[N];
int n;
bool topo(){
    queue<int> q;
    int i;
    int sum=0;
    for(i=1;i<=n;i++){
        if(!in[i])
            q.push(i);
    }
    while(q.size()){
        int t=q.front();
        q.pop();
        sum++;
        for(i=0;i<g[t].size();i++){
            int j=g[t][i];
            in[j]--;
            if(!in[j])
                q.push(j);
        }
    }
    if(sum==n)
        return true;
    return false;
}
int main(){
    int i;
    int m;
    cin>>n>>m;
    for(i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        in[v]++;
        in1[v]++;
    }
    if(topo()){
        cout<<"YES"<<endl;
        return 0;
    }
    else{
        for(i=1;i<=n;i++){
            memcpy(in,in1,sizeof in);
            if(in[i]>=1){
                in[i]--;
                if(topo()){
                    cout<<"YES"<<endl;
                    return 0;
                }
            }
        }
    }
    cout<<"NO"<<endl;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/ctyakwf/p/12633734.html