QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#26817#2564. Two TreesIrisuWA 10ms30552kbC++146.5kb2022-04-08 15:47:552022-04-29 04:36:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 04:36:08]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:30552kb
  • [2022-04-08 15:47:55]
  • 提交

answer

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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}

#define pb push_back
#define ALL(x) (x).begin(),(x).end()
#define mem(x) memset((x),0,sizeof(x))

typedef double db;
typedef long long ll;
typedef vector<int>vi;
typedef pair<int,int>pii;

typedef unsigned u32;
typedef unsigned uint;
typedef unsigned long long u64;

namespace orzjk{
  const int SZ=1e6;
  char buf[SZ],*p1=buf,*p2=buf;
  char nc(){
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,SZ,stdin),p1==p2)?EOF:*p1++;
  }
  char fub[SZ],*p3=fub,*p4=fub+SZ;
  void pc(char c){
    p3==p4&&(fwrite(fub,1,SZ,stdout),p3=fub);
    *p3++=c;
  }
  void flush(){
    fwrite(fub,1,p3-fub,stdout),p3=fub;
  }
}
using orzjk::nc;
using orzjk::pc;

//#define nc getchar
//#define pc putchar

int read(){
  int x=0,f=1;char c=nc();
  while(c<48)c=='-'&&(f=-1),c=nc();
  while(c>47)x=x*10+(c^48),c=nc();
  return x*f;
}
void write(int x){
  static char st[21];
  if(!x)return pc(48),void();
  char*p=st;
  if(x<0)x=-x,pc('-');
  for(;x;x/=10)*p++=x%10|48;
  do{
    pc(*--p);
  }while(p!=st);
}

//const int P=1e9+7;
const int P=998244353;
int qp(int a,int k){
  int res=1;for(;k;k>>=1,a=1ll*a*a%P)if(k&1)res=1ll*res*a%P;return res;
}

const int maxn=1e5+10;
int n;uint ans;

namespace T2{

struct edge{
  int u,v;
}G[maxn];

vi E[maxn];int deg[maxn];

int lg[maxn];
pii st[17][maxn];
int dep[maxn],dfn[maxn],seq[maxn];
uint sz[maxn],F1[maxn],F2[maxn],G1[maxn],G2[maxn];
void dfs1(int u,int f){
  static int now;
  st[0][now]={dfn[f],f},dfn[u]=++now,seq[now]=u;
  sz[u]=1,dep[u]=dep[f]+1;
  for(int v:E[u])if(v!=f){
    dfs1(v,u),sz[u]+=sz[v];
    F1[u]+=F1[v]+sz[v];
    F2[u]+=F2[v]+2*F1[v]+sz[v];
  }
}
void dfs2(int u,int f){
//  printf("(%u %u) (%u %u)\n",F1[u],F2[u],G1[u],G2[u]);
  ans+=G2[u];
  for(int v:E[u])if(v!=f){
    G1[v]=G1[u]-sz[v]+(n-sz[v]);
    G2[v]=G2[u]-2*F1[v]-sz[v]+2*(G1[u]-F1[v]-sz[v])+n-sz[v];
    dfs2(v,u);
  }
}
int qlca(int u,int v){
  if(u==v)return u;
  u=dfn[u],v=dfn[v];
  if(u>v)swap(u,v);
  int k=lg[v-u];pii*seq=st[k];
  return min(seq[u],seq[v-(1<<k)]).second;
}

void init(){
  rep(i,1,n-1){
    int u=read(),v=read();
    G[i]={u,v},deg[u]++,deg[v]++;
  }
  rep(i,1,n)E[i].reserve(deg[i]);
  rep(i,1,n-1){
    int u=G[i].u,v=G[i].v;
    E[u].pb(v),E[v].pb(u);
  }
  dfs1(1,0);
  G1[1]=F1[1],G2[1]=F2[1];
  dfs2(1,0);
//  rep(i,1,n)printf("%d%c",dfn[i],"\n "[i<iend]);
//  rep(i,1,n)printf("(%d %d)%c",st[0][i].first,st[0][i].second,"\n "[i<n]);
  rep(i,2,n)lg[i]=lg[i>>1]+1;
  rep(i,1,16)rep(j,1,n-(1<<i))st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
}

}

