UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213583#2769. 覆盖181126062316026ms6036kbC++112.2kb2024-11-12 21:18:022024-11-12 23:47:04

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
int n, m, u, v, pos, head[200001];
bool vis[100001];
struct edge
{
    int u, v, next;
} e[200001];
void add(int u, int v)
{
    e[++pos] = {u, v, head[u]};
    head[u] = pos;
}
vector<int> ans;
set<int> s[100001];
void dfs(int u, int fa = -1)
{
    for (int i = head[u]; i; i = e[i].next)
    {
        int v = e[i].v;
        if (v == fa)
            continue;
        dfs(v, u);
        if (s[u].size() > s[v].size())
        {
            for (auto x : s[v])
            {
                auto pos = s[u].find(x);
                if (pos != s[u].end())
                {
                    vis[u] = true;
                }
                else
                {
                    s[u].insert(x);
                }
            }
        }
        else
        {
            for (auto x : s[u])
            {
                auto pos = s[v].find(x);
                if (pos != s[v].end())
                {
                    vis[u] = 1;
                }
                else
                {
                    s[v].insert(x);
                }
            }
            swap(s[u], s[v]);
        }
    }
    if (vis[u])
    {
        ans.push_back(u);
        s[u].clear();
    }
}
signed main()
{
    n = read();
    for (int i = 1; i < n; i++)
    {
        u = read();
        v = read();
        add(u, v);
        add(v, u);
    }
    m = read();
    for (int i = 1; i <= m; i++)
    {
        u = read();
        v = read();
        if (u > v)
            swap(u, v);
        if (u == v)
        {
            vis[u] = true;
        }
        s[u].insert(i);
        s[v].insert(i);
    }
    dfs(1);
    printf("%lld\n", ans.size());
    sort(ans.begin(), ans.end());
    for (auto x : ans)
    {
        printf("%lld ", x);
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 30
Accepted

Test #1:

score: 30
Accepted
time: 4ms
memory: 5904kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 13
17 15
18 16
19 17
20 18
...

output:

2
3 10 

result:

ok ok

Test #2:

score: 0
Accepted
time: 0ms
memory: 5904kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 19
...

output:

2
6 13 

result:

ok ok

Test #3:

score: 0
Accepted
time: 0ms
memory: 5908kb

input:

20
2 1
3 1
4 3
5 1
6 5
7 4
8 7
9 8
10 9
11 10
12 6
13 2
14 7
15 14
16 11
17 12
18 17
19 7
20 15
5
4 ...

output:

3
1 4 11 

result:

ok ok

Test #4:

score: 0
Accepted
time: 0ms
memory: 5908kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 19
...

output:

2
5 11 

result:

ok ok

Test #5:

score: 0
Accepted
time: 2ms
memory: 5904kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 6
10 8
11 10
12 11
13 11
14 12
15 13
16 9
17 15
18 16
19 16
20 17
1...

output:

6
1 3 7 10 17 19 

result:

ok ok

Test #6:

score: 0
Accepted
time: 0ms
memory: 5908kb

input:

20
2 1
3 2
4 3
5 3
6 5
7 4
8 6
9 8
10 7
11 7
12 10
13 12
14 13
15 9
16 14
17 16
18 16
19 17
20 15
12...

output:

3
4 9 13 

result:

ok ok

Test #7:

score: 0
Accepted
time: 3ms
memory: 5908kb

input:

20
2 1
3 1
4 2
5 3
6 4
7 5
8 6
9 7
10 8
11 9
12 10
13 11
14 10
15 14
16 10
17 13
18 4
19 18
20 4
2
1...

output:

2
5 8 

result:

ok ok

Test #8:

score: 0
Accepted
time: 3ms
memory: 5904kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 4
8 7
9 6
10 9
11 8
12 11
13 10
14 10
15 12
16 14
17 13
18 17
19 14
20 18
4...

output:

2
4 10 

result:

ok ok

Test #9:

score: 0
Accepted
time: 0ms
memory: 5908kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 8
12 10
13 12
14 8
15 13
16 8
17 8
18 16
19 15
20 11
12
8...

output:

3
3 8 10 

result:

ok ok

Test #10:

score: 0
Accepted
time: 0ms
memory: 5908kb

input:

20
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 7
10 9
11 10
12 8
13 11
14 12
15 13
16 14
17 15
18 16
19 18
20 19
1...

output:

2
7 12 

result:

ok ok

Subtask #2:

score: 30
Accepted

Test #11:

score: 30
Accepted
time: 0ms
memory: 5996kb

input:

1000
2 1
3 2
4 3
5 4
6 3
7 6
8 7
9 8
10 9
11 10
12 5
13 11
14 13
15 14
16 15
17 12
18 16
19 18
20 19...

output:

11
53 131 186 235 258 280 289 317 397 471 585 

result:

ok ok

Test #12:

score: 0
Accepted
time: 0ms
memory: 6004kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 12
16 14
17 16
18 17
19 18
20 1...

output:

16
96 109 110 114 152 154 179 285 315 418 463 469 526 561 610 822 

result:

ok ok

Test #13:

score: 0
Accepted
time: 2ms
memory: 5972kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 1...

output:

2
40 305 

result:

ok ok

Test #14:

score: 0
Accepted
time: 0ms
memory: 6000kb

input:

1000
2 1
3 2
4 3
5 4
6 4
7 5
8 6
9 7
10 8
11 9
12 11
13 10
14 12
15 14
16 13
17 15
18 16
19 17
20 18...

output:

15
36 77 111 161 164 218 236 277 281 391 408 410 460 577 738 

result:

ok ok

Test #15:

score: 0
Accepted
time: 4ms
memory: 6036kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 1...

output:

17
46 104 126 230 293 313 324 439 460 462 469 504 513 526 546 646 694 

result:

ok ok

Test #16:

score: 0
Accepted
time: 4ms
memory: 5964kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 1...

output:

6
18 114 168 201 311 651 

result:

ok ok

Test #17:

score: 0
Accepted
time: 0ms
memory: 6012kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 13
16 14
17 16
18 17
19 15
20 1...

output:

16
11 42 68 113 171 249 311 341 361 373 423 450 497 567 586 649 

result:

ok ok

Test #18:

score: 0
Accepted
time: 0ms
memory: 5968kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 6
9 8
10 9
11 7
12 11
13 10
14 12
15 13
16 15
17 14
18 15
19 18
20 16...

output:

4
15 167 387 437 

result:

ok ok

Test #19:

score: 0
Accepted
time: 1ms
memory: 5984kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 1...

output:

10
33 158 210 236 363 364 367 372 873 978 

result:

ok ok

Test #20:

score: 0
Accepted
time: 3ms
memory: 5972kb

input:

1000
2 1
3 2
4 3
5 4
6 5
7 6
8 3
9 8
10 9
11 10
12 11
13 7
14 12
15 13
16 7
17 14
18 17
19 15
20 16
...

output:

5
7 85 118 241 307 

result:

ok ok

Subtask #3:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

2000000
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 11
15 14
16 13
17 15
18 16
19 17
2...

output:


result: