【Educational Codeforces Round 36 D】 Almost Acyclic Graph

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

找到任意一个环。 然后枚举删掉其中的某一条边即可。 (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包括这条边,那么就可以,否则,某个环不包括那也没办法,自然就无解了。 这样枚举的边的数目最多是500(环最多N条边) 然后复杂度就是500*10万了。 足够过了

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 5e2;

int n,m;
vector <int> g[N+10],path;
int bo[N+10];

bool dfs1(int x){
	bo[x] = 1;
	for (int y:g[x]){
		if (bo[y]==0 && dfs1(y)){
			path.push_back(x);
			return true;
		}
		else
			if (bo[y]==1) {
				path.push_back(y);
				path.push_back(x);
				return true;
			}
	}
	bo[x] = 2;
	return false;
}

bool dfs2(int x,int a,int b){
	bo[x] = 1;
	for (int y:g[x]){
        if (x==a && y==b) continue;
		if (bo[y]==0 && dfs2(y,a,b)) return true;
		else
			if (bo[y]==1) return true;
	}
	bo[x] = 2;
	return false;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	cin >> n >> m;
	for (int i = 1;i <= m;i++){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
	}

	bool loop1 = false;

	for (int i = 1;i <= n;i++)
		if (bo[i]==0 && dfs1(i)){
			loop1 = true;
			break;
		}

	if (!loop1) return cout<<"YES"<<endl,0;

	reverse(path.begin(),path.end());

	for (int i = 0;i <(int) path.size()-1;i++){
		int x = path[i],y = path[i+1];
        loop1 = false;
        memset(bo,0,sizeof bo);
        for (int j = 1;j <= n;j++)
            if (bo[j]==0 && dfs2(j,x,y)){
                loop1 = true;
                break;
            }
        if (!loop1) return cout <<"YES"<<endl,0;
	}
    cout <<"NO"<<endl;
	return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/8282387.html