[Crow Contest]From MLE to TLE 晚安

//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int> > graph(105,vector<int>(105,0));
vector<int> in(105,0);//入度
vector<int> path(105,0);
vector<int> cnt(105,0); 
vector<int> first(105,0);
int vis[1005];
//爆栈了咕咕咕咕 
int n,m,f=0,row=0;
//topo
void dfs(int depth){
	if(depth==n){
		row++;
		//扫一遍
	    if(!f){
	    	f=1;
	    	for(int i=0;i<n;i++)	first[i] = path[i];
		}
		for(int i=0;i<n;i++){
			if(first[i] == path[i]){
				cnt[i]++;
			}
		}
	}else{
		for(int i=1;i<=n;i++){
			if(!in[i]&&!vis[i]){//入度为0
				 for(int j=1;j<=n;j++){//刷新入度 
				 	if(graph[j][i]){
				 		in[j]--;
					 }
				 }
				 vis[i] = 1;
				 path[depth] = i;
				 dfs(depth+1);
				 for(int j=1;j<=n;j++){
				 	if(graph[j][i]){
				 		in[j]++;
					 }
				 }
				 vis[i] = 0;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	while(m--){
		int a,b;
		cin>>a>>b;
		graph[a][b] = 1;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(graph[i][j]){
				in[i]++;
			}
		}
	}
	dfs(0);
	int res=0;
	for(int j=0;j<n;j++){
		if(cnt[j]==row)
		res++;
	}
	cout<<res;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Csdn_jey/article/details/88983843
tle
MLE
今日推荐