QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286490#5439. Meet in the Middleushg8877WA 120ms37624kbC++144.8kb2023-12-17 22:46:032023-12-17 22:46:03

Judging History

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

  • [2023-12-17 22:46:03]
  • 评测
  • 测评结果:WA
  • 用时:120ms
  • 内存:37624kb
  • [2023-12-17 22:46:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
const int MAXN=1e5+5;
int n,q;
struct edge{
	int v,w;
	edge(int _v=0,int _w=0){v=_v,w=_w;} 
};
struct query{
	int v,id;
	query(int _v=0,int _i=0){v=_v,id=_i;} 
};
struct node{
	ll w,c,fr;
	node(ll _w=-1e18,ll _c=-1,ll _f=0){w=_w,c=_c,fr=_f;} 
};
vector<edge> edg1[MAXN],edg2[MAXN];
vector<int> vir[MAXN];
vector<query> que[MAXN];
ll dep2[MAXN],dep1[MAXN],in[MAXN],tot;
ll ex[MAXN];// 该点属性 
node b1[MAXN],b2[MAXN];// 维护最优点 
int siz[MAXN],maxx[MAXN],col[MAXN],son,tsiz;
ll ans[MAXN<<3];
bool vis[MAXN];
struct LCA_tech{
int a[20][MAXN<<1],lg[MAXN<<1];
int chk(int x,int y){
	return dep2[x]<dep2[y]?x:y;
}
void build(){
	for(int i=2;i<(MAXN<<1);i++) lg[i]=lg[i>>1]+1; 
	for(int i=1;i<20;i++)
		for(int j=1;j+(1<<i)-1<=tot;j++)
			a[i][j]=chk(a[i-1][j],a[i-1][j+(1<<i-1)]);
}
int lca(int x,int y){
	x=in[x],y=in[y];
	if(x>y) swap(x,y);
	int k=lg[y-x+1];
	return chk(a[k][x],a[k][y-(1<<k)+1]);
}
}T2;
void dfs(int u,int fa){
	T2.a[0][++tot]=u;in[u]=tot;
	for(auto e:edg2[u]) if(e.v!=fa){
		dep2[e.v]=dep2[u]+e.w;
		dfs(e.v,u);
		T2.a[0][++tot]=u;
	}
}// init for T2
void get_size(int u,int fa){
	maxx[u]=siz[u]=1;
	for(auto e:edg1[u]) if(e.v!=fa&&!vis[e.v]){
		get_size(e.v,u);
		maxx[u]=max(maxx[u],siz[e.v]);
		siz[u]+=siz[e.v];
	}
	maxx[u]=max(maxx[u],tsiz-siz[u]);
	if(maxx[u]<maxx[son]) son=u;
}
void build_vir(vector<int> a){
	static int st[MAXN],top;
	st[top=1]=1;
	sort(a.begin(),a.end(),[&](int x,int y){return in[x]<in[y];});
	a.erase(unique(a.begin(),a.end()),a.end());
	auto link=[&](int u,int v){
		vir[u].push_back(v);
		vir[v].push_back(u);
	};
	for(int u:a) if(u!=1){
		int p=T2.lca(u,st[top]);
		while(st[top]!=p){
			if(dep2[st[top-1]]>=dep2[p]) link(st[top],st[top-1]),top--;
			else{
				link(st[top],p);
				st[top]=p;
			}
		}
		st[++top]=u;
	}
	while(top>1) link(st[top-1],st[top]),top--; 
}
void dfz(int r){
	vis[r]=true;
	vector<int> nd;
	auto dfs=[&](auto self,int u,int fa)->void{
		nd.push_back(u);ex[u]=dep1[u];
		for(query q:que[u]) nd.push_back(q.v);
		for(edge e:edg1[u]) if(e.v!=fa&&!vis[e.v]){
			dep1[e.v]=dep1[u]+e.w;
			col[e.v]=(u==r?e.v:col[u]);
			self(self,e.v,u);
		}
	};dep1[r]=0;dfs(dfs,r,0);col[r]=r;
//	cout<<"BUILD VIR: ";
//	for(int j:nd) cout<<j<<' ';cout<<endl;
	build_vir(nd);
//	cout<<"VIRTURE TREE "<<r<<endl;
//	for(int i=1;i<=n;i++){
//		for(int j:vir[i]) if(i<j) cout<<"EDGE "<<i<<' '<<j<<endl;
//	}
//	cout<<"VALUE: "<<ex[1]<<' '<<ex[2]<<' '<<dep1[1]<<' '<<dep1[2]<<endl;
	auto push=[&](int u,node b){
		if(b1[u].fr==b.fr||b2[u].fr==b.fr) return;
		if(b1[u].w<b.w){
			if(b1[u].c!=b.c) b2[u]=b1[u];
			b1[u]=b;
		}else if(b1[u].c!=b.c&&b2[u].w<b.w) b2[u]=b;
	};
	auto dfs1=[&](auto self,int u,int fa)->void{
		b1[u]=node(ex[u],col[u],u);b2[u]=node();ex[u]=-1e18;
		for(int v:vir[u])if(v!=fa){
			self(self,v,u);
			push(u,node(b1[v].w+dep2[v]-dep2[u],b1[v].c,b1[v].fr));
			push(u,node(b2[v].w+dep2[v]-dep2[u],b2[v].c,b2[v].fr));
		}
	};dfs1(dfs1,1,0);// 自下而上 
//	cout<<"FIRST"<<endl;
//	cout<<b1[1].w<<' '<<b1[1].c<<endl;
//	cout<<b1[2].w<<' '<<b1[2].c<<' '<<dep1[1]<<endl;
//	cout<<col[1]<<' '<<col[2]<<' '<<dep1[r]<<endl;
	auto dfs2=[&](auto self,int u,int fa)->void{
		for(int v:vir[u])if(v!=fa){
			push(v,node(b1[u].w+dep2[v]-dep2[u],b1[u].c,b1[u].fr));
			push(v,node(b2[u].w+dep2[v]-dep2[u],b2[u].c,b2[u].fr));
			self(self,v,u);
		}
		vir[u].clear();
	};dfs2(dfs2,1,0);// 自上而下 
//	cout<<b1[1].w<<' '<<b1[1].c<<endl;
//	cout<<b1[2].w<<' '<<b1[2].c<<' '<<dep1[1]<<endl;
//	cout<<col[1]<<' '<<col[2]<<' '<<dep1[r]<<endl;
	auto dfs3=[&](auto self,int u,int fa)->void{
		for(query q:que[u]){
//			cout<<"GO! "<<u<<' '<<q.v<<endl;
			if(u==r||b1[q.v].c!=col[u]) ans[q.id]=max(ans[q.id],dep1[u]+b1[q.v].w);
			if(u==r||b2[q.v].c!=col[u]) ans[q.id]=max(ans[q.id],dep1[u]+b2[q.v].w);
		} 
		for(edge e:edg1[u]) if(e.v!=fa&&!vis[e.v]) self(self,e.v,u);
	};dfs3(dfs3,r,0);
	for(edge e:edg1[r]) if(!vis[e.v]){
		tsiz=siz[e.v];son=0;
		get_size(e.v,0);get_size(son,0);
		dfz(son);
	}
}
int main(){
	ios::sync_with_stdio(false);
	memset(ex,-0x3f,sizeof(ex));
	cin>>n>>q;
	for(int i=1;i<n;i++){
		int u,v,w;cin>>u>>v>>w;
		edg1[u].push_back(edge(v,w));
		edg1[v].push_back(edge(u,w));
	}
	for(int i=1;i<n;i++){
		int u,v,w;cin>>u>>v>>w;
		edg2[u].push_back(edge(v,w));
		edg2[v].push_back(edge(u,w));
	}
	for(int i=1;i<=q;i++){
		int u,v;cin>>u>>v;
		que[u].push_back(query(v,i));
	} 
	dfs(1,0);T2.build();
//	cout<<"DEEP: "<<dep2[1]<<' '<<dep2[2]<<endl;
	maxx[0]=1e9;tsiz=n;
	get_size(1,0);get_size(son,0);dfz(son);
	for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
}
/*
2 4
1 2 1
1 2 3
1 1
1 2
2 1
2 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 27048kb

input:

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

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 2ms
memory: 26524kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 2ms
memory: 26968kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 120ms
memory: 37624kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

647838384844
362326016102
387098802897
637066393138
456072330399
645842915960
641537859258
400174767683
459407279331
573316121269
674370549986
434572336505
435282975629
620172136910
553499885160
526743824759
471341504556
689550247042
549161302822
498449365451
396706495314
393003299495
701575393972
6...

result:

wrong answer 2nd numbers differ - expected: '626539793176', found: '362326016102'