求助壶关
查看原帖
求助壶关
1137574
qwqawaer楼主2024/11/20 16:14

糖丸了,我听说n方能过,只有一个点T,三个MLE

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+10,Mod=10007;
struct Node{
	int next,to;
}a[N];
int sz[N],head[N],tot,w[N],n,m,f[N],sum,id,step,s[N],tt,ans=-1e18;
map<pair<int,int>,int> vis;
void add(int u,int v){
	a[++tot].to=v;
	a[tot].next=head[u];
	head[u]=tot;	
}
vector<int> res;
void dfs(int u,int fa){
	for(int i=head[u];i;i=a[i].next){
		int v=a[i].to;
		if(v==fa)continue ;
		s[++step]=v;
		//cout<<"step "<<step<<" from "<<u<<" to "<<v<<" w "<<w[s[step]]<<endl;
		if(step-2>=0&&s[step-2]&&!vis[{s[step-2],s[step]}]&&!vis[{s[step],s[step-2]}]){
		//	cout<<"from "<<s[step]<<" to "<<s[step-2]<<" w "<<w[s[step]]<<endl;
			ans=max(ans,w[s[step-2]]*w[s[step]]);
			res.push_back(w[s[step-2]]*w[s[step]]%Mod);
			//res.push_back(w[s[step-2]]*w[s[step]]%Mod);
			vis[{s[step-2],s[step]}]=1;
			vis[{s[step],s[step-2]}]=1;
		}
		dfs(v,u);
		step--;
	}
}
signed main(){
	//freopen("P1351_2.in","r",stdin);
	cin>>n;
	//s[++step]=1;
	for(int i=1;i<=n-1;i++){
		int u,v;
		cin>>u>>v;
		add(u,v);
		add(v,u);
	}
	for(int i=1;i<=n;i++){
		cin>>w[i];
	}
	for(int i=1;i<=n;i++){
		step=0;
		s[++step]=i;
		dfs(i,0);
	}
	for(int i=0;i<res.size();i++){
	//	cout<<res[i]<<" ";
		sum+=res[i];
		sum%=Mod;
		sum+=res[i];
		sum%=Mod;
	}
	//cout<<endl;
	cout<<ans<<" "<<sum;
	return 0;
}
2024/11/20 16:14
加载中...