QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373506 | #5439. Meet in the Middle | Graphcity | WA | 19ms | 18432kb | C++20 | 3.8kb | 2024-04-01 19:29:40 | 2024-04-01 19:29:41 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=1e5,Maxk=5e5;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,X,Y,id[Maxn+5]; ll ans[Maxk+5],dep[Maxn+5];
int vis[Maxn+5],siz[Maxn+5],maxs[Maxn+5],rt,sum;
vector<pair<int,int>> v[Maxn+5];
vector<array<int,3>> w[Maxn+5];
struct Tree
{
int fa[Maxn+5],st[Maxn+5][20],dfn[Maxn+5],cur; ll dis[Maxn+5];
vector<pair<int,int>> v[Maxn+5];
inline int GetID(int x,int y) {return dfn[x]<dfn[y]?x:y;}
inline void dfs(int x,int f)
{
fa[x]=f,dfn[x]=++cur,st[cur][0]=f;
for(auto [y,z]:v[x]) if(y!=f) dis[y]=dis[x]+z,dfs(y,x);
}
inline int LCA(int x,int y)
{
if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y);
int len=__lg(y-x++); return GetID(st[x][len],st[y-(1<<len)+1][len]);
}
inline ll Dis(int x,int y) {return dis[x]+dep[x]+dis[y]+dep[y]-2*dis[LCA(x,y)];}
inline void Insert(int p)
{
if(X==-1) {X=p; return;} if(Y==-1) {Y=p; return;}
ll a=Dis(X,Y),b=Dis(X,p),c=Dis(Y,p);
if(b>a && b>=c) Y=p; else if(c>a && c>b) X=p;
}
inline void Init()
{
For(i,1,n-1)
{
int a=read(),b=read(),c=read();
v[a].emplace_back(b,c),v[b].emplace_back(a,c);
} dfs(1,0);
For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
st[i][j]=GetID(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
} T;
inline void GetRt(int x,int f)
{
siz[x]=1,maxs[x]=0; for(auto [y,z]:v[x]) if(y!=f && !vis[y])
GetRt(y,x),siz[x]+=siz[y],maxs[x]=max(maxs[x],siz[y]);
maxs[x]=max(maxs[x],sum-siz[x]);
if(maxs[x]<maxs[rt]) rt=x;
}
inline void dfs(int x,int f,ll d,int p) {dep[x]=d,id[x]=p; for(auto [y,z]:v[x]) if(y!=f && !vis[y]) dfs(y,x,d+z,p);}
inline void dfs1(int x,int f) {T.Insert(x); for(auto [y,z]:v[x]) if(y!=f && !vis[y]) dfs1(y,x);}
inline void Work(int x,vector<array<int,3>> qr)
{
dep[x]=0,id[x]=x,X=x,Y=-1; for(auto [y,z]:v[x]) if(!vis[y])
dfs(y,x,z,y),vector<array<int,3>>().swap(w[y]);
for(auto i:qr) w[id[i[0]]].push_back(i);
for(auto [y,z]:v[x]) if(!vis[y])
{
for(auto [a,b,id]:w[y])
{
if(X!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,X));
if(Y!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,Y));
} dfs1(y,x);
}
for(auto [a,b,id]:w[x])
{
if(X!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,X));
if(Y!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,Y));
}
X=x,Y=-1,reverse(v[x].begin(),v[x].end());
for(auto [y,z]:v[x]) if(!vis[y])
{
for(auto [a,b,id]:w[y])
{
if(X!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,X));
if(Y!=-1) ans[id]=max(ans[id],dep[a]-dep[b]+T.Dis(b,Y));
// printf("%d: %d %d %lld %lld %lld\n",x,X,Y,dep[a],dep[b],dep[Y]);
} dfs1(y,x);
}
}
inline void dfs(int x,vector<array<int,3>> qr)
{
vis[x]=1; Work(x,qr); for(auto [y,z]:v[x]) if(!vis[y])
rt=0,sum=siz[y],GetRt(1,0),GetRt(rt,0),dfs(rt,w[y]);
}
int main()
{
// freopen("1.in","r",stdin);
n=read(),m=read();
For(i,1,n-1)
{
int a=read(),b=read(),c=read();
v[a].emplace_back(b,c),v[b].emplace_back(a,c);
}
T.Init(); vector<array<int,3>> qr;
For(i,1,m) {int a=read(),b=read(); qr.push_back({a,b,i});}
maxs[0]=n+5,rt=0,sum=n,GetRt(1,0),GetRt(rt,0),dfs(rt,qr);
For(i,1,m) printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11944kb
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: 0ms
memory: 16076kb
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: 0ms
memory: 10228kb
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: 19ms
memory: 18432kb
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 626539793176 505328010255 637066393138 472546379596 645842915960 641537859258 573604504956 644081575470 803875451466 674370549986 734764046916 744862815441 763778393516 553499885160 526743824759 610373719514 689550247042 549161302822 726811438160 653134244120 666761991962 701575393972 6...
result:
wrong answer 3rd numbers differ - expected: '514273941704', found: '505328010255'