求助
查看原帖
求助
489725
方块鼠楼主2024/4/6 16:29
#include<iostream>
using namespace std;
int mapp[505];//child->father
bool fa[505];
int mapp2[505][505];//howmuchzhijiechild
int get1[505];
int get[505];//all child?
int mapp1[505][505];//?
int mapp3[505][505];//tu
int maxn=-100;
bool spot[505];
bool mapp4[505][505];
int f[2000][2000];
int now=1;
int ans=0;
int n,p;
void dfs(int prv,int father){
	if(get1[father]==0){
		get[father]=1;
		mapp3[prv][father]=mapp3[father][prv]=1;
		return ;
	}
	for(int i=1;i<=get1[father];i++){
		dfs(father,mapp2[father][i]);
		get[father]+=get[mapp2[father][i]];
		mapp3[father][mapp2[father][i]]=mapp3[mapp2[father][i]][father]=get[mapp2[father][i]]+1;
	}
}
int main(){
	int x,y;
//	freopen("epidemic.in","r",stdin);
//	freopen("epidemic.out","w",stdout);
	cin>>n>>p;
	for(int i=1;i<=p;i++){
		cin>>x>>y;
		spot[x]=spot[y]=true;
		get1[x]++;
		mapp2[x][get1[x]]=y;
		mapp[y]=x;
		fa[x]=true;
		mapp4[x][y]=true;
		mapp4[y][x]=true;
	}
//	for(int i=1;i<=n;i++){
//		if(fa[i]==false){
//			get++;
//			str[get]=i;
//		}
//	}
	for(int i=1;i<=n;i++){
		dfs(0,i);
	}
	mapp[1]=1;
	int x1,y1;
	for(int i=1;i<=n;i++){
		maxn=-100;
		if(spot[i]==false||spot[mapp[i]]==false){
			spot[i]=false;
			continue;
		}
		ans++;
		for(int j=1;j<=get1[i];j++){
	//		if(i==j||spot[j]==false){
	//				continue;
	//			}
	//			if(spot[mapp[j]]==false){
	//				spot[j]=false;
	//			}
	//			if(mapp4[i][j]!=true){
	//				continue;
	//			}
	//			if(mapp3[i][j]>maxn||mapp3[j][i]>maxn){
	//				maxn=mapp3[i][j];
	//				x1=i;
	//				y1=j;
	//			}
			int ch=mapp2[i][j];
			if(spot[ch]==false){
				continue;
			}
			if(spot[mapp[ch]]==false){
				spot[ch]=false;
				continue;
			}
			if(mapp3[i][ch]>maxn||mapp3[ch][i]>maxn){
				maxn=mapp3[i][ch];
				x1=i;
				y1=ch;
			}
		}
		spot[y1]=false;
	}
	cout<<ans<<endl;
}

奇异贪心,也不知道当时怎么想的......

2024/4/6 16:29
加载中...