QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#639449 | #2012. 树的重心 | RockyYue | 60 | 1008ms | 26332kb | C++14 | 3.3kb | 2024-10-13 19:38:10 | 2024-10-13 19:38:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
struct edge{
int v,nxt;
}e[N<<1];
int head[N],cnt;
void add(int u,int v){
e[++cnt]={v,head[u]},head[u]=cnt;
}
int n,deg[N],dep[N],sz[N],big[N],secbig[N],fa[N],dfnl[N],dfnr[N],times;
void dfs0(int u,int F){
sz[u]=1,dep[u]=dep[F]+1,big[u]=secbig[u]=0;
dfnl[u]=++times;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v!=F){
fa[v]=u,dfs0(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[big[u]])big[u]=v;
else if(sz[v]>sz[secbig[u]])secbig[u]=v;
}
}
dfnr[u]=times;
}
namespace llt{
vector<int> a;
void dfs1(int u,int pre){
a.push_back(u);
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v!=pre)dfs1(v,u);
}
}
void solve(){
int st;
for(int i=1;i<=n;++i){
if(deg[i]==1)st=i;
}
// cout<<st<<'\n';
a.clear();
dfs1(st,0);
ll res=0;
for(int i=0;i<n-1;++i){
if(i%2==1)res+=a[i/2]+a[i/2+1];
else res+=a[i/2];
++i;
if((n-i)%2==1)res+=a[n-1-(n-1-i)/2];
else res+=a[n-1-(n-1-i)/2]+a[n-1-(n-1-i)/2-1];
--i;
}
cout<<res<<'\n';
}
};
int U[N],V[N];
typedef pair<int,int> pii;
bool in(int u,int v){ // whether u in subtree(v)
return dfnl[v]<=dfnl[u]&&dfnl[u]<=dfnr[v];
}
namespace gen{
int W[N],secW[N],mins[N];
void solve(){
ll res=0;
for(int i=1;i<=n;++i)mins[i]=2e9,W[i]=secW[i]=0;
for(int u=1;u<=n;++u){
for(int v=u;v!=0;v=fa[v]){
int k=max(sz[big[u]],sz[v]-sz[u]);
if(k<mins[v])mins[v]=k,W[v]=u,secW[v]=0;
else if(k==mins[v])secW[v]=u;
}
}
for(int k=1;k<n;++k){
int u=U[k],v=V[k];
if(dep[u]>dep[v])swap(u,v); res+=W[v]+secW[v];
//cout<<"out of "<<v<<" "<<w<<' '<<secww<<'\n';
int del=sz[v];
sz[v]-=del;
vector<pii> Upd;
for(int w=fa[v];w!=0;w=fa[w]){
sz[w]-=del;
if(sz[big[w]]<sz[secbig[w]]||big[w]==v)Upd.push_back({w,big[w]}),big[w]=secbig[w];
}
int minS=2e9,ww,secww=0;
for(int w=1;w!=0;w=big[w]){
int maxS=0;
for(int i=head[w];i;i=e[i].nxt){
int p=e[i].v;
if(p!=fa[w]&&!in(p,v))maxS=max(maxS,sz[p]);
}
maxS=max(maxS,n-del-sz[w]);
if(maxS<minS)ww=w,minS=maxS;
else if(maxS==minS)secww=w;
}
for(int w=v;w!=0;w=fa[w])sz[w]+=del;
for(pii p:Upd)big[p.first]=p.second;
res+=ww+secww;
Upd.clear();
}
cout<<res<<'\n';
memset(W,0,sizeof W);
memset(secW,0,sizeof secW);
memset(mins,0,sizeof mins);
}
};
signed main() {
ios :: sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;cin>>T;
while(T--){
cin>>n;
for(int i=1;i<n;++i){
int u,v;cin>>u>>v; U[i]=u,V[i]=v;
add(u,v),add(v,u);
++deg[u],++deg[v];
}
dfs0(1,0);
int cnt1=0,cnt2=0;
for(int i=1;i<=n;++i)cnt1+=(deg[i]==1),cnt2+=(deg[i]==2);
if(cnt1==2&&cnt2==n-2)llt::solve();
else gen::solve();
memset(e,0,sizeof e);
memset(head,0,sizeof head); cnt=0;
memset(deg,0,sizeof deg);
memset(dep,0,sizeof dep);
memset(sz,0,sizeof sz);
memset(big,0,sizeof big);
memset(secbig,0,sizeof secbig);
memset(fa,0,sizeof fa);
memset(dfnl,0,sizeof dfnl);
memset(dfnr,0,sizeof dfnr);
times=0;
// for(int i=1;i<=n;++i)head[i]=deg[i]=dfnl[i]=dfnr[i]=dep[i]=fa[i]=big[i]=secbig[i]=sz[i]=0; cnt=times=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 0ms
memory: 24952kb
input:
5 7 1 6 2 4 5 3 3 7 6 4 4 7 7 1 5 2 7 3 6 4 7 6 5 5 7 7 3 2 2 7 4 7 5 1 1 7 6 7 7 1 4 2 7 3 4 4 6 6 5 5 7 7 2 1 3 6 5 7 6 1 1 4 4 7
output:
73 78 71 78 61
result:
ok 5 number(s): "73 78 71 78 61"
Test #2:
score: 5
Accepted
time: 6ms
memory: 24656kb
input:
5 7 2 1 3 1 1 5 4 5 5 6 6 7 7 1 6 2 3 4 5 6 5 5 3 3 7 7 4 7 5 3 6 2 2 1 1 3 3 7 7 4 2 2 1 1 5 5 3 3 7 6 7 7 5 2 6 1 1 2 2 3 3 4 4 7
output:
64 64 61 69 60
result:
ok 5 number(s): "64 64 61 69 60"
Test #3:
score: 5
Accepted
time: 9ms
memory: 24716kb
input:
5 199 7 46 10 170 17 190 23 22 22 67 31 13 13 116 34 100 36 91 37 195 39 88 40 139 43 187 47 158 48 83 49 128 50 30 54 26 26 169 59 85 60 178 65 191 73 28 28 41 75 56 76 82 77 133 80 55 55 44 81 150 87 92 89 74 90 110 91 123 92 163 93 42 42 44 44 180 95 35 35 66 66 161 96 176 97 3 98 140 101 38 103 ...
output:
54835 58386 59276 37467 56268
result:
ok 5 number(s): "54835 58386 59276 37467 56268"
Test #4:
score: 5
Accepted
time: 3ms
memory: 24960kb
input:
5 199 1 169 4 29 5 117 8 30 14 17 15 71 16 180 18 103 20 193 24 38 29 100 32 12 12 39 33 23 23 108 35 40 36 139 37 40 46 95 49 112 50 106 51 108 52 58 53 151 56 176 57 22 60 184 68 39 70 58 58 67 67 25 71 47 74 170 75 114 76 31 31 34 80 90 81 62 82 83 83 100 85 77 92 146 94 146 98 96 96 156 99 195 1...
output:
49937 58078 35079 56997 50641
result:
ok 5 number(s): "49937 58078 35079 56997 50641"
Test #5:
score: 0
Wrong Answer
time: 3ms
memory: 24944kb
input:
5 199 6 193 7 82 9 39 12 29 13 180 14 101 17 172 19 108 35 66 36 156 42 129 49 55 52 40 54 28 28 112 58 136 61 53 53 74 63 179 65 33 33 110 68 4 71 127 75 4 4 167 76 197 77 150 79 31 31 197 82 18 18 165 86 187 87 178 88 143 90 196 92 66 66 166 94 5 5 78 78 140 95 130 97 154 99 169 101 185 103 74 74 ...
output:
60385 55491 50093 50291 50868
result:
wrong answer 2nd numbers differ - expected: '55734', found: '55491'
Test #6:
score: 0
Wrong Answer
time: 7ms
memory: 24728kb
input:
5 1999 3 1248 6 1223 9 1663 12 360 14 1607 15 530 16 847 21 1010 25 733 28 947 32 1077 34 821 35 674 39 1017 40 967 45 430 50 529 51 1012 52 148 53 719 54 293 55 609 57 1047 58 582 60 142 66 1280 67 1530 68 717 69 70 70 746 71 1874 75 218 76 205 77 1498 80 1185 90 1026 94 711 95 385 98 1165 99 277 1...
output:
4415301 5174782 4890818 3652307 5974061
result:
wrong answer 3rd numbers differ - expected: '4892782', found: '4890818'
Test #7:
score: 0
Wrong Answer
time: 12ms
memory: 24964kb
input:
5 1999 1 243 2 1365 7 1088 11 1076 12 1102 15 9 18 516 20 1854 22 82 23 1532 25 809 28 973 31 1873 32 113 39 280 43 1138 44 1225 46 1289 47 948 48 814 52 863 54 1950 55 175 61 178 63 1081 66 282 68 1618 70 1306 71 314 74 771 80 406 81 565 82 777 83 1701 86 1247 87 1505 88 1816 90 77 94 308 96 1269 9...
output:
5138639 3958027 3122055 3472479 5741008
result:
wrong answer 2nd numbers differ - expected: '3952166', found: '3958027'
Test #8:
score: 5
Accepted
time: 14ms
memory: 24732kb
input:
5 1999 4 1262 6 1067 10 316 13 1983 14 610 15 945 18 192 22 532 24 688 29 385 30 1545 31 1061 37 1970 38 335 39 487 40 475 44 72 45 1468 47 470 49 873 50 1173 51 88 55 1366 56 1107 57 751 59 1340 60 636 63 1172 65 387 66 1286 69 681 70 489 75 1354 76 1958 77 910 78 1423 80 1696 81 328 83 1001 84 147...
output:
4417145 3733704 3195185 5409231 5428398
result:
ok 5 number(s): "4417145 3733704 3195185 5409231 5428398"
Test #9:
score: 5
Accepted
time: 37ms
memory: 25420kb
input:
5 49991 19763 10669 10669 20671 20671 35073 35073 22011 22011 39334 39334 16893 16893 36734 36734 35671 35671 24827 24827 5929 5929 22587 22587 16048 16048 24772 24772 40459 40459 37541 37541 33520 33520 47457 47457 23144 23144 40864 40864 13214 13214 23056 23056 19449 19449 41897 41897 23570 23570 ...
output:
3748673669 3748672930 3748662702 3748645661 3748626824
result:
ok 5 number(s): "3748673669 3748672930 3748662702 3748645661 3748626824"
Test #10:
score: 5
Accepted
time: 36ms
memory: 25880kb
input:
5 49991 17601 40966 40966 35434 35434 29765 29765 29569 29569 11520 11520 8262 8262 21778 21778 21137 21137 21481 21481 5034 5034 29974 29974 12934 12934 47299 47299 14541 14541 5632 5632 8467 8467 39604 39604 38825 38825 24334 24334 5304 5304 47890 47890 17264 17264 819 819 31896 31896 49966 49966 ...
output:
3748681091 3748673027 3748632619 3748681188 3748655226
result:
ok 5 number(s): "3748681091 3748673027 3748632619 3748681188 3748655226"
Test #11:
score: 5
Accepted
time: 37ms
memory: 26332kb
input:
5 49991 17601 40966 40966 35434 35434 29765 29765 29569 29569 11520 11520 8262 8262 21778 21778 21137 21137 21481 21481 5034 5034 29974 29974 12934 12934 47299 47299 14541 14541 5632 5632 8467 8467 39604 39604 38825 38825 24334 24334 5304 5304 47890 47890 17264 17264 819 819 31896 31896 49966 49966 ...
output:
3748681091 3748673027 3748632619 3748681188 3748655226
result:
ok 5 number(s): "3748681091 3748673027 3748632619 3748681188 3748655226"
Test #12:
score: 5
Accepted
time: 1008ms
memory: 24664kb
input:
5 262143 146770 175612 146770 60732 175612 53530 175612 157976 60732 129972 60732 8262 53530 208343 53530 241810 157976 54045 157976 125169 129972 77022 129972 12934 8262 139684 8262 132308 208343 202200 208343 113975 241810 96796 241810 231714 54045 214868 54045 206276 125169 254469 125169 177432 7...
output:
84574742390 109565525676 65160768012 75913832852 75507381681
result:
ok 5 number(s): "84574742390 109565525676 65160768012 75913832852 75507381681"
Test #13:
score: 5
Accepted
time: 1005ms
memory: 24948kb
input:
5 262143 79000 96360 79000 91210 96360 171486 96360 192733 91210 113737 91210 175804 171486 165391 171486 148872 192733 50082 192733 84694 113737 124373 113737 151080 175804 28512 175804 195077 165391 185052 165391 170088 148872 3444 148872 159445 50082 261317 50082 44496 84694 224968 84694 158634 1...
output:
69299203766 69180977724 60241673382 85059049735 74362083283
result:
ok 5 number(s): "69299203766 69180977724 60241673382 85059049735 74362083283"
Test #14:
score: 5
Accepted
time: 952ms
memory: 24720kb
input:
5 262143 80668 139094 80668 22182 139094 143143 139094 134409 22182 60477 22182 162265 143143 227503 143143 1560 134409 85115 134409 185254 60477 128516 60477 35671 162265 138714 162265 205852 227503 211287 227503 118458 1560 122509 1560 173622 85115 161651 85115 200495 185254 33882 185254 63113 128...
output:
66071449320 112893418366 81195207726 81558798680 81341220820
result:
ok 5 number(s): "66071449320 112893418366 81195207726 81558798680 81341220820"
Test #15:
score: 5
Accepted
time: 953ms
memory: 24756kb
input:
5 262143 134055 112409 134055 173361 112409 142281 112409 158535 173361 29319 173361 78805 142281 38595 142281 58788 158535 138588 158535 255716 29319 35295 29319 233057 78805 136424 78805 137397 38595 21026 38595 233348 58788 150537 58788 159016 138588 250059 138588 154684 255716 102400 255716 1721...
output:
89386489871 104106025313 77493369473 75581305725 100017003326
result:
ok 5 number(s): "89386489871 104106025313 77493369473 75581305725 100017003326"
Test #16:
score: 0
Time Limit Exceeded
input:
5 99995 259 33949 24431 46214 70531 47343 30948 7638 78110 39021 66676 61827 68226 23740 447 9624 73693 93832 51936 60316 70665 71118 34707 39970 79419 24476 46318 73713 13318 81365 203 61537 15901 1647 15871 94799 2179 98495 44986 18479 742 59873 21425 22057 53003 83148 1938 71642 96209 12362 85249...
output:
result:
Test #17:
score: 0
Time Limit Exceeded
input:
5 199995 138306 178143 126692 90580 195249 41160 183019 129512 30948 65522 174231 160304 62697 53062 34707 136319 119438 13105 86673 47289 21484 63852 135033 177492 88820 41556 127597 11667 38660 9688 20532 47907 199457 156229 19863 119502 124210 148366 53003 31552 130020 22624 138539 78394 125197 1...
output:
result:
Test #18:
score: 0
Time Limit Exceeded
input:
5 199995 175791 121334 164012 66489 62892 65680 158225 14074 191070 165961 53626 106523 110768 25732 168560 2805 33591 198570 25518 182266 193051 47804 171184 195743 175898 49560 105280 165577 137932 33634 75375 129044 109365 164280 125769 147064 170883 197513 162323 100475 47461 181353 80970 151789...
output:
result:
Test #19:
score: 0
Time Limit Exceeded
input:
5 299995 150147 169846 139337 63773 185064 212052 77320 23419 156180 162102 63142 20120 225131 146038 212549 20016 46406 42169 286305 140266 13104 5614 270163 227814 148262 9967 27434 252557 217908 105623 138578 103358 36469 211475 18717 236263 13867 41398 91671 91198 263412 104644 167079 204277 187...
output:
result:
Test #20:
score: 0
Time Limit Exceeded
input:
5 299995 284734 56982 179603 60848 62377 129299 263286 294600 275034 103994 101598 17127 4082 162607 101427 121742 51111 114853 74024 22822 107874 97512 145157 37145 76679 153213 103753 6327 1108 29718 51455 213273 40667 290500 12308 112958 39082 104741 25356 54011 282325 107031 79497 19021 194864 2...