QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#693403 | #5439. Meet in the Middle | wdnmdwrnmmp | WA | 21ms | 27592kb | C++14 | 3.4kb | 2024-10-31 16:05:16 | 2024-10-31 16:05:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> vi;
typedef pair<int,int> pi;
using i64=unsigned long long;
void chmax(i64 &x,i64 y){
if(y>x){
x=y;
}
}
const int N=1e5+5;
int n, q;
namespace tree0{
vector<pi> G[N];
int dfn[N], dfc;
i64 dep[N];
int mn[17][N];
void link(int u,int v,int w){
G[u].pb(v, w);
G[v].pb(u, w);
}
void dfs(int u,int p){
dfn[u]=++dfc;
mn[0][dfc]=p;
for(auto [v,w]:G[u]){
if(v!=p){
dep[v]=dep[u]+w;
dfs(v, u);
}
}
}
int get(int u,int v){
return dfn[u]<dfn[v]? u: v;
};
void init(){
dfs(1, 0);
rep(i,1,16){
rep(j,1,n-(1<<i)+1){
mn[i][j]=get(mn[i-1][j], mn[i-1][j+(1<<(i-1))]);
}
}
}
int LCA(int u,int v){
if(u==v){
return u;
}
if((u=dfn[u])>(v=dfn[v])){
swap(u, v);
}
int lg=__lg(v-(u++));
return get(mn[lg][u], mn[lg][v-(1<<lg)+1]);
}
i64 dis(int u,int v){
return dep[u]+dep[v]-dep[LCA(u,v)]*2;
}
void clear(){
rep(i,1,n){
G[i].clear();
}
dfc=0;
}
}
using tree0::dis;
namespace tree1{
struct D{
int u, v;
i64 d, tagu, tagv;
void ins(int c,i64 ext){
if(u==0){
u=v=c, d=0, tagu=tagv=ext;
}
else{
i64 du=dis(u, c), dv=dis(v, c);
if(du+tagu<dv+tagv){
swap(u, v);
swap(du, dv);
swap(tagu, tagv);
}
if(du+ext>d+tagv){
d=du, v=c, tagv=ext;
}
}
}
i64 val(){
return d+tagu+(u!=v)*tagv;
}
};
vector<pi> G[N];
D f[N], g[N], pre[N], suf[N];
void link(int u,int v,int w){
G[u].pb(v, w);
G[v].pb(u, w);
}
void dfs0(int u,int p){
f[u]={u, u, 0, 0, 0};
for(auto [v,w]:G[u]){
if(v!=p){
dfs0(v, u);
f[u].ins(f[v].u, f[v].tagu+w);
f[u].ins(f[v].v, f[v].tagv+w);
}
}
}
void dfs1(int u,int p){
D cur{0, 0, 0, 0, 0};
rep(i,0,(int)G[u].size()-1){
int v=G[u][i].fi, w=G[u][i].se;
if(v!=p){
pre[v]=cur;
cur.ins(f[v].u, f[v].tagu+w);
cur.ins(f[v].v, f[v].tagv+w);
}
}
cur={0, 0, 0, 0, 0};
per(i,(int)G[u].size()-1,0){
int v=G[u][i].fi, w=G[u][i].se;
if(v!=p){
suf[v]=cur;
cur.ins(f[v].u, f[v].tagu+w);
cur.ins(f[v].v, f[v].tagv+w);
}
}
for(auto [v,w]:G[u]){
if(v!=p){
g[v]=g[u];
if(pre[v].u!=0){
g[v].ins(pre[v].u, pre[v].tagu+w);
g[v].ins(pre[v].v, pre[v].tagv+w);
}
if(suf[v].u!=0){
g[v].ins(suf[v].u, suf[v].tagu+w);
g[v].ins(suf[v].v, suf[v].tagv+w);
}
dfs1(v, u);
}
}
}
i64 qry(int u,int v){
// cout<<g[u].u<<' '<<g[u].v<<'\n';
i64 ans=0;
rep(i,0,1){
chmax(ans, dis(v, (i? f[u].v: f[u].u))+(i? f[u].tagv: f[u].tagu) );
if(u!=1) chmax(ans, dis(v, (i? g[u].v: g[u].u))+(i? g[u].tagv: g[u].tagu) );
}
return ans;
}
void clear(){
}
}
void solve(){
cin>>n>>q;
rep(i,1,n-1){
int u,v,w; cin>>u>>v>>w;
tree0::link(u, v, w);
}
tree0::init();
rep(i,1,n-1){
int u,v,w; cin>>u>>v>>w;
tree1::link(u, v, w);
}
tree1::dfs0(1, 0);
tree1::dfs1(1, 0);
rep(i,1,q){
int u,v; cin>>u>>v;
cout<< tree1::qry(v, u) <<'\n';
}
tree0::clear();
tree1::clear();
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t=1;
while(t--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15036kb
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: 18228kb
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: 18192kb
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: 21ms
memory: 27592kb
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:
554258141791 445722427107 435889348670 566926073949 446842769606 577978336144 530741835090 476565924931 536728781741 670467536522 504326584855 568268584862 581922274256 679408261957 460443524956 498942164781 536061131621 541916515005 500212423513 601912230508 517781696669 521058698513 701575393972 5...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '554258141791'