NC232165. Rain_w Loves Skirt
描述
输入描述
The first line contains two integers --the number of shops in the city and the number of skirt types.
Then lines follow, the -th line contains three integers denoting -th road which connecting two shops.
Then a line follows contains integers, the -th integer denoting the -th shop sells -th skirt.
Then follows an integer -- the number of days
Then line follow, the -th line contain two integers , denoting the two types of skirts rain_w chose on the -th day.
输出描述
Print lines, the -th line contains an integer denoting how long rain_w will walk with skirt on -th day.
示例1
输入:
8 3 1 2 1 2 3 1 3 4 1 4 5 1 3 6 1 6 7 1 7 8 1 1 2 1 2 1 2 1 3 2 2 1 3 2
输出:
3 4
C++(clang++ 11.0.1) 解法, 执行用时: 520ms, 内存消耗: 24740K, 提交时间: 2022-10-15 22:10:11
#include<bits/stdc++.h> #define For(i,a,b) for(int i=a;i<=b;++i) using namespace std; typedef long long ll; const ll INF=1e16; const int N=5e4+5; ll dis[N];int dpth[N]; int od[N];vector<int>ov[N];vector<ll>vl[N]; int dfn[N*2][20],dnl[N],dnr[N],tot; int lg2[N*2]; int n,m,k; vector<int>col[N];int D1[N],D2[N]; int bel[N]; #define pb push_back #define x ov[p][i] void dfs(int p){ dfn[++tot][0]=p,dnl[p]=dnr[p]=tot; For(i,0,od[p]-1){ if(dpth[x])continue; dis[x]=dis[p]+vl[p][i],dpth[x]=dpth[p]+1; dfs(x); dfn[++tot][0]=p,dnr[p]=tot; } } #define dmin(a,b) (dpth[a]<dpth[b]?a:b) int lca(int p1,int p2){ int l=min(dnl[p1],dnl[p2]); int r=max(dnr[p1],dnr[p2]); int lg=lg2[r-l+1]-1; r=max(l,r-(1<<lg)+1); // cerr<<l<<" "<<r<<" "<<lg<<"\n"; return dmin(dfn[l][lg],dfn[r][lg]); } ll gdis(int p1,int p2){ int a=lca(p1,p2); return dis[p1]+dis[p2]-dis[a]-dis[a]; } map<int,ll>mp[N]; void prc(){ int q;cin>>q; while(q--){ int c1,c2;cin>>c1>>c2; if(col[c1].size()<col[c2].size()||(col[c1].size()==col[c2].size()&&c2>c1)) swap(c1,c2); if(mp[c1].count(c2)){cout<<mp[c1][c2]<<"\n";continue;} ll ans=INF; for(auto p:col[c2]){ ans=min(ans,max({gdis(p,D1[c1]),gdis(p,D2[c1]),gdis(p,D1[c2]),gdis(p,D2[c2])})); } mp[c1][c2]=ans; cout<<ans<<"\n"; } } void init(){ dpth[1]=1,dfs(1); for(int i=1;i<=tot;i<<=1) lg2[i]++; For(i,1,tot) lg2[i]+=lg2[i-1]; For(i,1,19){ int len=(1<<(i-1)); For(j,1,tot){ int p2=min(tot,j+len); dfn[j][i]=dmin(dfn[j][i-1],dfn[p2][i-1]); } } For(i,1,n)if(gdis(1,i)>gdis(1,D1[bel[i]])) D1[bel[i]]=i; For(i,1,n)if(gdis(D1[bel[i]],i)>gdis(D2[bel[i]],D1[bel[i]])) D2[bel[i]]=i; } void input(){ ios::sync_with_stdio(0),cin.tie(0); cin>>n>>k;m=n-1; For(i,1,m){ int p1,p2;ll v;cin>>p1>>p2>>v; ++od[p1],ov[p1].pb(p2),vl[p1].pb(v); ++od[p2],ov[p2].pb(p1),vl[p2].pb(v); } For(i,1,n)cin>>bel[i],col[bel[i]].pb(i),D1[bel[i]]=D2[bel[i]]=i; } signed main(){ // freopen("in.txt","r",stdin); input(); init(); prc(); return 0; }