QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290321#4945. 暴力写挂SoyTony100 ✓1940ms134216kbC++146.3kb2023-12-24 20:02:212023-12-24 20:02:21

Judging History

你现在查看的是最新测评结果

  • [2023-12-24 20:02:21]
  • 评测
  • 测评结果:100
  • 用时:1940ms
  • 内存:134216kb
  • [2023-12-24 20:02:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=4e5+10;
const ll llinf=0x3f3f3f3f3f3f3f3f;

inline int read(){
    int x=0,w=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x*w;
}

int n;
struct edge{
    int to,nxt,w;
}E1[maxn<<1],E2[maxn<<1],e1[maxn<<2],e2[maxn<<1];
int Head1[maxn],Cnt1,Head2[maxn],Cnt2,head1[maxn<<1],cnt1,head2[maxn],cnt2;
inline void add_edge(edge *e,int *head,int &cnt,int u,int v,int w){
    e[++cnt].to=v,e[cnt].nxt=head[u],head[u]=cnt,e[cnt].w=w;
    e[++cnt].to=u,e[cnt].nxt=head[v],head[v]=cnt,e[cnt].w=w;
}

int tot;
int fa1[maxn<<1],dep1[maxn<<1],siz1[maxn<<1],son1[maxn<<1];
ll dis1[maxn<<1];
int top1[maxn<<1];
void rebuild(int u,int f){
    for(int i=Head1[u],j=0,last=u,v,w;i;i=E1[i].nxt){
        v=E1[i].to,w=E1[i].w;
        if(v==f) continue;
        ++j;
        if(j==1){
            add_edge(e1,head1,cnt1,u,v,w);
        }
        else{
            ++tot;
            add_edge(e1,head1,cnt1,last,tot,0);
            add_edge(e1,head1,cnt1,tot,v,w);
            last=tot;
        }
    }
    for(int i=Head1[u],v;i;i=E1[i].nxt){
        v=E1[i].to;
        if(v==f) continue;
        rebuild(v,u);
    }
}
void dfs1(int u,int f,int d){
    fa1[u]=f,dep1[u]=d,siz1[u]=1;
    for(int i=head1[u],v,w;i;i=e1[i].nxt){
        v=e1[i].to,w=e1[i].w;
        if(v==f) continue;
        dis1[v]=dis1[u]+w;
        dfs1(v,u,d+1);
        siz1[u]+=siz1[v];
        if(!son1[u]||siz1[v]>siz1[son1[u]]) son1[u]=v;
    }
}
void dfs2(int u,int t){
    top1[u]=t;
    if(!son1[u]) return;
    dfs2(son1[u],t);
    for(int i=head1[u],v;i;i=e1[i].nxt){
        v=e1[i].to;
        if(v==fa1[u]||v==son1[u]) continue;
        dfs2(v,v);
    }
}
inline int get_LCA1(int u,int v){
    while(top1[u]!=top1[v]){
        if(dep1[top1[u]]>dep1[top1[v]]) swap(u,v);
        v=fa1[top1[v]];
    }
    if(dep1[u]>dep1[v]) swap(u,v);
    return u;
}


int fa2[maxn],dep2[maxn],siz2[maxn],son2[maxn];
ll dis2[maxn];
int top2[maxn],dfn2[maxn],dfncnt2;
void dfs3(int u,int f,int d){
    fa2[u]=f,dep2[u]=d,siz2[u]=1;
    for(int i=Head2[u],v,w;i;i=E2[i].nxt){
        v=E2[i].to,w=E2[i].w;
        if(v==f) continue;
        dis2[v]=dis2[u]+w;
        dfs3(v,u,d+1);
        siz2[u]+=siz2[v];
        if(!son2[u]||siz2[v]>siz2[son2[u]]) son2[u]=v;
    }
}
void dfs4(int u,int t){
    top2[u]=t,dfn2[u]=++dfncnt2;
    if(!son2[u]) return;
    dfs4(son2[u],t);
    for(int i=Head2[u],v;i;i=E2[i].nxt){
        v=E2[i].to;
        if(v==fa2[u]||v==son2[u]) continue;
        dfs4(v,v);
    }
}
inline int get_LCA2(int u,int v){
    while(top2[u]!=top2[v]){
        if(dep2[top2[u]]>dep2[top2[v]]) swap(u,v);
        v=fa2[top2[v]];
    }
    if(dep2[u]>dep2[v]) swap(u,v);
    return u;
}

ll ans;

vector<int> V;
int st[maxn],top;
ll mx[maxn][2];
inline void build(){
    sort(V.begin(),V.end(),[&](int A,int B){
        return dfn2[A]<dfn2[B];
    });
    V.erase(unique(V.begin(),V.end()),V.end());
    cnt2=0;
    head2[1]=0;
    st[top=1]=1;
    for(int u:V){
        if(u==1) continue;
        if(get_LCA2(u,st[top])==st[top]){
            head2[u]=0;
            st[++top]=u;
            continue;
        }
        while(top>1&&dep2[get_LCA2(u,st[top])]<dep2[st[top-1]]){
            add_edge(e2,head2,cnt2,st[top-1],st[top],0);
            --top;
        }
        if(get_LCA2(u,st[top])==st[top-1]){
            add_edge(e2,head2,cnt2,st[top-1],st[top],0);
            head2[u]=0;
            st[top]=u;
        }
        else{
            int lca=get_LCA2(u,st[top]);
            head2[lca]=0,head2[u]=0;
            add_edge(e2,head2,cnt2,lca,st[top],0);
            st[top]=lca,st[++top]=u;
        }
    }
    for(int i=top;i>1;--i){
        add_edge(e2,head2,cnt2,st[i-1],st[i],0);
    }
}
void calc(int u,int f){
    for(int i=head2[u],v;i;i=e2[i].nxt){
        v=e2[i].to;
        if(v==f) continue;
        calc(v,u);
        ans=max({ans,mx[u][0]+mx[v][1]-dis2[u],mx[u][1]+mx[v][0]-dis2[u]});
        mx[u][0]=max(mx[u][0],mx[v][0]),mx[u][1]=max(mx[u][1],mx[v][1]);
        mx[v][0]=-llinf,mx[v][1]=-llinf;
    }
    if(u==1) mx[u][0]=-llinf,mx[u][1]=-llinf;
}

int ct;
bool vis[maxn<<2];
int maxsiz[maxn<<1],sumsiz;
inline void init(int u){
    ct=0;
    sumsiz=siz1[u];
}
void dfs_siz(int u,int f){
    siz1[u]=1;
    for(int i=head1[u],v;i;i=e1[i].nxt){
        v=e1[i].to;
        if(vis[i]||v==f) continue;
        dfs_siz(v,u);
        siz1[u]+=siz1[v];
    }
}
void dfs_ct(int u,int f){
    maxsiz[u]=0;
    for(int i=head1[u],v;i;i=e1[i].nxt){
        v=e1[i].to;
        if(vis[i]||v==f) continue;
        dfs_ct(v,u);
        maxsiz[v]=max(siz1[v],sumsiz-siz1[v]);
        if(!ct||maxsiz[v]<maxsiz[e1[ct].to]) ct=i;
    }
}
void dfsS(int u,int f,int tmp){
    if(u<=n){
        V.push_back(u);
        mx[u][0]=dis1[u]-dis1[get_LCA1(u,tmp)];
    }
    for(int i=head1[u],v;i;i=e1[i].nxt){
        v=e1[i].to;
        if(vis[i]||v==f) continue;
        dfsS(v,u,tmp);
    }
}
void dfsT(int u,int f,int &tmp){
    if(u<=n){
        V.push_back(u);
        mx[u][1]=dis1[u];
        tmp=u;
    }
    for(int i=head1[u],v;i;i=e1[i].nxt){
        v=e1[i].to;
        if(vis[i]||v==f) continue;
        dfsT(v,u,tmp);
    }
}
void solve(int id){
    if(!id) return;
    vis[id]=true,vis[id^1]=true;
    int u=e1[id].to,v=e1[id^1].to;
    if(fa1[u]==v) swap(u,v);
    V.clear();
    int tmp=0;
    dfsT(v,0,tmp);
    if(!tmp) return;
    dfsS(u,0,tmp);
    build();
    calc(1,0);
    dfs_siz(u,0),init(u),dfs_ct(u,0),solve(ct);
    dfs_siz(v,0),init(v),dfs_ct(v,0),solve(ct);
    
}

int main(){
    n=read();
    for(int i=1,u,v,w;i<n;++i){
        u=read(),v=read(),w=read();
        add_edge(E1,Head1,Cnt1,u,v,w);
    }
    for(int i=1,u,v,w;i<n;++i){
        u=read(),v=read(),w=read();
        add_edge(E2,Head2,Cnt2,u,v,w);
    }
    tot=n,cnt1=1;
    rebuild(1,0);
    dfs1(1,0,0);
    dfs2(1,1);
    dfs3(1,0,0);
    dfs4(1,1);
    ans=-llinf;
    for(int i=1;i<=n;++i) mx[i][0]=mx[i][1]=-llinf;
    dfs_siz(1,0),init(1),dfs_ct(1,0),solve(ct);
    for(int i=1;i<=n;++i) ans=max(ans,dis1[i]-dis2[i]);
    printf("%lld\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 28500kb

input:

36
35 34 1
16 17 1
26 27 1
4 5 1
2 1 1
30 29 1
9 10 1
13 14 1
14 36 1
29 28 1
31 16 1
33 34 1
18 17 1
6 7 1
22 23 1
25 24 1
25 26 1
22 21 1
24 23 1
14 15 1
19 20 1
4 2 1
31 32 1
32 33 1
11 12 1
21 20 1
5 6 1
4 9 1
16 15 1
2 3 1
28 27 1
12 13 1
11 10 1
19 18 1
7 8 1
19 18 1
18 17 1
22 21 1
7 8 1
35 3...

output:

28

result:

ok 1 number(s): "28"

Test #2:

score: 5
Accepted
time: 0ms
memory: 9984kb

input:

366
302 300 1
281 284 1
205 213 1
38 56 1
280 278 1
215 218 1
287 289 1
337 336 1
32 19 1
161 135 1
154 152 1
339 343 1
333 336 1
316 320 1
190 203 1
235 229 1
238 253 1
162 188 1
125 128 1
104 100 1
255 275 1
141 139 1
249 231 1
322 318 1
320 323 1
2 49 1
122 123 1
2 3 1
252 229 1
93 96 1
13 83 1
4...

output:

66

result:

ok 1 number(s): "66"

Test #3:

score: 5
Accepted
time: 0ms
memory: 12372kb

input:

1388
233 230 2010117013
466 465 517864445
273 277 468266280
3 33 1876495368
523 525 393728142
1290 1288 964611218
456 453 291234141
1106 862 1491931739
748 752 1273412570
702 706 91665845
1194 862 955991116
1006 863 1683051927
862 1165 1155264346
372 370 1224015059
544 548 667577990
2 148 1198631891...

output:

328592252066

result:

ok 1 number(s): "328592252066"

Test #4:

score: 5
Accepted
time: 5ms
memory: 4388kb

input:

1999
247 25 793109451
1026 1023 1869940234
1132 832 612508077
773 1136 163935570
750 752 843387799
1244 982 1777108778
38 131 1018040742
1273 943 1734553409
96 28 2014569809
14 18 1736377610
1558 1536 1818734353
1507 1395 1869434908
1471 1838 516216128
1115 1051 938285248
1889 1570 1781082422
111 47...

output:

40944563637

result:

ok 1 number(s): "40944563637"

Test #5:

score: 5
Accepted
time: 3ms
memory: 4492kb

input:

2666
1914 1909 1900304480
1896 1827 46829052
1462 461 1034104716
1993 2208 1476581917
534 306 1587894016
2318 2334 54406485
407 1523 1108845815
118 58 1464204605
739 1127 1082834408
1333 1240 1326511079
22 851 1562784465
874 204 31232643
2157 2230 1432494307
2269 2494 1877799642
2525 2547 767349491
...

output:

46319083678

result:

ok 1 number(s): "46319083678"

Test #6:

score: 5
Accepted
time: 7ms
memory: 5416kb

input:

5666
5394 4025 1009479564
1480 2237 -1429591010
1404 2085 856916845
5289 3972 742726642
2981 2818 1314010296
1457 1090 -733492464
819 771 1384454828
2244 1483 1063195272
1292 1007 -1861141429
4640 3648 -1921175128
2368 1545 -1581683837
2 90 1858875740
3 666 935624153
1121 922 190864513
1186 954 4355...

output:

77595942376

result:

ok 1 number(s): "77595942376"

Test #7:

score: 5
Accepted
time: 27ms
memory: 5868kb

input:

8666
165 1201 1038443784
2507 1934 -734585573
1843 2444 1510685986
125 255 -371342030
7397 7180 1436849724
4391 3562 1721269684
8169 8406 -769044783
7346 7183 1040271094
2484 2407 -1780590520
6059 6591 1083318614
1947 3267 1411381807
1532 1465 1760339062
774 654 -1082029949
7969 7683 -1138237500
368...

output:

43632978289

result:

ok 1 number(s): "43632978289"

Test #8:

score: 5
Accepted
time: 22ms
memory: 6840kb

input:

11111
5475 5474 1264346617
5400 5399 479067219
6768 6767 1666857947
4681 4680 -1300739728
9511 9512 501265531
7280 7281 -641351804
5261 5262 -915253126
8278 8279 607718841
2624 2625 272191602
69 70 1522014920
9336 9335 -1301642281
10833 10834 -1327316854
9991 9990 1428368830
4841 4840 -424194350
398...

output:

29894787715

result:

ok 1 number(s): "29894787715"

Test #9:

score: 5
Accepted
time: 28ms
memory: 39716kb

input:

12345
8243 8244 -1053057901
12050 12051 -1633460666
10904 10905 52815170
9562 9561 -1045255643
861 860 1207725542
2964 2965 -1535983715
11858 11859 477008449
3541 3542 -612566097
11514 11513 1670385301
5072 5073 283074153
11471 11472 -1911022368
7286 7285 911877895
3455 3454 113595854
1357 1358 -114...

output:

33297530419

result:

ok 1 number(s): "33297530419"

Test #10:

score: 5
Accepted
time: 261ms
memory: 78852kb

input:

188888
76753 76752 608781106
173345 173344 1385300362
52402 52401 616008356
164674 164673 1465319105
8389 8388 1711299259
106391 106390 1915795430
140922 140921 1191918051
23209 23210 1554648596
144489 144490 940708893
113066 113067 1845664846
156111 156112 1032902039
47174 47173 948212630
7550 7551...

output:

190352672270703

result:

ok 1 number(s): "190352672270703"

Test #11:

score: 5
Accepted
time: 579ms
memory: 124192kb

input:

366666
130393 130392 1745739051
2909 2908 648703068
287682 287683 214392793
34303 34304 -1178470735
201947 201948 1633041545
71534 71535 -1898563484
364105 364106 3121060
245712 245713 1970744166
332266 332265 -1399804510
350346 350347 1258576095
297078 297079 1846269167
353774 353773 -1654457930
14...

output:

262853684155

result:

ok 1 number(s): "262853684155"

Test #12:

score: 5
Accepted
time: 405ms
memory: 87980kb

input:

188888
63165 63166 1837585977
186944 186943 418930609
158449 158448 353605203
81350 81349 818330358
46400 46401 1797408919
86471 86470 2009543790
134326 134327 1744525828
182974 182973 1801286949
159896 159897 829675145
105703 105704 1813431214
75191 75192 290150959
183962 183961 1946851254
154410 1...

output:

190262840337216

result:

ok 1 number(s): "190262840337216"

Test #13:

score: 5
Accepted
time: 1148ms
memory: 118972kb

input:

366666
326151 326150 431983823
102666 102665 709407221
282506 282505 249567611
103228 103229 1484554698
55108 55107 1022198226
43915 43914 423843667
252667 252666 1949908115
314746 314747 736646600
127181 127180 1851447045
321033 321032 379403997
356758 356757 56556585
286283 286282 1171907991
19794...

output:

370227457628347

result:

ok 1 number(s): "370227457628347"

Test #14:

score: 5
Accepted
time: 941ms
memory: 118468kb

input:

366666
333246 333245 -1455666363
209725 209726 411942300
137733 137734 -1036880356
282418 282419 1257507163
31970 31969 68984509
245443 245442 640608996
17646 17647 1484211578
352864 352865 -1957781539
119808 119809 1994115839
233773 233774 733202407
50580 50579 -645159820
234978 234977 -1253386771
...

output:

1087912926406

result:

ok 1 number(s): "1087912926406"

Test #15:

score: 5
Accepted
time: 514ms
memory: 88632kb

input:

188888
68419 68522 303955801
42623 14462 1286912953
25459 33619 717260553
21744 21793 1190600040
128869 112300 587562905
112298 118721 1704852203
149867 112300 457066097
17095 48549 177299469
23649 41641 725059649
85230 77024 757651257
112298 183254 1590484170
112298 132572 1791449084
8766 29333 115...

output:

46886180770

result:

ok 1 number(s): "46886180770"

Test #16:

score: 5
Accepted
time: 1096ms
memory: 128412kb

input:

366666
86275 75039 1457813126
189352 226927 1915597621
147287 48158 1290734574
31893 123322 712745103
302921 273732 123061183
302056 286799 1551449832
41543 41058 600034432
94194 106399 1771598529
43183 11406 1070539898
28883 24429 201958100
203676 255576 9754959
32408 64152 246897921
15567 64399 14...

output:

222227692617

result:

ok 1 number(s): "222227692617"

Test #17:

score: 5
Accepted
time: 1130ms
memory: 134216kb

input:

366666
129619 5198 -973656183
266812 231272 1567664157
5196 88382 785954950
14850 5196 118061314
5197 185242 1088219202
5197 73335 -1334128788
5198 50710 764178009
5196 104181 -539107013
216830 235373 667432679
186324 186419 -1550436917
186139 257465 319261589
5197 28980 -1399296087
5198 20052 -6614...

output:

582086506117

result:

ok 1 number(s): "582086506117"

Test #18:

score: 5
Accepted
time: 1798ms
memory: 112680kb

input:

366666
221087 261714 -1157424755
275319 221086 1146814470
322529 221086 -792303321
4 14915 -879002672
221087 347873 -782761546
78013 85363 1827554750
221085 271698 1986551213
134063 78013 -216058810
221087 286211 987545442
126222 78012 128783110
17264 2 490291481
292957 221085 -1076233944
230128 221...

output:

126836005122

result:

ok 1 number(s): "126836005122"

Test #19:

score: 5
Accepted
time: 1940ms
memory: 98040kb

input:

366666
278666 284245 -656119512
86406 110657 -502085363
144075 128040 776809184
69336 174347 -804165979
67547 156366 -1848058246
64373 71724 -715071830
19658 5225 -1361227618
315145 291521 701585949
303323 267128 -1560410812
300079 306255 -929824651
286404 249489 -1944484045
68182 173207 -786742825
...

output:

247695762186

result:

ok 1 number(s): "247695762186"

Test #20:

score: 5
Accepted
time: 1227ms
memory: 96812kb

input:

366666
213470 213298 -1206709900
93209 46605 -1438620736
82357 164714 -1234024344
69683 139365 -215225542
234896 213857 -1140909086
54821 109642 -559041306
92759 185518 925676817
37465 74929 -912279904
85738 171476 -1123336314
96113 192226 680464365
55163 110326 -1286101794
334453 349377 -1841466041...

output:

45736064383

result:

ok 1 number(s): "45736064383"

Extra Test:

score: 0
Extra Test Passed