namespace virT{

int sumsz;
int clo,tim[maxn];
uint coef,D[maxn];

int ecnt,h[maxn];
struct edge{
  int nxt,to,w;
}E[maxn];
void addline(int u,int v){
  using T2::dep;
//  printf("(%d %d)\n",u,v);
  E[++ecnt]={h[u],v,dep[v]-dep[u]},h[u]=ecnt;
}

uint sz[maxn],F[maxn],G[maxn];
void dfs1(int u){
  F[u]=0,sz[u]=tim[u]==clo;
  for(int i=h[u];i;i=E[i].nxt){
    int v=E[i].to;
    dfs1(v),sz[u]+=sz[v],F[u]+=F[v]+E[i].w*sz[v];
  }
//  printf("!%d, %d\n",u,F[u]);
}

void dfs2(int u){
  for(int i=h[u];i;i=E[i].nxt){
    int v=E[i].to;
    G[v]=G[u]+E[i].w*(sumsz-2*sz[v]);
    dfs2(v);
  }
}

void calc(vi&V,uint coef){
  virT::coef=coef;
  using T2::dfn;
  using T2::qlca;
  static int st[maxn];
  int top=0;
  sumsz=V.size(),clo++;
  for(int u:V){
//    printf("[%d]\n",u);
    h[u]=0,tim[u]=clo;
    if(!top){
      st[++top]=u;continue;
    }
    int anc=qlca(st[top],u);
    if(anc!=st[top]){
      while(top>1&&dfn[anc]<dfn[st[top-1]]){
        addline(st[top-1],st[top]),top--;
      }
      if(top>1&&st[top-1]==anc){
        addline(anc,st[top--]);
      }else{
        h[anc]=0,addline(anc,st[top]),st[top]=anc;
      }
    }
    st[++top]=u;
  }
  rep(i,1,top-1)addline(st[i],st[i+1]);
  dfs1(st[1]);
  G[st[1]]=F[st[1]];
  dfs2(st[1]);
  for(int u:V)ans+=2*coef*D[u]*G[u];
//  for(int u:V)printf("(%d), %d %d\n",u,D[u],G[u]);
//  puts("GG");
}

}

namespace T1{

using virT::D;

struct edge{
  int u,v;
}G[maxn];

vi E[maxn];int deg[maxn];

uint sz[maxn],F1[maxn],F2[maxn],G1[maxn],G2[maxn];
void dfs1(int u,int f){
  sz[u]=1;
  for(int v:E[u])if(v!=f){
    dfs1(v,u),sz[u]+=sz[v];
    F1[u]+=F1[v]+sz[v];
    F2[u]+=F2[v]+2*F1[v]+sz[v];
  }
}
void dfs2(int u,int f){
  ans+=G2[u];
  for(int v:E[u])if(v!=f){
    G1[v]=G1[u]-sz[v]+(n-sz[v]);
    G2[v]=G2[u]-2*F1[v]-sz[v]+2*(G1[u]-F1[v]-sz[v])+n-sz[v];
    dfs2(v,u);
  }
}

bool vis[maxn];
uint sck,sumsz;
void dfs3(int u,int f){
  sumsz++,sz[u]=1;
  for(int v:E[u])if(v!=f&&!vis[v]){
    dfs3(v,u),sz[u]+=sz[v];
  }
}
void dfs4(int u,int f){
  uint s=sumsz-sz[u];
  for(int v:E[u])if(v!=f&&!vis[v]){
    dfs4(v,u),chkmax(s,sz[v]);
  }
  if(s*2<=sumsz)sck=u;
}
void dfs5(int u,int f){
  D[u]=D[f]+1;
  for(int v:E[u])if(v!=f&&!vis[v]){
    dfs5(v,u);
  }
}
struct cmp{
  bool operator()(const vi&x,const vi&y){
    return x.size()>y.size();
  }
};
vi divide(int rt){
//  printf("!%d\n",rt);
  sumsz=0,dfs3(rt,0),dfs4(rt,0),rt=sck;
//  printf("#%d\n",rt);
  vis[rt]=1,D[rt]=0;
  priority_queue<vi,vector<vi>,cmp>Q;
  for(int v:E[rt])if(!vis[v]){
    vi vec=divide(v);
    dfs5(v,rt);
    virT::calc(vec,-1);
    Q.push(vec);
  }
  Q.push(vi(1,rt));
  while(Q.size()>1u){
    vi A=Q.top();Q.pop();
    vi B=Q.top();Q.pop();
    int sx=A.size(),sy=B.size();
    vi ano(sx+sy);
    using T2::dfn;
    for(int p=0,p1=0,p2=0;p<sx+sy;p++){
      if(p2==sy||(p1<sx&&dfn[A[p1]]<dfn[B[p2]])){
        ano[p]=A[p1++];
      }else{
        ano[p]=B[p2++];
      }
    }
    Q.push(ano);
  }
  vi res=Q.top();
  virT::calc(res,1);
  vis[rt]=0;
  return res;
}

void init(){
  rep(i,1,n-1){
    int u=read(),v=read();
    G[i]={u,v},deg[u]++,deg[v]++;
  }
  rep(i,1,n)E[i].reserve(deg[i]);
  rep(i,1,n-1){
    int u=G[i].u,v=G[i].v;
    E[u].pb(v),E[v].pb(u);
  }
  dfs1(1,0);
  G1[1]=F1[1],G2[1]=F2[1];
  dfs2(1,0);
}

}

