QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#692449 | #5439. Meet in the Middle | NATO | TL | 4ms | 16172kb | C++23 | 2.1kb | 2024-10-31 14:31:05 | 2024-10-31 14:31:27 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define INF 214748364719260817ll
#define MAXN 2147483647ll
using namespace std;
ll n,m;
struct graph
{
ll head[100005],cnt;
struct ed
{
ll v,next,w;
}edge[200005];
void add(ll u,ll v,ll w)
{
edge[++cnt].v=v;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].w=w;
edge[++cnt].v=u;edge[cnt].next=head[v];head[v]=cnt;edge[cnt].w=w;
}
ll f[200005][20],tot,lg[200005],dfn[200005],dep[200005],dis[200005];
ll from[200005][20];
void clear()
{
for(ll i=1;i<=n;++i)head[i]=0;cnt=tot=0;
}
void dfs(ll id,ll fa)
{
dep[id]=dep[fa]+1;
f[dfn[id]=++tot][0]=f[dfn[fa]][0]+1;
from[dfn[id]][0]=id;
for(ll i=head[id];i;i=edge[i].next)
{
ll v=edge[i].v,w=edge[i].w;
if(v==fa)continue;
dis[v]=dis[id]+w;
dfs(v,id);
f[++tot][0]=dep[id];from[tot][0]=id;
}
}
ll get_lca(ll u,ll v)
{
if(dfn[u]>dfn[v])swap(u,v);
ll use=lg[dfn[v]-dfn[u]+1];
ll r1=from[dfn[u]][use],r2=from[dfn[v]-(1<<use)+1][use];
return dep[r1]<dep[r2]?r1:r2;
}
void init()
{
ll u,v,w;
for(ll i=1;i<n;++i)cin>>u>>v>>w,add(u,v,w);
for(ll i=2;i<=2*n-1;++i)lg[i]=lg[i>>1]+1;
dfs(1,0);
for(ll i=tot;i>0;--i)
for(ll j=1;(i+(1<<j)-1)<=tot;++j)
{
f[i][j]=f[i][j-1];from[i][j]=from[i][j-1];
if(f[i][j]>f[i+(1<<(j-1))][j-1])
f[i][j]=f[i+(1<<(j-1))][j-1],from[i][j]=from[i+(1<<(j-1))][j-1];
}
}
ll get_dis(ll u,ll v)
{
return dis[u]+dis[v]-2*dis[get_lca(u,v)];
}
}g[2];
ll t;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
random_device my;
mt19937 rd(my());
g[0].clear();
g[1].clear();
cin>>n>>m;
g[0].init();
g[1].init();
set<ll>p;
for(ll i=1;i<=48000;++i)
{
ll u=rd()%n+1,v=rd()%n+1;
ll maxn=-1,uid=0;
for(ll j=1;j<=n;++j)
{
ll val=g[0].get_dis(u,j)+g[1].get_dis(v,j);
if(val>maxn)maxn=val,uid=j;
}
p.insert(uid);
}
while(m--)
{
ll u,v;
cin>>u>>v;
ll maxn=-1;
for(auto it:p)
{
ll val=g[0].get_dis(u,it)+g[1].get_dis(v,it);
maxn=max(maxn,val);
}
cout<<maxn<<'\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 14064kb
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: 4ms
memory: 16172kb
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: 4ms
memory: 13840kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Time Limit Exceeded
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...