QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#22438 | #1757. Grand Central Station | asoul# | AC ✓ | 518ms | 132268kb | C++20 | 3.8kb | 2022-03-09 18:22:35 | 2022-04-30 01:04:37 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i ++)
#define per(i, r, l) for(int i = (r); i >= (l); i --)
#define trv(i, u, v) for(int i = head[u], v = e[i].to; i; v = e[i = e[i].nxt].to)
#define fi first
#define se second
#define all(s) s.begin(), s.end()
#define sz(s) (int)(s.size())
#define lb(s) ((s) & -(s))
#define pb push_back
using namespace std;
typedef long long ll;
typedef __int128 u64;
mt19937_64 hua(time(0));
template<typename T> inline bool chkmx(T &x, T y) {return x < y ? x = y, 1 : 0;}
template<typename T> inline bool chkmn(T &x, T y) {return y < x ? x = y, 1 : 0;}
template<int T> using A = array<int, T>;
inline int read() {
int x = 0, f = 1; char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = 0;
for(; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return f ? x : -x;
}
const int maxn = 3e5;
const u64 mod[2] = {999999999999989, 999999999999883};
inline u64 add(u64 a, u64 b, u64 mod) {return (a += b) >= mod ? a - mod : a;}
inline u64 sub(u64 a, u64 b, u64 mod) {return (a -= b) < 0 ? a + mod : a;}
inline u64 mul(u64 a, u64 b, u64 mod) {return a * b % mod;}
struct mint {
u64 x, y;
mint(u64 _x = 0, u64 _y = 0) {x = _x, y = _y;}
friend mint operator + (mint x, mint y) {
return mint{add(x.x, y.x, mod[0]), add(x.y, y.y, mod[1])};
}
friend mint operator - (mint x, mint y) {
return mint{sub(x.x, y.x, mod[0]), sub(x.y, y.y, mod[1])};
}
friend mint operator * (mint x, mint y) {
return mint{mul(x.x, y.x, mod[0]), mul(x.y, y.y, mod[1])};
}
friend bool operator < (mint x, mint y) {
return x.x == y.x ? x.x < y.x : x.y < y.y;
}
friend bool operator == (mint x, mint y) {
return x.x == y.x && x.y == y.y;
}
void debug() {
cout << (ll) x << ' ' << (ll) y << '\n';
}
}f[maxn + 5], g[maxn + 5], pw[maxn + 5], ipw[maxn + 5], pre[maxn + 5], suf[maxn + 5], spw[maxn + 5];
int prs[maxn + 5], sus[maxn + 5];
typedef pair<mint, pair<int, int>> pp;
const mint bs = {200131, 302139};
const mint ibs = {605888143266154, 498373265285125};
inline u64 fpw(u64 a, u64 b, u64 mod, u64 ans = 1) {
for(; b; b >>= 1, a = mul(a, a, mod)) if(b & 1) ans = mul(ans, a, mod);
return ans;
}
int n;
vector<int> G[maxn + 5];
vector<mint> st;
int siz[maxn + 5];
void dfs1(int u, int par) {
f[u] = {0, 0};
vector<pp> s;
for(auto v : G[u]) if(v != par) {
dfs1(v, u);
s.pb({f[v], {siz[v], v}});
}
sort(all(s));
siz[u] = 1;
for(auto x : s) f[u] = f[u] + x.fi * pw[siz[u]], siz[u] += x.se.fi;
f[u] = f[u] + spw[siz[u] - 1];
// cout << u << ":\n"; f[u].debug();
}
void dfs2(int u, int par) {
// cout << u << ":\n"; g[u].debug();
vector<pp> s;
s.pb({{0, 0}, {0, 0}});
s.pb({g[u], {n - siz[u], u}});
for(auto v : G[u]) if(v != par) {
s.pb({f[v], {siz[v], v}});
}
sort(all(s));
auto calc = [&] (int l, int r) {
if(l > r) return mint{0, 0};
return (pre[r] - pre[l - 1]) * ipw[prs[l - 1]];
};
prs[0] = 1, pre[0] = {0, 0};
rep(i, 1, sz(s) - 1) {
prs[i] = prs[i - 1] + s[i].se.fi;
pre[i] = pre[i - 1] + s[i].fi * pw[prs[i - 1]];
}
// assert(prs[sz(s) - 1] == n);
st.pb(pre[sz(s) - 1]);
rep(i, 1, sz(s) - 1) {
if(s[i].se.se == u) continue;
g[s[i].se.se] = (calc(1, i - 1) * bs + calc(i + 1, sz(s) - 1) * pw[prs[i - 1]]) + spw[n - siz[s[i].se.se] - 1];
}
for(auto v : G[u]) if(v != par) dfs2(v, u);
}
int main() {
// freopen("in.txt", "r", stdin);
pw[0] = ipw[0] = spw[0] = {1, 1};
rep(i, 1, maxn) pw[i] = pw[i - 1] * bs, ipw[i] = ipw[i - 1] * ibs, spw[i] = spw[i - 1] + pw[i];
n = read();
rep(i, 2, n) {
int u = read(), v = read();
G[u].pb(v), G[v].pb(u);
}
dfs1(1, 1);
// rep(i, 1, n) {
// dfs1(i, i);
// st.pb(f[i]);
// }
// return 0;
dfs2(1, 1);
sort(all(st)), st.erase(unique(all(st)), st.end());
cout << sz(st) << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 47ms
memory: 76504kb
input:
1000 438 102 97 433 728 280 620 314 120 43 834 500 374 154 556 182 542 265 711 870 952 273 104 76 109 549 73 871 65 861 649 731 679 547 306 987 109 583 776 295 37 936 836 362 247 700 967 811 817 995 482 818 34 324 763 591 12 515 890 599 756 243 503 941 265 332 408 790 274 799 448 282 733 363 424 274...
output:
784
result:
ok single line: '784'
Test #2:
score: 0
Accepted
time: 47ms
memory: 76360kb
input:
1000 21 386 710 824 505 123 943 634 624 718 40 230 652 634 448 794 858 767 566 176 535 944 38 563 866 943 415 68 597 127 649 70 318 980 79 353 831 876 55 357 679 104 427 951 736 669 129 29 813 405 879 160 99 973 73 699 202 700 565 80 11 477 521 315 464 132 560 304 351 555 671 949 571 393 273 730 881...
output:
828
result:
ok single line: '828'
Test #3:
score: 0
Accepted
time: 86ms
memory: 79412kb
input:
38558 7003 23985 29307 29351 703 34035 30888 32096 30611 37877 1772 19028 8792 15844 28451 25209 27875 32487 22935 260 38261 22356 32425 35251 18290 23963 30763 31440 4689 20025 9076 37955 8185 17786 29703 28208 25142 26831 32543 25661 23763 30430 5261 25175 26576 22388 12988 12555 11220 24674 36527...
output:
38505
result:
ok single line: '38505'
Test #4:
score: 0
Accepted
time: 493ms
memory: 104888kb
input:
288354 232840 117312 162741 262522 279350 119278 10580 162741 73466 217320 179730 91475 86898 105936 281878 17577 121709 262578 98208 275496 143650 27167 234538 248941 92384 247459 104666 26510 172704 118571 271484 235172 284116 135213 115626 85372 195358 63709 270389 108426 132939 109287 250658 250...
output:
20984
result:
ok single line: '20984'
Test #5:
score: 0
Accepted
time: 170ms
memory: 83604kb
input:
95504 56881 88368 31237 22349 83053 4113 94662 74197 24572 60632 23472 20345 8246 29223 23161 8866 25614 84614 60063 73627 58372 46167 70693 37240 77485 40459 33403 37917 21426 70251 57562 16829 73827 66275 54331 61513 13503 68175 9671 61761 70917 52865 40374 64238 14471 5991 38004 68517 46785 68336...
output:
95373
result:
ok single line: '95373'
Test #6:
score: 0
Accepted
time: 57ms
memory: 76968kb
input:
3871 342 2313 2012 2027 2233 1840 3114 2620 3031 2284 871 1366 2037 3761 558 1370 2425 2794 1564 2478 435 1458 1066 1224 1894 1469 2856 253 696 3126 116 166 1901 15 1899 2224 834 2326 2970 2123 3595 398 2947 1780 1331 3348 2964 3679 1247 3590 3036 1128 2380 2739 1125 372 2663 1600 1812 2904 2770 190...
output:
3871
result:
ok single line: '3871'
Test #7:
score: 0
Accepted
time: 48ms
memory: 77012kb
input:
1469 1365 900 84 740 110 616 476 39 188 1194 1003 563 1058 325 531 866 1412 843 1162 1419 83 300 568 1174 911 69 58 611 1160 452 918 343 701 430 910 665 961 175 1213 752 414 334 811 474 421 9 385 1416 81 1116 159 694 820 792 379 1034 989 421 1302 840 511 685 781 422 228 893 1394 334 1082 972 1297 18...
output:
1469
result:
ok single line: '1469'
Test #8:
score: 0
Accepted
time: 323ms
memory: 90764kb
input:
175819 11277 113133 12609 95478 100262 100323 77490 39122 109858 16865 80865 142912 110636 48853 125281 136378 42758 12882 23846 32461 116890 33708 37129 65240 47518 165507 14820 164754 125913 159493 122149 47528 144074 13845 115620 46748 137806 7032 7542 63258 165381 72402 144152 119110 105231 3797...
output:
175267
result:
ok single line: '175267'
Test #9:
score: 0
Accepted
time: 50ms
memory: 76392kb
input:
20 8 11 3 4 13 4 14 5 7 11 5 6 16 15 1 19 13 18 7 15 16 17 5 8 12 2 12 1 9 14 14 20 10 17 2 10 9 18
output:
20
result:
ok single line: '20'
Test #10:
score: 0
Accepted
time: 39ms
memory: 76304kb
input:
20 12 2 8 3 1 20 19 7 18 1 7 16 14 9 5 9 6 17 8 17 4 7 15 16 13 6 2 3 15 10 14 11 4 11 10 20 2 18
output:
10
result:
ok single line: '10'
Test #11:
score: 0
Accepted
time: 478ms
memory: 132268kb
input:
300000 292135 15478 15478 136216 109185 15478 15478 242099 103220 103279 15478 22455 15478 102679 5604 15478 76086 103220 103220 208942 15478 179342 210978 15478 15478 115167 226522 15478 103220 209365 203552 103220 15478 235035 140119 15478 15478 249837 123062 103220 223686 15478 112203 15478 50845...
output:
2
result:
ok single line: '2'
Test #12:
score: 0
Accepted
time: 180ms
memory: 84060kb
input:
105794 76736 23390 93047 40011 46085 17858 95620 30454 72606 69064 81548 71375 89542 73664 66524 67299 7647 37844 58361 76223 77613 63623 83909 44822 68359 98225 1350 97362 67436 17260 73270 63852 52184 92453 719 36806 55855 22342 32085 36008 31772 17244 53334 39479 1098 81068 40235 33878 63798 4852...
output:
103031
result:
ok single line: '103031'
Test #13:
score: 0
Accepted
time: 463ms
memory: 102888kb
input:
276262 2946 75329 127924 249904 115653 154386 76098 136172 45205 262808 261950 65566 18564 90926 260195 97193 97843 190276 242702 153221 42507 191412 45653 73382 249361 117669 22884 217604 47983 262738 26356 6176 191734 82159 195561 61976 125595 97194 194242 149567 189389 186398 241528 54030 231489 ...
output:
108544
result:
ok single line: '108544'
Test #14:
score: 0
Accepted
time: 502ms
memory: 103560kb
input:
299619 218764 98442 206412 273853 26899 128877 109578 92945 287118 149372 253286 208830 263011 189024 226852 287954 101914 175066 297949 234307 93690 62374 170820 40304 148307 254782 23602 175276 33924 250988 179658 296711 29715 177000 229224 698 77108 200321 281241 210349 70706 238979 26453 37795 5...
output:
55663
result:
ok single line: '55663'
Test #15:
score: 0
Accepted
time: 518ms
memory: 103764kb
input:
298742 133751 176370 275294 156008 122306 209897 234377 197029 215464 212287 176520 210432 279094 280632 56085 280381 28133 197004 188123 129079 1275 265657 249362 185370 192548 246821 219928 278817 92546 286425 208499 15773 232775 46298 210432 14577 191251 197715 49602 164705 31992 105104 269294 25...
output:
5521
result:
ok single line: '5521'
Test #16:
score: 0
Accepted
time: 43ms
memory: 76404kb
input:
1000 39 652 523 560 749 120 297 37 571 685 337 882 78 870 561 438 947 123 814 964 280 75 534 314 942 928 916 494 813 644 914 249 789 356 462 366 93 472 221 243 741 168 683 103 207 963 537 573 522 967 909 578 738 631 52 327 772 935 422 472 216 665 689 837 617 973 90 896 894 80 308 822 162 102 164 281...
output:
801
result:
ok single line: '801'