void solve(){
  n=read();
  T1::init();
  T2::init();
  ans/=2;
  T1::divide(1);
  cout<<ans<<endl;
}

signed main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
//  int T;cin>>T;while(T--)solve();
  solve();
  orzjk::flush();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 24140kb

input:

3
1 2
1 3
1 2
1 3

output:

24

result:

ok 1 number(s): "24"

Test #2:

score: 0
Accepted
time: 4ms
memory: 22068kb

input:

3
1 2
1 3
1 2
2 3

output:

22

result:

ok 1 number(s): "22"

Test #3:

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

input:

500
30 198
198 333
198 17
333 430
333 44
17 99
17 19
430 160
430 162
44 154
44 253
99 466
99 397
19 301
19 101
160 416
160 446
162 375
162 174
154 256
154 170
253 67
253 248
466 462
466 216
397 104
397 306
301 460
301 464
101 226
101 50
416 137
416 456
446 443
446 465
375 92
375 266
174 209
174 84
2...

output:

75020868

result:

ok 1 number(s): "75020868"

Test #4:

score: 0
Accepted
time: 10ms
memory: 28384kb

input:

500
154 376
376 491
376 141
491 499
491 150
141 447
141 427
499 24
499 187
150 352
150 476
447 351
447 223
427 75
427 386
24 222
24 487
187 472
187 449
352 285
352 105
476 117
476 426
351 140
351 336
223 121
223 236
75 388
75 272
386 418
386 106
222 292
222 323
487 111
487 155
472 443
472 217
449 31...

output:

1435776283

result:

ok 1 number(s): "1435776283"

Test #5:

score: 0
Accepted
time: 2ms
memory: 28160kb

input:

500
445 27
27 137
27 105
137 129
137 341
105 431
105 86
129 290
129 292
341 70
341 376
431 206
431 63
86 330
86 324
290 291
290 463
292 200
292 199
70 204
70 476
376 474
376 356
206 260
206 279
63 185
63 459
330 67
330 323
324 266
324 430
291 434
291 242
463 276
463 333
200 493
200 372
199 14
199 12...

output:

29218852

result:

ok 1 number(s): "29218852"

Test #6:

score: 0
Accepted
time: 1ms
memory: 28324kb

input:

500
421 464
464 260
464 67
464 246
464 183
464 44
464 68
464 209
464 27
464 234
464 55
464 423
464 173
464 146
464 74
464 310
464 256
464 359
464 115
464 467
464 479
464 497
464 451
464 466
464 361
464 124
464 286
464 116
464 475
464 292
464 186
464 65
464 295
464 107
260 123
464 443
464 101
464 162...

output:

9330687

result:

ok 1 number(s): "9330687"

Test #7:

score: 0
Accepted
time: 1ms
memory: 28300kb

input:

