QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#692600 | #5439. Meet in the Middle | wjwweiwei | WA | 16ms | 13056kb | C++14 | 2.5kb | 2024-10-31 14:46:18 | 2024-10-31 14:46:25 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
typedef long long ll;
bool Mst;
const int N=1e5+5;
int n,q;
int T;
struct Tree{
int head[N],to[N<<1],nxt[N<<1],tot,fa[N],dep[N];
int son[N],siz[N],top[N];
int fl[N<<1];ll dis[N];
inline void clear(){
for(int i=0;i<=n;i++){
head[i]=dis[i]=dep[i]=son[i]=siz[i]=top[i]=0;
}
tot=0;
}
inline void add(int u,int v,int w){nxt[++tot]=head[u],to[tot]=v,fl[tot]=w,head[u]=tot;}
void dfs(int u,int fat){
dep[u]=dep[fat]+1;
fa[u]=fat;
siz[u]=1;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(v==fat)continue;
dis[v]=dis[u]+fl[i];
dfs(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v])son[u]=v;
}
}
void dfs2(int u,int topu){
top[u]=topu;
if(!son[u])return ;
dfs2(son[u],top[u]);
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(v==fa[u]||v==son[u])continue;
dfs2(v,v);
}
}
inline int lca(int u,int v){
while(top[u]^top[v]){
if(dep[top[u]]>dep[top[v]])u=fa[top[u]];
else v=fa[top[v]];
}
return dep[u]<dep[v]?u:v;
}
inline ll gdis(int u,int v){return dis[u]+dis[v]-2ll*dis[lca(u,v)];}
}T1,T2;
mt19937 rnd(time(0));
set<int>rea;
int gt[N],cnt;
bool Med;
struct Qry{
int u,v,id;
}qr[N<<3];
ll Ans[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;cin>>q;
int u,v,w;
for(int i=1;i<n;i++)cin>>u>>v>>w,T1.add(u,v,w),T1.add(v,u,w);
for(int i=1;i<n;i++)cin>>u>>v>>w,T2.add(u,v,w),T2.add(v,u,w);
T1.dfs(1,0),T2.dfs(1,0);T1.dfs2(1,1),T2.dfs2(1,1);
if(n<=3000&&q<=3000){
for(int i=1;i<=q;i++){
cin>>u>>v;
ll ans=-1;
for(int j=1;j<=n;j++){
ll now=T1.gdis(u,j)+T2.gdis(v,j);
ans=max(ans,now);
}
cout<<ans<<"\n";
}
}
else{
int t=23000;
for(int i=1;i<=q;i++){
cin>>qr[i].u>>qr[i].v;qr[i].id=i;
}
int st=min(q,t),cur=1;
random_shuffle(qr+1,qr+1+q);
for(cur=1;cur<=st;cur++){
int u=qr[cur].u,v=qr[cur].v;
ll ans=-1;
int id=1;
for(int j=1;j<=n;j++){
ll now=T1.gdis(u,j)+T2.gdis(v,j);
ans=max(ans,now);
if(ans==now)id=j;
}
rea.insert(id);
Ans[cur]=ans;
if(rea.size()==4)break;
}
cnt=0;
for(auto v:rea)gt[++cnt]=v;
for(int i=cur;i<=q;i++){
cin>>u>>v;
ll ans=-1;
for(int k=1;k<=cnt;k++){
int j=gt[k];
ll now=T1.gdis(u,j)+T2.gdis(v,j);
ans=max(ans,now);
}
Ans[i]=ans;
}
for(int i=1;i<=q;i++)cout<<Ans[i]<<"\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7932kb
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: 1ms
memory: 8000kb
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: 1ms
memory: 7944kb
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: 16ms
memory: 13056kb
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:
691344841051 821113600623 668453366344 511427753568 593596426760 625281495622 774310527192 483145808092 661905477354 507620192148 629099989939 730849776540 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 700935120397 7...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '691344841051'