QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#768008 | #2766. Unique Cities | _8_8_ | 0 | 616ms | 169888kb | C++20 | 2.9kb | 2024-11-20 23:32:24 | 2024-11-20 23:32:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (1 << 20) + 12, MOD = (int)1e9 + 7;
int n, m, a[N], vis[N], timer, p[N];
vector<int> g[N], gt[N];
bool is[N];
map<int, int> dist[N];
int f(int d) {
timer++;
vis[d] = timer;
int lst = d;
queue<int> q;
q.push(d);
dist[d][d] = 0;
while(!q.empty()) {
int v = q.front();
lst = v;
q.pop();
for(int to : g[v]) {
if(vis[to] != timer) {
dist[d][to] = dist[d][v] + 1;
q.push(to);
vis[to] = timer;
}
}
}
return lst;
}
int col[N], dep[N], mxd[N], res[N], d, d1, o, cr;
int zap = 0;
bool ok[N];
void dfs(int v, int pr = -1) {
mxd[v] = dep[v];
for(int to:g[v]) if(to != pr) {
gt[v].push_back(to);
dep[to] = dep[v] + 1;
dfs(to, v);
if(ok[to]) ok[v] = 1;
mxd[v] = max(mxd[v], mxd[to]);
}
if(v == d) ok[v] = 1;
}
vector<int> st;
set<int> cur[N];
void go(int v) {
if(dist[cr][v] > dist[o][v]) {
set<int> raz;
for(int i = 0; i < (int)st.size() && mxd[v] - dep[v] < dep[v] - dep[st[i]]; i++) {
raz.insert(a[st[i]]);
cur[v].insert(a[st[i]]);
}
res[v] += (int)raz.size();
}
if(gt[v].empty()) return;
int sz = (int)gt[v].size();
vector<int> p(sz), s(sz);
p[0] = mxd[gt[v][0]];
s[sz - 1] = mxd[gt[v][sz - 1]];
for(int i = 1; i < sz; i++) {
p[i] = max(p[i - 1], mxd[gt[v][i]]);
}
for(int i = sz - 2; i >= 0; i--) {
s[i] = max(s[i + 1], mxd[gt[v][i]]);
}
for(int i = 0; i < sz; i++) {
int to = gt[v][i];
int mx = dep[v];
if(i) mx = max(mx, p[i - 1]);
if(i < sz - 1) mx = max(mx, s[i + 1]);
mx -= dep[v];
vector<int> bf = st;
while(!st.empty() && dep[v] - dep[st.back()] <= mx) {
st.pop_back();
}
st.push_back(v);
go(to);
st = bf;
}
}
void solve(int root) {
for(int i = 1; i <= n; i++) {
gt[i].clear();
}
st.clear();
dep[root] = 1;
dfs(root);
go(root);
}
void test() {
cin >> n >> m;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
d = f(1), d1 = f(d);
f(d1);
// cout << d << ' ' << d1 << '\n';
o = d1;cr = d;
solve(d);
zap = 1;
o = d;cr = d1;
solve(d1);
// solve(4);
// solve(1);
for(int i = 1; i <= n; i++) {
cout << res[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 4
Accepted
time: 23ms
memory: 112196kb
input:
2 1 1 2 1 1
output:
1 1
result:
ok 2 lines
Test #2:
score: 4
Accepted
time: 11ms
memory: 112916kb
input:
1842 848 740 1093 1299 922 801 1560 265 1664 1043 65 1430 427 80 233 4 1238 1623 1473 1569 274 953 1485 1259 649 1671 1409 246 542 742 1517 720 1120 1527 1328 1167 1531 1056 1130 673 1222 192 980 1393 913 446 688 135 23 1065 1787 978 1481 1765 1720 310 202 1406 1451 475 523 104 774 1531 829 169 396 ...
output:
1 1 1 0 0 0 2 0 3 0 2 1 0 3 0 0 0 2 0 0 4 2 0 2 4 0 0 1 1 1 2 1 1 1 0 0 1 3 0 1 2 0 1 1 4 0 1 1 1 3 0 2 1 0 1 1 0 0 2 1 1 2 1 1 1 0 3 0 1 2 0 0 1 0 0 0 1 1 3 1 4 3 1 2 2 0 1 3 0 2 1 0 1 1 1 1 1 2 3 1 0 1 2 1 0 1 2 4 1 3 0 0 0 4 0 1 1 2 2 2 3 0 5 2 1 5 0 1 1 0 0 2 0 2 0 2 4 0 1 0 3 0 3 0 0 2 0 1 0 2 ...
result:
ok 1842 lines
Test #3:
score: 4
Accepted
time: 63ms
memory: 129796kb
input:
951 908 777 679 676 510 34 425 424 889 78 4 924 408 151 905 399 942 606 776 275 332 616 900 513 372 911 130 465 430 126 303 380 603 506 505 592 38 917 270 435 815 591 773 63 66 417 642 301 693 814 454 735 671 899 674 925 846 254 898 845 569 703 659 707 241 482 715 646 282 274 925 861 374 699 488 903...
output:
302 286 491 486 478 563 380 32 497 563 115 30 581 428 510 491 374 565 242 418 469 550 410 229 499 386 274 588 487 276 279 536 445 203 483 56 565 361 129 140 533 437 229 68 442 559 570 502 493 212 588 96 330 405 262 526 464 516 258 541 554 264 475 441 544 476 406 222 468 397 531 591 406 39 320 198 60...
result:
ok 951 lines
Test #4:
score: 0
Wrong Answer
time: 20ms
memory: 117528kb
input:
1817 365 1516 74 1811 1805 1036 311 1769 1663 1029 473 1596 310 337 888 324 426 385 315 1780 1684 1750 794 895 1029 1108 1194 566 613 489 1230 1727 1520 170 302 731 1171 591 295 1165 1233 304 835 1607 404 1035 1675 158 735 1553 1764 1390 1078 197 980 1176 1626 16 1218 1530 1146 1692 1477 1513 574 32...
output:
0 0 0 0 0 224 0 203 0 0 0 0 229 0 0 0 0 0 0 0 162 0 0 0 0 0 0 0 0 0 0 0 208 0 0 0 0 0 0 0 161 0 0 0 0 0 0 0 0 0 0 0 0 0 146 118 0 0 66 0 0 70 240 177 0 165 0 0 0 0 0 68 243 0 0 0 0 0 0 0 0 208 0 0 12 0 195 0 0 0 0 6 0 114 218 0 0 199 0 0 0 0 0 27 157 0 0 0 0 0 0 0 0 0 0 168 0 0 0 0 0 0 0 0 0 257 0 0...
result:
wrong answer 2nd lines differ - expected: '129', found: '0'
Subtask #2:
score: 0
Time Limit Exceeded
Test #33:
score: 32
Accepted
time: 391ms
memory: 144896kb
input:
115391 1 32067 50006 1710 5850 21016 66364 72998 34367 24783 10670 49950 93666 81835 81234 53480 68963 87357 43320 93905 30509 72528 92224 520 100511 54804 2917 58490 23858 93643 87530 90737 65205 60740 110812 9553 90266 70028 67222 108045 88982 35584 110286 53518 21733 108735 26404 108228 109796 92...
output:
1 1 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 115391 lines
Test #34:
score: 0
Time Limit Exceeded
input:
114976 1 74053 36053 62978 89255 32367 21913 113882 44280 60815 35782 107811 95272 109039 78845 22484 41688 1781 111596 111506 59375 19869 45586 84990 81214 38638 90205 14928 14370 10758 5465 87745 5949 66720 6357 76134 26466 91805 105936 31792 68220 74216 108462 60158 67410 69489 50297 74956 63776 ...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #50:
score: 32
Accepted
time: 616ms
memory: 169888kb
input:
157976 157976 20171 157173 44732 54119 107845 121149 109200 110309 82678 108206 89140 64200 36916 128759 3966 123760 92978 105716 43700 146126 14924 3429 107721 36095 94906 78173 97162 29552 119574 39605 25145 138492 16622 99431 60281 7949 76176 136644 75716 91518 127987 110605 77999 110960 101187 5...
output:
1 5 3 3 4 2 3 2 2 1 2 1 4 1 3 1 7 2 1 3 1 5 2 1 1 2 1 3 1 4 2 1 1 1 1 1 3 1 2 5 4 6 2 2 1 1 2 3 4 1 1 1 1 4 2 4 3 2 2 1 1 4 2 3 1 3 1 2 1 1 1 1 1 3 2 1 1 5 2 1 1 9 2 3 1 1 3 1 2 3 2 2 2 3 1 4 2 1 1 1 2 1 3 1 4 1 6 2 1 2 3 2 1 1 1 2 1 1 2 2 1 2 8 2 2 1 2 1 1 5 2 1 2 1 3 1 4 1 1 3 3 1 10 6 3 1 3 2 2 9...
result:
ok 157976 lines
Test #51:
score: 0
Time Limit Exceeded
input:
191205 191205 92326 99995 188142 63029 71973 50899 68069 72878 159479 103397 143191 93854 27574 146981 63427 186167 104978 177629 22355 188155 184293 15184 101908 143833 111535 144687 112209 61685 59401 128258 97717 87496 168079 130237 49319 190230 119632 98368 76193 184597 22790 136093 1080 80811 1...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%