QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#26822 | #2564. Two Trees | Irisu | ML | 5ms | 40544kb | C++14 | 6.9kb | 2022-04-08 16:32:49 | 2022-04-29 04:36:30 |
Judging History
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;u64 dt;
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];
u64 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]);
dt+=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(const 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];
u64 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){
dt+=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){
u64 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);
}
}
vi vec[maxn],son[maxn];
struct cmp{
bool operator()(int x,int y){
return vec[x].size()>vec[y].size();
}
};
void divide(int rt,int f){
// printf("!%d\n",rt);
sumsz=0,dfs3(rt,0),dfs4(rt,0),rt=sck;
// printf("#%d\n",rt);
vis[rt]=1,D[rt]=0;
if(f)son[f].push_back(rt);
for(int v:E[rt])if(!vis[v]){
divide(v,rt);
dfs5(v,rt);
}
priority_queue<int,vi,cmp>Q;
for(int v:son[rt]){
// printf("[%d : %d]\n",rt,v);
virT::calc(vec[v],-1),Q.push(v);
}
vec[rt].push_back(rt);
Q.push(rt);
while(Q.size()>1u){
int x=Q.top();Q.pop();
int y=Q.top();Q.pop();
vi&A=vec[x],&B=vec[y];
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++];
}
}
vi tmp;
if(y!=rt){
A.swap(ano);
B.swap(tmp);
Q.push(x);
}else{
B.swap(ano);
A.swap(tmp);
Q.push(y);
}
}
// printf("@%d : ",rt);for(int x:vec[rt])printf("%d ",x);puts("");
virT::calc(vec[rt],1);
vis[rt]=0;
}
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=dt/2;
T1::divide(1,0);
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 32788kb
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: 33164kb
input:
3 1 2 1 3 1 2 2 3
output:
22
result:
ok 1 number(s): "22"
Test #3:
score: 0
Accepted
time: 2ms
memory: 37940kb
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: 2ms
memory: 36964kb
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: 1ms
memory: 37616kb
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: 2ms
memory: 37088kb
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: 2ms
memory: 36960kb
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: 1ms
memory: 38180kb
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: 1ms
memory: 36884kb
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: 2ms
memory: 37700kb
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: 1ms
memory: 38492kb
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: 3ms
memory: 38424kb
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: 2ms
memory: 39164kb
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: 0
Accepted
time: 5ms
memory: 40420kb
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:
2082289167
result:
ok 1 number(s): "2082289167"
Test #15:
score: 0
Accepted
time: 4ms
memory: 40408kb
input:
2000 774 919 919 94 919 1486 94 839 94 288 1486 1037 1486 1779 839 1307 839 550 288 1902 288 527 1037 1412 1037 367 1779 349 1779 1169 1307 752 1307 548 550 1089 550 1018 1902 616 1902 69 527 897 527 1597 1412 1197 1412 1033 367 1646 367 1727 349 916 349 534 1169 1254 1169 41 752 410 752 853 548 139...
output:
780832696
result:
ok 1 number(s): "780832696"
Test #16:
score: 0
Accepted
time: 2ms
memory: 40544kb
input:
2000 1250 1654 1654 215 1654 494 1654 1229 1654 1822 1654 1256 1654 1681 1654 1745 1654 570 1654 523 1654 1651 1654 673 1654 615 1654 210 1654 922 1654 62 1654 1624 1654 231 1654 26 1654 1330 1654 403 1654 255 1654 1734 1654 1546 1654 1809 1654 213 1654 469 1654 1268 1654 918 1654 589 1654 1457 1654...
output:
86564484
result:
ok 1 number(s): "86564484"
Test #17:
score: 0
Accepted
time: 2ms
memory: 39320kb
input:
2000 1679 862 862 1229 862 1126 862 139 862 858 862 1730 862 854 862 323 862 896 862 1249 862 1048 862 1219 862 934 862 445 862 1732 862 1049 862 1735 862 1020 862 503 862 1207 862 1011 862 1272 862 1496 862 1962 862 691 862 274 862 915 862 1624 862 780 862 350 862 455 862 505 862 1293 862 100 862 6...
output:
61198783
result:
ok 1 number(s): "61198783"
Test #18:
score: 0
Accepted
time: 4ms
memory: 39688kb
input:
2000 1650 387 387 806 387 577 387 1798 387 1887 387 346 387 1655 387 948 387 250 387 311 387 284 387 1930 387 1333 387 1775 387 1361 387 397 387 1923 387 1822 387 1625 387 529 387 1321 387 1241 387 1059 387 13 387 370 387 179 387 1365 387 755 387 1537 387 1105 387 428 387 867 387 1824 387 709 387 19...
output:
78140503
result:
ok 1 number(s): "78140503"
Test #19:
score: 0
Accepted
time: 3ms
memory: 39644kb
input:
2000 32 1266 1266 33 1266 1006 1266 1942 1266 1886 1266 148 1266 1180 1266 485 1266 1009 1266 1237 1266 1529 1266 1085 1266 103 1266 389 1266 1890 1266 173 1266 1479 1266 220 1266 1356 1266 1798 1266 1698 1266 1461 1266 271 1266 830 1266 1490 1266 1183 1266 1799 1266 975 1266 1550 1266 1802 1266 324...
output:
90345056
result:
ok 1 number(s): "90345056"
Test #20:
score: 0
Accepted
time: 2ms
memory: 40476kb
input:
2000 1588 1435 1435 963 1435 1995 1435 871 1435 1379 1435 634 1435 1822 1435 131 1435 1807 1435 704 1435 1629 1435 1096 1435 1667 1435 493 1435 402 1435 1029 1435 860 1435 859 1435 1446 1435 637 1435 339 1435 1895 1435 1329 1435 989 1435 839 1435 759 1435 260 1435 1935 1435 1304 1435 117 1435 435 14...
output:
62856991
result:
ok 1 number(s): "62856991"
Test #21:
score: 0
Accepted
time: 5ms
memory: 40008kb
input:
2000 830 1297 1297 1345 1297 419 1297 774 1297 303 1297 974 1297 1389 1297 991 1297 1979 1297 810 1297 1831 1297 1808 1297 1921 1297 337 1297 1950 1297 1757 1297 1534 1297 1000 1297 1651 1297 1075 1297 687 1297 1998 1297 336 1297 220 1297 102 1297 675 1297 1527 1297 705 1297 1431 1297 735 1297 1066 ...
output:
70817596
result:
ok 1 number(s): "70817596"
Test #22:
score: 0
Accepted
time: 0ms
memory: 39880kb
input:
2000 518 1352 1352 1293 1352 308 1352 433 1352 1135 1352 404 1352 429 1352 375 1352 1654 1352 710 1352 984 1352 1639 1352 1972 1352 107 1352 1494 1352 1615 1352 1796 1352 584 1352 1339 1352 1171 1352 185 1352 1803 1352 526 1352 1918 1352 788 1352 660 1352 23 1352 1622 1352 1619 1352 932 1352 1487 13...
output:
57316912
result:
ok 1 number(s): "57316912"
Test #23:
score: -100
Memory Limit Exceeded
input:
10000 3222 9327 9327 5629 9327 1730 5629 1822 5629 6427 1730 7478 1730 1148 1822 5482 1822 6681 6427 9351 6427 1330 7478 5688 7478 2987 1148 7077 1148 5697 5482 2819 5482 2593 6681 2161 6681 7664 9351 434 9351 6702 1330 7324 1330 2048 5688 9346 5688 5530 2987 3562 2987 9939 7077 2484 7077 6011 5697 ...