ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#207173 | #3730. 路径覆盖 | tanghexuan | 100 | 6851ms | 46704kb | C++11 | 2.5kb | 2024-07-27 18:35:52 | 2024-07-27 20:10:22 |
answer
#include <bits/stdc++.h>
using namespace std;
static auto __fast_io = []()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
return 0;
}();
// #define DEBUGx
#ifdef DEBUGx
#define TRC_PG(fmt, args...) fprintf(stderr, "\033[1;32m TRC_PG(%s:%d):\t\033[0m" fmt, __func__, __LINE__, ##args)
#define TRC_PR(fmt, args...) fprintf(stderr, "\033[1;31m TRC_PR(%s:%d):\t\033[0m" fmt, __func__, __LINE__, ##args)
#define debug fprintf(stderr, "Passing [%s] in LINE %d\t\n", __func__, __LINE__)
#endif
#ifndef DEBUGx
#define TRC_PG(...)
#define TRC_PR(...)
#define debug
#endif
const int MAX = 2e5 + 10;
vector<int> child[MAX];
int dep[MAX];
int fa[MAX][30];
int cnt[MAX];
int p[MAX]; // 维护树上差分
int n, m;
void dfs(int u, int f, int deep)
{
fa[u][0] = f;
dep[u] = deep;
for (auto v : child[u])
{
if (v == f)
continue;
dfs(v, u, deep + 1);
}
}
int lca(int x, int y)
{
if (dep[x] < dep[y])
swap(x, y);
for (int i = 29; i >= 0; i--)
{
if (dep[x] - (1 << i) >= dep[y])
x = fa[x][i];
}
if (x == y)
return x;
for (int i = 29; i >= 0; i--)
{
if (fa[x][i] != fa[y][i])
{
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
int dfs2(int u, bool flag)
{
for (auto v : child[u])
{
if (v == fa[u][0])
continue;
p[u] += dfs2(v, false);
}
TRC_PR("p[%d] = %d %d\n", u, p[u], fa[u][0]);
return p[u];
}
void input()
{
cin >> n >> m;
int u, v;
for (int i = 1; i < n; i++)
{
cin >> u >> v;
child[u].push_back(v);
child[v].push_back(u);
}
dfs(1, 0, 0);
}
void init()
{
for (int j = 1; j < 30; j++)
{
for (int i = 1; i <= n; i++)
{
fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
}
}
void work()
{
int x, y;
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
// lca(x, y)
int l = lca(x, y);
p[x]++;
p[y]++;
p[l] -= 2;
TRC_PG("l = %d\n", l);
}
debug;
dfs2(1, true);
for (int i = 2; i <= n; i++)
{
TRC_PG("p[%d]=%d\n", i, p[i]);
cnt[p[i]]++;
}
}
void output()
{
for (int i = 0; i <= m; i++)
{
cout << cnt[i] << " ";
}
cout << "\n";
}
int main()
{
input();
init();
work();
output();
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 0ms
memory: 6136kb
input:
1000 1000 177 510 837 486 186 514 879 576 539 998 726 753 2 299 285 688 215 231 119 515 182 691 784 ...
output:
45 123 120 92 75 50 41 30 22 15 27 15 16 9 10 8 3 6 13 7 5 5 7 5 4 6 1 2 4 5 7 6 2 4 4 2 3 0 2 1 3 1...
result:
ok 1001 tokens
Test #2:
score: 5
Accepted
time: 4ms
memory: 6136kb
input:
1000 1000 805 382 148 659 394 856 487 66 879 291 857 494 719 202 141 780 455 246 83 312 563 4 822 47...
output:
40 122 140 110 59 55 42 31 25 23 13 12 8 10 17 12 8 13 10 6 9 6 4 8 2 1 2 4 5 3 3 4 6 3 4 3 0 1 1 1 ...
result:
ok 1001 tokens
Test #3:
score: 5
Accepted
time: 0ms
memory: 6140kb
input:
1000 1000 364 785 443 330 676 952 200 930 220 1000 716 317 826 696 79 509 272 158 271 949 267 188 28...
output:
58 116 104 103 78 62 44 19 27 32 15 11 14 12 15 7 7 10 8 4 7 4 5 4 2 2 2 1 2 5 5 1 6 1 3 4 3 6 2 4 2...
result:
ok 1001 tokens
Test #4:
score: 5
Accepted
time: 0ms
memory: 6140kb
input:
1000 1000 975 265 634 600 924 62 898 506 189 380 587 540 959 836 200 542 859 558 929 214 252 958 908...
output:
66 93 102 102 70 40 42 31 20 20 18 12 19 18 13 13 6 3 9 11 7 5 6 9 7 5 7 4 5 5 3 1 7 1 4 1 5 1 2 5 2...
result:
ok 1001 tokens
Test #5:
score: 5
Accepted
time: 0ms
memory: 6140kb
input:
1000 1000 263 358 162 528 555 642 304 627 788 315 749 516 451 64 111 977 222 135 670 589 623 148 250...
output:
46 110 99 110 79 44 46 35 20 25 21 17 10 12 13 12 9 3 11 5 5 7 5 9 3 6 10 1 3 4 8 5 2 8 4 1 2 2 2 0 ...
result:
ok 1001 tokens
Test #6:
score: 5
Accepted
time: 3ms
memory: 6136kb
input:
1000 1000 693 558 223 100 12 316 364 608 149 604 793 80 4 749 415 543 640 840 849 776 27 813 619 798...
output:
51 95 127 105 68 53 41 37 22 21 18 16 17 10 14 13 12 8 6 6 4 6 8 6 5 3 4 9 7 4 5 3 2 2 3 2 1 5 4 3 2...
result:
ok 1001 tokens
Test #7:
score: 5
Accepted
time: 339ms
memory: 37800kb
input:
200000 200000 152924 133355 132784 159949 48163 16199 107570 97541 108052 7611 121634 61497 40491 18...
output:
10512 22056 24296 20427 14769 10544 7761 6236 4996 4260 3672 3161 2627 2420 2194 1921 1758 1636 1487...
result:
ok 200001 tokens
Test #8:
score: 5
Accepted
time: 359ms
memory: 37760kb
input:
200000 200000 147949 131398 138160 49746 60026 2627 110862 120595 62030 171586 162532 147784 9808 16...
output:
10402 21912 24583 20104 14665 10469 7785 6218 5122 4154 3569 3107 2720 2361 2228 1899 1802 1574 1418...
result:
ok 200001 tokens
Test #9:
score: 5
Accepted
time: 437ms
memory: 37756kb
input:
200000 200000 18246 146321 152738 59315 13160 121883 79818 99769 172133 53584 44172 71493 152611 922...
output:
10546 22129 24732 19896 14755 10456 7727 6217 5014 4257 3624 3038 2715 2358 2152 1960 1779 1616 1439...
result:
ok 200001 tokens
Test #10:
score: 5
Accepted
time: 577ms
memory: 37812kb
input:
200000 200000 177068 157332 161550 30570 34766 134765 130545 145981 49533 61233 120580 103500 120552...
output:
10366 22004 24521 20243 14685 10480 7948 6143 4970 4263 3606 3154 2687 2431 2210 1936 1811 1667 1481...
result:
ok 200001 tokens
Test #11:
score: 5
Accepted
time: 592ms
memory: 37784kb
input:
200000 200000 81290 157046 174387 104800 168076 137362 101422 158473 114059 106215 172207 45206 6535...
output:
10421 22218 24402 20067 14460 10638 7833 6254 5062 4399 3611 3157 2781 2455 2173 1972 1815 1610 1533...
result:
ok 200001 tokens
Test #12:
score: 5
Accepted
time: 551ms
memory: 37804kb
input:
200000 200000 80458 68136 78172 113909 67739 14907 163591 44122 47776 5528 8314 105652 144363 48914 ...
output:
10483 22007 24570 19994 14649 10494 7858 6259 5105 4284 3559 3138 2846 2411 2197 1982 1801 1602 1518...
result:
ok 200001 tokens
Test #13:
score: 5
Accepted
time: 343ms
memory: 37804kb
input:
200000 200000 126535 165361 110140 165275 187945 23092 125642 132277 47001 160386 160529 654 196022 ...
output:
10568 22042 24353 20150 14725 10323 7947 6218 5050 4259 3611 3143 2699 2398 2127 1981 1784 1655 1486...
result:
ok 200001 tokens
Test #14:
score: 5
Accepted
time: 313ms
memory: 37764kb
input:
200000 200000 124605 132337 37126 155194 177080 45019 62577 123915 100781 98898 178482 75891 77362 1...
output:
10393 21879 24491 20298 14694 10389 7911 6160 5075 4177 3642 3083 2647 2450 2099 1980 1797 1574 1510...
result:
ok 200001 tokens
Test #15:
score: 5
Accepted
time: 351ms
memory: 37788kb
input:
200000 200000 16019 146675 78225 135779 18131 51731 161892 52507 42027 111152 81034 185338 122292 18...
output:
10432 21923 24450 20187 14651 10448 7909 6137 5054 4337 3656 3038 2718 2410 2221 1998 1789 1544 1423...
result:
ok 200001 tokens
Test #16:
score: 5
Accepted
time: 437ms
memory: 37828kb
input:
200000 200000 165721 192656 138852 49059 86153 31626 138928 128658 199703 43701 112683 140272 41586 ...
output:
10282 21983 24614 20177 14590 10371 7777 6197 5089 4216 3606 3132 2803 2463 2183 1930 1799 1656 1536...
result:
ok 200001 tokens
Test #17:
score: 5
Accepted
time: 494ms
memory: 46704kb
input:
200000 199999 163369 45918 160316 159985 114894 121112 165819 62891 183049 30002 39996 157895 71254 ...
output:
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 200000 tokens
Test #18:
score: 5
Accepted
time: 687ms
memory: 45640kb
input:
200000 200000 114571 129412 84680 111567 41875 21990 110441 49406 112540 167381 122214 82036 139727 ...
output:
0 2 1 0 1 1 1 1 0 2 1 1 0 1 1 0 0 1 1 1 0 0 1 0 2 1 1 1 1 0 1 1 2 0 2 0 1 2 1 1 1 2 0 1 1 1 1 0 0 1 ...
result:
ok 200001 tokens
Test #19:
score: 5
Accepted
time: 685ms
memory: 39256kb
input:
200000 200000 152950 172286 81402 40455 102388 180772 41862 9052 16836 122285 124563 50714 138444 40...
output:
9337 19966 21990 17842 12903 9087 6901 5208 4210 3564 2960 2444 2066 1839 1507 1355 1146 1117 898 86...
result:
ok 200001 tokens
Test #20:
score: 5
Accepted
time: 679ms
memory: 46700kb
input:
200000 200000 91696 108358 15991 131997 141623 126634 82046 133943 90417 65186 157116 190193 38945 2...
output:
0 0 0 1 1 1 0 0 1 1 1 1 0 2 0 0 2 1 1 0 0 2 1 2 1 0 1 0 1 2 0 0 4 1 0 2 0 0 1 0 1 2 0 2 1 0 2 1 1 1 ...
result:
ok 200001 tokens