QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#692299#5439. Meet in the MiddlewjwweiweiML 0ms0kbC++142.6kb2024-10-31 14:17:332024-10-31 14:17:34

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 14:17:34]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-10-31 14:17:33]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
typedef long long ll;
bool Mst;
const int N=1e5+5;
int n,q;
int T;
struct Tree{
	int head[N],to[N<<1],nxt[N<<1],tot,fa[N],dep[N];
	int son[N],siz[N],top[N];
	int fl[N<<1];ll dis[N];
	inline void clear(){
		for(int i=0;i<=n;i++){
			head[i]=dis[i]=dep[i]=son[i]=siz[i]=top[i]=0;
		}
		tot=0;
	}
	inline void add(int u,int v,int w){nxt[++tot]=head[u],to[tot]=v,fl[tot]=w,head[u]=tot;}
	void dfs(int u,int fat){
		dep[u]=dep[fat]+1;
		fa[u]=fat;
		siz[u]=1;
		for(int i=head[u];i;i=nxt[i]){
			int v=to[i];
			if(v==fat)continue;
			dis[v]=dis[u]+fl[i];
			dfs(v,u);
			siz[u]+=siz[v];
			if(siz[son[u]]<siz[v])son[u]=v;
		}
	}
	void dfs2(int u,int topu){
		top[u]=topu;
		if(!son[u])return ;
		dfs2(son[u],top[u]);
		for(int i=head[u];i;i=nxt[i]){
			int v=to[i];
			if(v==fa[u]||v==son[u])continue;
			dfs2(v,v);
		}
	}
	inline int lca(int u,int v){
		while(top[u]^top[v]){
			if(dep[top[u]]>dep[top[v]])u=fa[top[u]];
			else v=fa[top[v]];
		}
		return dep[u]<dep[v]?u:v;
	}
	inline ll gdis(int u,int v){return dis[u]+dis[v]-2ll*dis[lca(u,v)];}
}T1,T2;
mt19937 rnd(time(0));
set<int>rea;
int gt[N],cnt;
bool Med;
int main(){
//	cerr<<1.0*(&Mst-&Med)/1024/1024<<"\n";
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;
		T1.clear(),T2.clear();
		int u,v,w;
		for(int i=1;i<n;i++)cin>>u>>v>>w,T1.add(u,v,w),T1.add(v,u,w);
		for(int i=1;i<n;i++)cin>>u>>v>>w,T2.add(u,v,w),T2.add(v,u,w);
		T1.dfs(1,0),T2.dfs(1,0);T1.dfs2(1,1),T2.dfs2(1,1);
		cin>>q;
		if(n<=3000&&q<=3000){
			for(int i=1;i<=q;i++){
				cin>>u>>v;
				ll ans=-1;
//				int id=0;
				for(int j=1;j<=n;j++){
					ll now=T1.gdis(u,j)+T2.gdis(v,j);
					ans=max(ans,now);
//					if(ans==now)id=j;
				}
				cout<<ans<<"\n";
//				rea.insert(id);
//				if(rea.size()>4)cerr<<rea.size()<<"\n";
			}
			
		}
		else{
			rea.clear();
			int t=100;
			int las=0,pp=0;
			for(int i=1;i<=t;i++){
				int u=rnd()%n+1,v=rnd()%n+1;
				ll ans=-1;
				int id=1;
				for(int j=1;j<=n;j++){
					ll now=T1.gdis(u,j)+T2.gdis(v,j);
					ans=max(ans,now);
					if(ans==now)id=j;
				}
				rea.insert(id);
				int now=rea.size();
				if(las==now)pp++;
				else las=now,pp=0;
//				if(pp>10&&now==2)break;
				if(rea.size()==4)break;
			}
			cnt=0;
			for(auto v:rea)gt[++cnt]=v;
			for(int i=1;i<=q;i++){
				cin>>u>>v;
				ll ans=-1;
				for(int k=1;k<=cnt;k++){
					int j=gt[k];
					ll now=T1.gdis(u,j)+T2.gdis(v,j);
					ans=max(ans,now);
				}
				cout<<ans<<"\n";
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:


result: