奇怪的编译错误 求调&解释
查看原帖
奇怪的编译错误 求调&解释
549521
boy♂Next♂dooor楼主2024/7/21 11:41

题目 已知是c数组引起的编译错误(因为要是我用pre的就不会报错),但我不知道这哪里有问题

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int n,k,p,q,ans;
struct edge{
	int to,w;
};
vector<edge> g[N];
int d[N],pre[N];
int c[N][N];
void add(int x,int y,int z)
{
	g[x].push_back({y,z});
}
void dfs(int x,int from)
{   
	//t++;
	//cout<<g[x].size()<<endl;
	for(int i=0;i<g[x].size();i++)
	{
		int v=g[x][i].to;
		//cout<<"v:"<<v<<" from:"<<from<<endl;
		if(v==from) continue;
		d[v]=d[x]+1;
		//cout<<d[v]<<endl;
		c[v][x]=c[x][v]=1;//pre[v]=x;
		if(d[v]>d[p]) p=v;
		dfs(v,x);
	}
}
void dp(int x,int from)
{
	for(int i=0;i<g[x].size();i++)
	{
		int v=g[x][i].to;
		int w=g[x][i].w;
		//cout<<w<<endl;
		if(v==from) continue;
		dp(v,x);
		/*if(ans<d[v]+d[x]+w)
		{
		    cout<<"v,x:"<<v<<","<<x<<endl;
		    cout<<"  "<<d[v]+d[x]+w<<endl;
		}*/
		ans=max(ans,d[v]+d[x]+w);
		d[x]=max(d[x],d[v]+w);
	}
}
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n-1;i++)
	{
		int x,y;
		cin>>x>>y;
		add(x,y,1);
		add(y,x,1);
	}
	/*for(int i=1;i<=n;i++)
	//{
	//	cout<<g[i].size()<<" "; 
	}*/
	
	dfs(1,0);
	//cout<<"p:"<<p<<endl;
	//cout<<t<<endl; 1
	//for(int i=1;i<=n;i++) cout<<d[i]<<" ";
	//cout<<endl;
	d[p]=0;
	memset(pre,0,sizeof(pre));

	dfs(p,0);	
	//cout<<"q:"<<p<<endl;
	//for(int i=1;i<=n;i++) cout<<d[i]<<" ";
	//cout<<endl;
	//cout<<d[p]<<endl;
	
	//int t=0;
	for(int i=1;i<=n;i++)
	{
	    
		for(int j=0;j<g[i].size();j++)
		{
		    //t++;
			if(c[i][g[i][j].to])//if(pre[i]==g[i][j].to)
			{
				g[i][j].w=-1;
				//cout<<"pre"<<i<<" :"<<pre[i]<<endl;
			}
		}
	}
    //cout<<"t:"<<t<<endl;
	int	l1=d[p];
	//cout<<l1<<endl;
    if(k==1)
	{
		cout<<2*(n-1)-(l1-1)<<endl;
		return 0;
	}
	memset(d,0,sizeof(d));
	dp(1,0);
	int l2=ans;
	//cout<<l2<<endl;
	cout<<2*(n-1)-(l1-1)-(l2-1);
 	return 0;
}

2024/7/21 11:41
加载中...