QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#692938#5439. Meet in the MiddleLinkWishWA 5ms28184kbC++144.3kb2024-10-31 15:15:492024-10-31 15:15:58

Judging History

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

  • [2024-10-31 15:15:58]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:28184kb
  • [2024-10-31 15:15:49]
  • 提交

answer

//Linkwish's code
#include<bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define si inline
using namespace std;
typedef long long ll;typedef __int128 li;typedef long double ld;
typedef unsigned long long ull;typedef pair<int,int> pii;typedef pair<ll,ll> pll;
typedef const int ci;typedef const ll cl;ci iinf=INT_MAX;cl linf=LLONG_MAX;
template<typename T>si bool gmax(T &x,const T y){if(x<y)return x=y,true;return false;}
template<typename T>si bool gmin(T &x,const T y){if(y<x)return x=y,true;return false;}

namespace LinkWish{

	char buf[1<<16],*bp1,*bp2;
	si void Init(){if(bp1==bp2)bp2=(bp1=buf)+fread(buf,1,1<<15,stdin);}
	si char Texas(){return (Init(),bp1!=bp2)?*bp1++:EOF;}
	si void read(int &x){
		char ch=Texas();for(;ch<'0'||ch>'9';ch=Texas());
		for(x=0;ch>='0'&&ch<='9';ch=Texas())x=x*10+(ch^48);
	}
	char wrbuf[1<<26],*cur=wrbuf;
	char stk[60];int top;
	si void write(ll x){
		if(x==0)return *(cur++)='0',*(cur++)='\n',void();
		for(;x;x/=10)stk[++top]=x%10;
		for(;top;top--)*(cur++)=stk[top]+'0';
		*(cur++)='\n';
	}
	
	ci N=100005,M=500005;

	int n,m;

	ll ans[M];
	vector<pii> que[N];
	int col[N];

	struct Tree2{
		vector<pii> e[N];

		int dep[N],dfn[N],sign;
		ll dis[N];
		pii pf[17][N];

		si void add(int x,int y,int w){
			e[x].emplace_back(y,w);
			e[y].emplace_back(x,w);
		}
		void dfs(int x,int fa){
			pf[0][dfn[x]=++sign]=pii(dep[x]=dep[fa]+1,fa);
			for(pii i:e[x])if(i.fi!=fa)dis[i.fi]=dis[x]+i.se,dfs(i.fi,x);
		}
		si void init(){
			dfs(1,0);
			for(int i=1;i<17;i++)
				for(int j=1;j+(1<<i)-1<=n;j++)
					pf[i][j]=min(pf[i-1][j],pf[i-1][j+(1<<(i-1))]);
		}
		si int lca(int x,int y){
			if(x==y)return x;
			if((x=dfn[x])>(y=dfn[y]))swap(x,y);
			int k=__lg(y-(x++));
			return min(pf[k][x],pf[k][y-(1<<k)+1]).se;
		}
		si ll Dis(int x,int y){return dis[x]+dis[y]-2*dis[lca(x,y)];}

		si void Clear(){
			sign=0;
			for(int i=1;i<=n;i++)e[i].clear();
		}
	}T2;

	vector<pii> e[N];
	si void add(int x,int y,int w){
		e[x].emplace_back(y,w);
		e[y].emplace_back(x,w);
	}

	int sz[N];
	ll dis[N];
	bool vis[N];
	void getrt(int x,int fa,ci S,int &rt,vector<int> &all){
		int mx=0;sz[x]=1;
		all.push_back(x);
		for(pii i:e[x])
			if(!vis[i.fi]&&i.fi!=fa)
				getrt(i.fi,x,S,rt,all),sz[x]+=sz[i.fi],gmax(mx,sz[i.fi]);
		if(max(mx,S-sz[x])<=S/2)rt=x;
	}
	void getinfo(int x,int fa,ci c){
		col[x]=c,sz[x]=1;
		for(pii i:e[x])
			if(!vis[i.fi]&&i.fi!=fa)
				dis[i.fi]=dis[x]+i.se,getinfo(i.fi,x,c),sz[x]+=sz[i.fi];
	}

	void solve(int x,int S){
		if(S==1){
			for(pii i:que[x])gmax(ans[i.se],T2.Dis(x,i.fi));
			return ;
		}
		vector<int> cur;
		getrt(x,0,S,x,cur);
		dis[x]=0,col[x]=x;
		for(pii i:e[x])
			if(!vis[i.fi])
				dis[i.fi]=i.se,getinfo(i.fi,x,i.fi);

		int d1=0,d2=0,d3=0,d4=0,d5=0,d6=0;ll nv=-linf;
		for(int i:cur)if(gmax(nv,dis[i]+T2.dis[i]))d1=i;
		nv=-linf;for(int i:cur)if(gmax(nv,dis[i]+T2.Dis(d1,i)))d2=i;
		nv=-linf;for(int i:cur)if(col[i]!=col[d2]&&gmax(nv,dis[i]+T2.Dis(d1,i)))d3=i;

		nv=-linf;for(int i:cur)if(col[i]!=col[d1]&&gmax(nv,dis[i]+T2.dis[i]))d4=i;
		nv=-linf;for(int i:cur)if(gmax(nv,dis[i]+T2.Dis(d4,i)))d5=i;
		nv=-linf;for(int i:cur)if(col[i]!=col[d5]&&gmax(nv,dis[i]+T2.Dis(d4,i)))d6=i;
		vector<int> cd={d1,d2,d3,d4,d5,d6};
		sort(cd.begin(),cd.end()),cd.erase(unique(cd.begin(),cd.end()),cd.end());

		for(int i:cur){
			for(pii j:que[i]){
				ll v=0;
				for(int k:cd)
					if(col[k]!=col[i])
						gmax(v,dis[k]+T2.Dis(j.fi,k));
				gmax(ans[j.se],dis[i]+v);
			}
		}

		vis[x]=true;
		for(pii i:e[x])if(!vis[i.fi])solve(i.fi,sz[i.fi]);
	}

	si void Clear(){
		for(int i=1;i<=n;i++)e[i].clear(),vis[i]=false;
		T2.Clear();
		for(int i=1;i<=n;i++)que[i].clear();
		for(int i=1;i<=m;i++)ans[i]=0;
	}

	si void solve(){
		read(n),read(m);
		for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),add(x,y,z);
		for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),T2.add(x,y,z);
		for(int i=1,x,y;i<=m;i++)read(x),read(y),que[x].emplace_back(y,i);

		T2.init();
		solve(1,n);

		for(int i=1;i<=m;i++)write(ans[i]);

		Clear();
	}

	void mian(){
solve();
	}
}

signed main(){
	#ifndef ONLINE_JUDGE
	assert(freopen("move.in","r",stdin));
	assert(freopen("move.out","w",stdout));
	#endif
	LinkWish::mian();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 28184kb

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:

wrong answer Answer contains longer sequence [length = 4], but output contains 0 elements