500
500 213
213 492
213 45
213 438
213 225
213 207
213 134
213 328
213 192
213 167
213 419
213 96
213 270
213 262
213 487
213 294
213 169
213 166
213 130
213 61
213 86
213 450
213 165
213 473
213 140
213 137
213 206
213 317
213 341
213 219
213 297
213 437
213 127
213 298
213 25
213 31
213 3
213 174
...

output:

4381036

result:

ok 1 number(s): "4381036"

Test #8:

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

input:

500
200 23
23 202
23 105
23 344
23 193
23 13
23 206
23 47
23 351
23 164
23 49
23 18
23 207
23 412
23 153
23 50
23 137
23 22
23 313
23 38
23 130
23 401
23 296
23 128
23 94
23 104
23 318
23 222
23 428
23 278
23 477
23 90
23 170
23 380
23 338
202 208
23 75
202 187
23 62
23 165
23 284
202 150
202 282
23...

output:

5567183

result:

ok 1 number(s): "5567183"

Test #9:

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

input:

500
405 193
193 290
193 406
193 401
193 203
193 361
193 202
193 243
193 416
193 34
193 80
193 234
193 53
193 433
193 170
193 275
193 146
193 140
193 356
193 434
193 354
193 292
193 54
193 247
193 420
193 377
193 346
193 102
193 98
193 142
193 12
193 270
193 33
193 107
193 35
193 173
193 386
193 190
...

output:

4487471

result:

ok 1 number(s): "4487471"

Test #10:

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

input:

500
171 110
110 332
110 60
110 186
110 259
110 2
110 299
110 494
110 466
110 410
110 66
110 78
110 352
110 137
110 188
110 94
110 295
110 323
110 213
110 334
110 469
110 179
110 139
110 318
110 443
110 219
110 342
110 474
110 315
110 75
110 146
110 360
110 426
110 100
110 455
110 33
110 305
110 238
...

output:

3701740

result:

ok 1 number(s): "3701740"

Test #11:

score: 0
Accepted
time: 6ms
memory: 28312kb

input:

500
277 29
29 442
29 335
29 20
29 358
29 460
29 465
29 471
29 478
29 22
29 126
29 320
29 427
29 166
29 387
29 321
29 14
29 379
29 238
29 173
29 78
29 302
29 333
29 108
29 472
29 481
29 287
29 269
29 202
29 255
29 284
29 304
29 354
29 492
29 310
29 251
29 293
442 123
442 444
442 3
442 394
29 298
29 2...

output:

6044048

result:

ok 1 number(s): "6044048"

Test #12:

score: 0
Accepted
time: 2ms
memory: 28192kb

input:

500
439 65
65 79
65 193
65 228
65 10
65 39
79 432
65 137
79 303
79 136
79 72
79 121
65 368
65 102
65 452
79 35
65 363
79 473
79 214
65 66
65 450
193 60
65 4
65 401
228 158
193 226
193 326
79 321
228 153
79 396
79 14
228 20
193 112
228 55
65 430
228 462
10 426
79 442
65 346
65 165
65 308
65 184
65 7
...

output:

5949212

result:

ok 1 number(s): "5949212"

Test #13:

score: 0
Accepted
time: 4ms
memory: 30552kb

input:

2000
1487 1400
1400 970
1400 892
970 1775
970 304
892 72
892 32
1775 42
1775 1846
304 73
304 1485
72 648
72 1200
32 1544
32 1978
42 448
42 351
1846 1667
1846 1228
73 125
73 1118
1485 643
1485 1532
648 291
648 749
1200 823
1200 1122
1544 67
1544 1555
1978 281
1978 485
448 1683
448 191
351 331
351 427...

output:

2087128292

result:

ok 1 number(s): "2087128292"

Test #14:

score: -100
Wrong Answer
time: 8ms
memory: 30464kb

input:

2000
963 1511
1511 1963
1511 1474
1963 19
1963 1805
1474 1107
1474 596
19 548
19 359
1805 744
1805 1176
1107 1394
1107 1065
596 1657
596 1207
548 210
548 1232
359 1340
359 1487
744 787
744 507
1176 1086
1176 1308
1394 1076
1394 214
1065 1347
1065 433
1657 913
1657 1993
1207 1403
1207 1824
210 1629
2...

output:

4229772815

result:

wrong answer 1st numbers differ - expected: '2082289167', found: '4229772815'