QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125962 | #6267. Triples of Cows | pandapythoner | 50 | 467ms | 4472kb | C++20 | 4.1kb | 2023-07-18 02:59:05 | 2023-07-18 02:59:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rnd(234);
const ll inf = 1e18;
struct DSU {
int n;
vector<int> t;
void build(int _n) {
n = _n;
t.resize(n);
for (int i = 0; i < n; i += 1) {
t[i] = i;
}
}
int get(int v) {
if (t[v] == v) {
return v;
}
return (t[v] = get(t[v]));
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
return;
}
t[a] = b;
}
};
struct grp {
set<int> usd;
set<int> not_usd;
ll sm_pwr = 0;
int sz() {
return (int)usd.size() + (int)not_usd.size();
}
int usd_sz() {
return (int)usd.size();
}
};
int n;
vector<vector<int>> g;
vector<ll> rs;
vector<grp> grps;
DSU dsu;
vector<int> cur_grp;
vector<ll> pwr;
vector<int> dltd;
void unlink(int v) {
int t = cur_grp[v];
if (t == -1) {
return;
}
cur_grp[v] = -1;
grps[t].usd.erase(v);
grps[t].not_usd.insert(v);
grps[t].sm_pwr -= pwr[v];
grps[t].sm_pwr -= grps[t].usd_sz() * 2;
pwr[v] += grps[t].sz() - 1;
}
void link(int v, int t) {
if (cur_grp[v] == t) {
return;
}
unlink(v);
pwr[v] -= grps[t].sz() - 1;
grps[t].sm_pwr += grps[t].usd_sz() * 2;
grps[t].sm_pwr += pwr[v];
grps[t].not_usd.erase(v);
grps[t].usd.insert(v);
cur_grp[v] = t;
}
void join(int v, int t, ll &sm) {
unlink(v);
sm -= pwr[v] * (pwr[v] - 1);
pwr[v] += grps[t].sz();
sm += pwr[v] * (pwr[v] - 1);
grps[t].not_usd.insert(v);
sm += grps[t].sm_pwr * 2;
link(v, t);
}
void unjoin(int v, int t, ll &sm) {
unlink(v);
sm -= grps[t].sm_pwr * 2;
grps[t].not_usd.erase(v);
sm -= pwr[v] * (pwr[v] - 1);
pwr[v] -= grps[t].sz();
sm += pwr[v] * (pwr[v] - 1);
}
void link_all(int t) {
while (!grps[t].not_usd.empty()) {
int v = *grps[t].not_usd.begin();
link(v, t);
}
}
void solve() {
ll sm = 0;
pwr.assign(n, 0);
for (int i = 0; i < n; i += 1) {
ll f = (int)g[i].size();
pwr[i] = f;
sm += f * (f - 1);
}
grps.assign(n, grp());
dsu.build(n);
cur_grp.assign(n, -1);
dltd.assign(n, false);
rs.resize(n);
for (int i = 0; i < n; i += 1) {
rs[i] = sm;
dltd[i] = true;
int v = i;
for (auto to : g[v]) {
if (dltd[to]) {
int t = dsu.get(to);
link_all(t);
unjoin(v, t, sm);
} else {
unlink(to);
unlink(v);
sm -= pwr[to] * (pwr[to] - 1);
pwr[to] -= 1;
sm += pwr[to] * (pwr[to] - 1);
sm -= pwr[v] * (pwr[v] - 1);
pwr[v] -= 1;
sm += pwr[v] * (pwr[v] - 1);
}
}
for (auto to : g[v]) {
if (dltd[to]) {
int t = dsu.get(to);
link_all(t);
vector<int> x;
while (!grps[t].usd.empty()) {
int u = *grps[t].usd.begin();
grps[t].usd.erase(grps[t].usd.begin());
x.push_back(u);
unjoin(u, t, sm);
}
for (auto u : x) {
join(u, v, sm);
}
dsu.merge(t, v);
} else {
join(to, v, sm);
}
}
}
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n;
g.assign(n, vector<int>());
for (int i = 0; i < n - 1; i += 1) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
solve();
for (int i = 0; i < n; i += 1) {
cout << rs[i] << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 3496kb
input:
3 1 2 2 3
output:
2 0 0
result:
ok 3 lines
Test #2:
score: 5
Accepted
time: 1ms
memory: 3500kb
input:
4 1 2 1 3 1 4
output:
6 6 0 0
result:
ok 4 lines
Test #3:
score: 5
Accepted
time: 1ms
memory: 3528kb
input:
5 3 5 5 1 1 4 1 2
output:
8 10 2 0 0
result:
ok 5 lines
Test #4:
score: 5
Accepted
time: 0ms
memory: 3572kb
input:
500 11 3 327 355 314 180 428 417 349 438 319 16 322 365 430 4 116 384 95 469 476 16 96 444 330 323 282 380 179 365 416 151 361 34 422 417 122 440 206 264 169 381 237 420 167 47 118 421 442 149 474 112 135 359 230 221 178 391 39 464 326 105 198 24 33 419 455 240 120 50 406 134 124 32 186 424 6 157 21...
output:
1970 1960 1958 1956 2050 2046 2044 2042 2040 2038 2034 2032 2028 2026 2022 2020 2076 2080 2090 2074 2066 2058 2056 2054 2058 2048 2046 2048 2046 2044 2042 2040 2052 2050 2082 2070 2068 2108 2106 2102 2100 2106 2104 2110 2108 2106 2120 2126 2122 2134 2166 2162 2130 2122 2120 2114 2106 2102 2100 2094 ...
result:
ok 500 lines
Test #5:
score: 5
Accepted
time: 0ms
memory: 3580kb
input:
500 117 273 162 26 468 332 44 21 227 163 368 215 241 403 60 319 297 303 376 139 14 134 438 452 8 65 195 48 230 188 475 485 152 136 35 375 272 487 72 242 208 180 330 157 75 487 278 159 110 497 364 339 255 73 405 16 463 368 26 320 226 377 149 410 240 436 439 56 43 422 37 287 186 228 347 248 294 435 67...
output:
1952 1950 1942 1932 1934 1928 1920 1914 1912 1910 1908 1906 1904 1898 1892 1932 2080 2070 2216 2212 2210 2238 2236 2234 2232 2230 2242 2330 2326 2314 2312 2324 2322 2372 2386 2384 2394 2392 2388 2386 2380 2378 2376 2394 2392 2390 2396 2402 2412 2410 2406 2402 2400 2394 2406 2396 2538 2534 2588 2586 ...
result:
ok 500 lines
Test #6:
score: 5
Accepted
time: 248ms
memory: 4344kb
input:
5000 192 2280 866 1993 3318 2270 866 4634 1062 1191 1616 1183 3798 951 866 1590 4607 3400 866 4962 1816 4504 468 325 4569 1736 866 1130 2690 866 4389 866 4240 1120 1932 1614 2487 3373 866 1903 1261 2826 219 1064 44 4360 866 1333 866 1636 2726 2577 2130 866 4693 4818 1190 866 3319 2069 1424 240 1113 ...
output:
4926576 4926574 4926572 4926566 4926564 4926560 4926554 4922120 4922118 4917686 4917684 4917680 4913250 4908822 4908820 4904394 4904356 4904380 4904378 4904376 4904374 4904372 4904370 4899946 4899970 4895548 4895546 4891126 4886708 4886706 4886704 4882288 4877874 4877872 4877870 4877868 4873456 4869...
result:
ok 5000 lines
Test #7:
score: 5
Accepted
time: 467ms
memory: 4472kb
input:
5000 1026 19 4489 1374 1374 976 2917 317 2875 19 3741 2419 3056 444 19 1591 1983 19 4825 1374 2444 1374 2937 19 1374 2301 1246 1374 19 4829 19 3893 1374 3084 19 2482 19 3772 19 2434 4565 2642 3810 3291 1650 4033 1374 3824 4103 3281 19 852 19 2561 1018 112 1374 2096 1661 1374 19 280 19 3695 356 2671 ...
output:
7126950 7122520 7118092 7118090 7113664 7113652 7113650 7110676 7107704 7103280 7100310 7095888 7091468 7088500 7084082 7079666 7075252 7070840 7066430 10708504626 10708504624 10708504634 10693938346 10679385276 10664845418 10664842452 10650315800 10650315798 10650315796 10635802344 10635799380 1062...
result:
ok 5000 lines
Test #8:
score: 5
Accepted
time: 113ms
memory: 4080kb
input:
5000 3656 2617 1546 2617 3883 2531 487 302 2617 1655 2617 3150 684 4158 2641 4762 2552 2300 2617 2415 3766 1485 3401 155 2458 2617 1941 1219 2617 4612 990 4670 4551 1784 2617 3315 2617 3058 2617 4611 3379 2555 1745 1914 2617 4652 3820 1368 557 4236 4777 2667 774 4214 4592 961 3468 754 539 3369 2390 ...
output:
5011042 5006584 5006580 5006538 5002082 5002074 4997620 4997618 4997612 4997618 4997616 4993164 4988714 4988712 4988710 4988840 4984392 4984110 4984106 4979660 4975216 4970774 4970770 4970768 4970766 4970762 4966322 4961884 4961904 4957468 4953034 4948602 4948598 4948588 4944158 4939730 4939728 4935...
result:
ok 5000 lines
Test #9:
score: 5
Accepted
time: 171ms
memory: 4020kb
input:
5000 1963 1714 2089 1714 1958 1714 1174 516 2468 1714 4013 780 542 1714 1714 921 1714 494 2799 3447 1714 4167 2663 1377 4753 58 3643 4557 1714 623 1714 2198 4997 1992 1518 722 1714 1367 2723 4870 4103 2072 1714 4384 4299 2533 433 969 1714 4644 1253 1652 4838 2735 1508 1714 596 4817 2465 3850 3287 23...
output:
5108150 5103636 5099124 5099122 5099116 5099086 5099084 5099076 5094566 5094564 5094562 5094560 5094556 5090048 5085542 5081038 5076536 5076532 5076490 5071990 5067492 5067490 5062994 5058500 5054008 5049518 5049516 5049514 5045026 5045066 5045064 5040578 5040626 5036142 5036140 5031658 5027178 5022...
result:
ok 5000 lines
Test #10:
score: 5
Accepted
time: 229ms
memory: 4108kb
input:
5000 1952 4980 1776 4362 3887 1033 1033 4047 523 4980 357 1033 1033 941 4631 1033 1033 3253 960 4980 1033 2582 4980 887 984 2694 3715 4956 4728 1376 4980 3197 2996 4357 1490 1033 1033 2798 9 1033 4980 1084 1033 3349 2554 4980 204 1037 1033 4660 3004 1249 3817 2960 1033 1761 1033 2152 1033 1740 1033 ...
output:
7200814 7197878 7193392 7190458 7190456 7187524 7183040 7178558 7175628 7171148 7168220 7163742 7159266 7154792 7151866 7147394 7147226 7142756 7138288 7133822 7130898 7126434 7126428 7121966 7117506 7113048 7108592 7108598 7105676 7101222 7096770 7092320 7087872 7087870 7087868 7087866 7084946 7080...
result:
ok 5000 lines
Test #11:
score: 0
Time Limit Exceeded
input:
200000 180343 93112 133794 25463 15259 32390 161405 75514 53906 75514 31334 75514 145757 75514 91720 75514 113316 10952 75514 24136 30812 144626 84587 109632 28810 104546 75514 162784 73000 185642 75514 12153 75514 30841 74493 76012 177125 150153 100571 107545 118366 139210 10965 75514 21955 75514 7...
output:
result:
Test #12:
score: 0
Time Limit Exceeded
input:
200000 156524 164378 63451 181093 142236 156524 156524 91309 195366 63451 63451 54126 62067 43431 48083 17306 156524 139870 156524 79853 63451 118039 63451 10162 116377 114109 82955 48083 17725 63451 1075 156524 77519 156524 156524 118428 63451 126448 108533 156524 133574 156524 63451 10239 119214 1...
output:
result:
Test #13:
score: 0
Time Limit Exceeded
input:
200000 128878 43530 11660 61275 93778 135514 128878 65607 105027 128878 99196 126852 121316 119378 12994 121964 128878 157185 186752 128878 66811 97145 166913 95469 188773 91487 128878 164320 52546 128878 173916 128878 180121 126852 174374 128878 123398 126852 123003 128878 109982 128878 106656 1288...
output:
result:
Test #14:
score: 0
Time Limit Exceeded
input:
200000 75292 57657 154653 141350 140800 57657 57657 162746 124811 59959 154653 89884 57657 25957 60896 67370 183207 26852 57657 43172 188291 57657 154653 102124 57657 157547 132684 57657 154653 143124 124811 188592 57657 109286 66896 87163 57657 30111 154653 131248 107225 57657 139132 29848 181221 1...
output:
result:
Test #15:
score: 0
Time Limit Exceeded
input:
200000 48508 52931 115469 53334 53334 109332 55615 111128 160153 129069 81733 149640 69172 127972 53334 71755 107633 53334 92200 144859 191346 61711 192310 131522 144360 89737 112809 69429 24034 53334 8244 53334 134631 53334 53334 157868 179139 39842 112773 53334 131063 78546 95501 143642 50916 5333...
output:
result:
Test #16:
score: 0
Time Limit Exceeded
input:
200000 189585 140451 189585 21397 97507 124392 189551 66778 35172 110107 34572 137133 189585 175319 68631 185609 189585 113509 195998 52967 125268 189585 189585 38545 195154 62941 22458 189585 111738 150491 181476 40967 189585 175600 183092 164108 2191 91624 189585 98869 104639 189585 189585 11160 1...
output:
result:
Test #17:
score: 0
Time Limit Exceeded
input:
200000 52744 169423 21958 49886 107574 14898 179360 193309 52744 173255 11649 194058 52744 37299 185040 52744 173083 16183 45172 52744 111564 52744 118979 40378 16515 40367 116092 15886 21781 52744 52744 18485 155320 52744 164746 118979 52744 178385 119944 52744 183596 148338 97026 100365 154834 159...
output:
result:
Test #18:
score: 0
Time Limit Exceeded
input:
200000 98188 97568 97568 139023 97568 145232 43972 86482 97568 76922 152654 113293 97568 187093 14560 97568 130417 25367 180256 26394 70931 175568 97568 46170 102988 88140 152897 80698 151625 97568 28288 28935 97568 184813 46246 97568 126910 118530 120407 37832 97568 130045 101579 48512 176793 92123...
output:
result:
Test #19:
score: 0
Time Limit Exceeded
input:
200000 82514 160608 129118 74477 117781 160275 32607 77450 33384 83542 177617 190138 33384 6337 94051 17548 75494 139091 22526 33384 194668 33384 9010 33384 32463 33384 33384 81726 33384 199774 68788 33384 52181 175598 18372 33384 160069 33384 75532 3294 154793 166505 33384 84585 52141 162634 138451...
output:
result:
Test #20:
score: 0
Time Limit Exceeded
input:
200000 123057 85473 181057 139168 139374 155981 97580 66941 139374 26235 92577 28176 53567 44615 139374 158256 139374 111698 139374 7486 167825 194890 39334 139374 124253 139374 99441 512 41950 157947 45135 122712 19652 139374 145589 139374 77187 106020 111608 128762 139374 163048 67917 139374 51654...