Submission #863879

# Submission time Handle Problem Language Result Execution time Memory
863879 2023-10-21T10:07:09 Z lolismek Mergers (JOI19_mergers) C++14
100 / 100
1063 ms 141136 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>

using namespace std;

const int NMAX = 5e5;
const int LGMAX = 19;

vector <int> adj[NMAX + 1];
int s[NMAX + 1];

int lvl[NMAX + 1];
int dp[NMAX + 1][LGMAX + 1];

void dfs1(int node, int parent){
    lvl[node] = lvl[parent] + 1;
    dp[node][0] = parent;

    for(int child : adj[node]){
        if(child != parent){
            dfs1(child, node);
        }
    }
}

int LCA(int a, int b){
    if(lvl[a] < lvl[b]){
        swap(a, b);
    }

    int diff = lvl[a] - lvl[b];
    for(int i = LGMAX; i >= 0; i--){
        if(diff & (1 << i)){
            a = dp[a][i];
        }
    }

    if(a == b){
        return a;
    }

    for(int i = LGMAX; i >= 0; i--){
        if(dp[a][i] != dp[b][i]){
            a = dp[a][i];
            b = dp[b][i];
        }
    }

    return dp[a][0];
}

int glca[NMAX + 1];
int bad[NMAX + 1];

int dfs2(int node, int parent){
    int mini = lvl[glca[s[node]]];

    for(int child : adj[node]){
        if(child != parent){
            mini = min(mini, dfs2(child, node));
        }
    }

    if(mini >= lvl[node]){
        bad[node] = parent;
    }

    return mini;
}

int Id = 0;
int id[NMAX + 1];

void dfs3(int node, int parent){
    if(bad[node] == parent){
        id[node] = ++Id;
    }else{
        id[node] = id[parent];
    }

    for(int child : adj[node]){
        if(child != parent){
            dfs3(child, node);
        }
    }
}

vector <int> T[NMAX + 1];

void dfs4(int node, int parent){
    for(int child : adj[node]){
        if(child != parent){
            if(id[child] != id[node]){
                T[id[node]].push_back(id[child]);
                T[id[child]].push_back(id[node]);
            }
            dfs4(child, node);
        }
    }
}

int main(){

    int n, k;
    cin >> n >> k;

    for(int i = 1; i <= n - 1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }

    dfs1(1, 0);

    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i <= n; i++){
            dp[i][j] = dp[dp[i][j - 1]][j - 1];
        }
    }

    for(int i = 1; i <= n; i++){
        if(glca[s[i]] == 0){
            glca[s[i]] = i;
        }else{
            glca[s[i]] = LCA(glca[s[i]], i);
        }
    }

    dfs2(1, 0);
    dfs3(1, 0);
    dfs4(1, 0);

    int leafs = 0;
    for(int i = 1; i <= Id; i++){
        sort(T[i].begin(), T[i].end());
        T[i].erase(unique(T[i].begin(), T[i].end()), T[i].end());

        if((int)T[i].size() == 1){
            leafs++;
        }
    }

    cout << (leafs / 2) + (leafs % 2) << '\n';

    return 0;
}

/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31580 KB Output is correct
2 Correct 6 ms 31580 KB Output is correct
3 Correct 6 ms 31836 KB Output is correct
4 Correct 6 ms 31580 KB Output is correct
5 Correct 6 ms 31580 KB Output is correct
6 Correct 6 ms 31836 KB Output is correct
7 Correct 7 ms 31920 KB Output is correct
8 Correct 6 ms 31580 KB Output is correct
9 Correct 6 ms 31580 KB Output is correct
10 Correct 6 ms 31580 KB Output is correct
11 Correct 6 ms 31580 KB Output is correct
12 Correct 6 ms 31580 KB Output is correct
13 Correct 6 ms 31580 KB Output is correct
14 Correct 6 ms 31816 KB Output is correct
15 Correct 6 ms 31836 KB Output is correct
16 Correct 6 ms 31580 KB Output is correct
17 Correct 6 ms 31796 KB Output is correct
18 Correct 6 ms 31580 KB Output is correct
19 Correct 6 ms 31800 KB Output is correct
20 Correct 6 ms 31580 KB Output is correct
21 Correct 6 ms 31580 KB Output is correct
22 Correct 6 ms 31580 KB Output is correct
23 Correct 6 ms 31780 KB Output is correct
24 Correct 6 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31580 KB Output is correct
2 Correct 6 ms 31580 KB Output is correct
3 Correct 6 ms 31836 KB Output is correct
4 Correct 6 ms 31580 KB Output is correct
5 Correct 6 ms 31580 KB Output is correct
6 Correct 6 ms 31836 KB Output is correct
7 Correct 7 ms 31920 KB Output is correct
8 Correct 6 ms 31580 KB Output is correct
9 Correct 6 ms 31580 KB Output is correct
10 Correct 6 ms 31580 KB Output is correct
11 Correct 6 ms 31580 KB Output is correct
12 Correct 6 ms 31580 KB Output is correct
13 Correct 6 ms 31580 KB Output is correct
14 Correct 6 ms 31816 KB Output is correct
15 Correct 6 ms 31836 KB Output is correct
16 Correct 6 ms 31580 KB Output is correct
17 Correct 6 ms 31796 KB Output is correct
18 Correct 6 ms 31580 KB Output is correct
19 Correct 6 ms 31800 KB Output is correct
20 Correct 6 ms 31580 KB Output is correct
21 Correct 6 ms 31580 KB Output is correct
22 Correct 6 ms 31580 KB Output is correct
23 Correct 6 ms 31780 KB Output is correct
24 Correct 6 ms 31580 KB Output is correct
25 Correct 6 ms 31580 KB Output is correct
26 Correct 9 ms 31940 KB Output is correct
27 Correct 8 ms 31836 KB Output is correct
28 Correct 9 ms 32092 KB Output is correct
29 Correct 8 ms 31836 KB Output is correct
30 Correct 8 ms 31832 KB Output is correct
31 Correct 6 ms 31576 KB Output is correct
32 Correct 9 ms 32092 KB Output is correct
33 Correct 6 ms 31580 KB Output is correct
34 Correct 8 ms 31836 KB Output is correct
35 Correct 9 ms 31836 KB Output is correct
36 Correct 9 ms 31836 KB Output is correct
37 Correct 8 ms 31808 KB Output is correct
38 Correct 6 ms 31572 KB Output is correct
39 Correct 8 ms 31836 KB Output is correct
40 Correct 8 ms 31836 KB Output is correct
41 Correct 8 ms 31836 KB Output is correct
42 Correct 8 ms 31856 KB Output is correct
43 Correct 8 ms 32088 KB Output is correct
44 Correct 6 ms 31576 KB Output is correct
45 Correct 8 ms 31808 KB Output is correct
46 Correct 8 ms 31832 KB Output is correct
47 Correct 6 ms 31576 KB Output is correct
48 Correct 8 ms 31836 KB Output is correct
49 Correct 8 ms 32072 KB Output is correct
50 Correct 8 ms 32096 KB Output is correct
51 Correct 10 ms 31832 KB Output is correct
52 Correct 8 ms 31840 KB Output is correct
53 Correct 8 ms 31840 KB Output is correct
54 Correct 9 ms 31840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31580 KB Output is correct
2 Correct 6 ms 31580 KB Output is correct
3 Correct 6 ms 31836 KB Output is correct
4 Correct 6 ms 31580 KB Output is correct
5 Correct 6 ms 31580 KB Output is correct
6 Correct 6 ms 31836 KB Output is correct
7 Correct 7 ms 31920 KB Output is correct
8 Correct 6 ms 31580 KB Output is correct
9 Correct 6 ms 31580 KB Output is correct
10 Correct 6 ms 31580 KB Output is correct
11 Correct 6 ms 31580 KB Output is correct
12 Correct 6 ms 31580 KB Output is correct
13 Correct 6 ms 31580 KB Output is correct
14 Correct 6 ms 31816 KB Output is correct
15 Correct 6 ms 31836 KB Output is correct
16 Correct 6 ms 31580 KB Output is correct
17 Correct 6 ms 31796 KB Output is correct
18 Correct 6 ms 31580 KB Output is correct
19 Correct 6 ms 31800 KB Output is correct
20 Correct 6 ms 31580 KB Output is correct
21 Correct 6 ms 31580 KB Output is correct
22 Correct 6 ms 31580 KB Output is correct
23 Correct 6 ms 31780 KB Output is correct
24 Correct 6 ms 31580 KB Output is correct
25 Correct 6 ms 31584 KB Output is correct
26 Correct 79 ms 45256 KB Output is correct
27 Correct 93 ms 44936 KB Output is correct
28 Correct 8 ms 31832 KB Output is correct
29 Correct 6 ms 31836 KB Output is correct
30 Correct 6 ms 31580 KB Output is correct
31 Correct 97 ms 45000 KB Output is correct
32 Correct 8 ms 31832 KB Output is correct
33 Correct 113 ms 53704 KB Output is correct
34 Correct 95 ms 45060 KB Output is correct
35 Correct 8 ms 31836 KB Output is correct
36 Correct 104 ms 45932 KB Output is correct
37 Correct 8 ms 31832 KB Output is correct
38 Correct 8 ms 31800 KB Output is correct
39 Correct 79 ms 45156 KB Output is correct
40 Correct 8 ms 32092 KB Output is correct
41 Correct 89 ms 44792 KB Output is correct
42 Correct 112 ms 47668 KB Output is correct
43 Correct 6 ms 31576 KB Output is correct
44 Correct 113 ms 53908 KB Output is correct
45 Correct 105 ms 49492 KB Output is correct
46 Correct 8 ms 31836 KB Output is correct
47 Correct 8 ms 31836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 45380 KB Output is correct
2 Correct 100 ms 49396 KB Output is correct
3 Correct 9 ms 31836 KB Output is correct
4 Correct 8 ms 31824 KB Output is correct
5 Correct 6 ms 31576 KB Output is correct
6 Correct 6 ms 31576 KB Output is correct
7 Correct 8 ms 31836 KB Output is correct
8 Correct 107 ms 45908 KB Output is correct
9 Correct 8 ms 31832 KB Output is correct
10 Correct 101 ms 45140 KB Output is correct
11 Correct 6 ms 31580 KB Output is correct
12 Correct 105 ms 45136 KB Output is correct
13 Correct 114 ms 46008 KB Output is correct
14 Correct 120 ms 48864 KB Output is correct
15 Correct 78 ms 45128 KB Output is correct
16 Correct 8 ms 31812 KB Output is correct
17 Correct 6 ms 31836 KB Output is correct
18 Correct 94 ms 48676 KB Output is correct
19 Correct 119 ms 53476 KB Output is correct
20 Correct 8 ms 31836 KB Output is correct
21 Correct 6 ms 31580 KB Output is correct
22 Correct 89 ms 46284 KB Output is correct
23 Correct 10 ms 31836 KB Output is correct
24 Correct 114 ms 45544 KB Output is correct
25 Correct 115 ms 51792 KB Output is correct
26 Correct 9 ms 32088 KB Output is correct
27 Correct 8 ms 32092 KB Output is correct
28 Correct 8 ms 31836 KB Output is correct
29 Correct 8 ms 31836 KB Output is correct
30 Correct 8 ms 31836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31580 KB Output is correct
2 Correct 6 ms 31580 KB Output is correct
3 Correct 6 ms 31836 KB Output is correct
4 Correct 6 ms 31580 KB Output is correct
5 Correct 6 ms 31580 KB Output is correct
6 Correct 6 ms 31836 KB Output is correct
7 Correct 7 ms 31920 KB Output is correct
8 Correct 6 ms 31580 KB Output is correct
9 Correct 6 ms 31580 KB Output is correct
10 Correct 6 ms 31580 KB Output is correct
11 Correct 6 ms 31580 KB Output is correct
12 Correct 6 ms 31580 KB Output is correct
13 Correct 6 ms 31580 KB Output is correct
14 Correct 6 ms 31816 KB Output is correct
15 Correct 6 ms 31836 KB Output is correct
16 Correct 6 ms 31580 KB Output is correct
17 Correct 6 ms 31796 KB Output is correct
18 Correct 6 ms 31580 KB Output is correct
19 Correct 6 ms 31800 KB Output is correct
20 Correct 6 ms 31580 KB Output is correct
21 Correct 6 ms 31580 KB Output is correct
22 Correct 6 ms 31580 KB Output is correct
23 Correct 6 ms 31780 KB Output is correct
24 Correct 6 ms 31580 KB Output is correct
25 Correct 6 ms 31580 KB Output is correct
26 Correct 9 ms 31940 KB Output is correct
27 Correct 8 ms 31836 KB Output is correct
28 Correct 9 ms 32092 KB Output is correct
29 Correct 8 ms 31836 KB Output is correct
30 Correct 8 ms 31832 KB Output is correct
31 Correct 6 ms 31576 KB Output is correct
32 Correct 9 ms 32092 KB Output is correct
33 Correct 6 ms 31580 KB Output is correct
34 Correct 8 ms 31836 KB Output is correct
35 Correct 9 ms 31836 KB Output is correct
36 Correct 9 ms 31836 KB Output is correct
37 Correct 8 ms 31808 KB Output is correct
38 Correct 6 ms 31572 KB Output is correct
39 Correct 8 ms 31836 KB Output is correct
40 Correct 8 ms 31836 KB Output is correct
41 Correct 8 ms 31836 KB Output is correct
42 Correct 8 ms 31856 KB Output is correct
43 Correct 8 ms 32088 KB Output is correct
44 Correct 6 ms 31576 KB Output is correct
45 Correct 8 ms 31808 KB Output is correct
46 Correct 8 ms 31832 KB Output is correct
47 Correct 6 ms 31576 KB Output is correct
48 Correct 8 ms 31836 KB Output is correct
49 Correct 8 ms 32072 KB Output is correct
50 Correct 8 ms 32096 KB Output is correct
51 Correct 10 ms 31832 KB Output is correct
52 Correct 8 ms 31840 KB Output is correct
53 Correct 8 ms 31840 KB Output is correct
54 Correct 9 ms 31840 KB Output is correct
55 Correct 6 ms 31584 KB Output is correct
56 Correct 79 ms 45256 KB Output is correct
57 Correct 93 ms 44936 KB Output is correct
58 Correct 8 ms 31832 KB Output is correct
59 Correct 6 ms 31836 KB Output is correct
60 Correct 6 ms 31580 KB Output is correct
61 Correct 97 ms 45000 KB Output is correct
62 Correct 8 ms 31832 KB Output is correct
63 Correct 113 ms 53704 KB Output is correct
64 Correct 95 ms 45060 KB Output is correct
65 Correct 8 ms 31836 KB Output is correct
66 Correct 104 ms 45932 KB Output is correct
67 Correct 8 ms 31832 KB Output is correct
68 Correct 8 ms 31800 KB Output is correct
69 Correct 79 ms 45156 KB Output is correct
70 Correct 8 ms 32092 KB Output is correct
71 Correct 89 ms 44792 KB Output is correct
72 Correct 112 ms 47668 KB Output is correct
73 Correct 6 ms 31576 KB Output is correct
74 Correct 113 ms 53908 KB Output is correct
75 Correct 105 ms 49492 KB Output is correct
76 Correct 8 ms 31836 KB Output is correct
77 Correct 8 ms 31836 KB Output is correct
78 Correct 80 ms 45380 KB Output is correct
79 Correct 100 ms 49396 KB Output is correct
80 Correct 9 ms 31836 KB Output is correct
81 Correct 8 ms 31824 KB Output is correct
82 Correct 6 ms 31576 KB Output is correct
83 Correct 6 ms 31576 KB Output is correct
84 Correct 8 ms 31836 KB Output is correct
85 Correct 107 ms 45908 KB Output is correct
86 Correct 8 ms 31832 KB Output is correct
87 Correct 101 ms 45140 KB Output is correct
88 Correct 6 ms 31580 KB Output is correct
89 Correct 105 ms 45136 KB Output is correct
90 Correct 114 ms 46008 KB Output is correct
91 Correct 120 ms 48864 KB Output is correct
92 Correct 78 ms 45128 KB Output is correct
93 Correct 8 ms 31812 KB Output is correct
94 Correct 6 ms 31836 KB Output is correct
95 Correct 94 ms 48676 KB Output is correct
96 Correct 119 ms 53476 KB Output is correct
97 Correct 8 ms 31836 KB Output is correct
98 Correct 6 ms 31580 KB Output is correct
99 Correct 89 ms 46284 KB Output is correct
100 Correct 10 ms 31836 KB Output is correct
101 Correct 114 ms 45544 KB Output is correct
102 Correct 115 ms 51792 KB Output is correct
103 Correct 9 ms 32088 KB Output is correct
104 Correct 8 ms 32092 KB Output is correct
105 Correct 8 ms 31836 KB Output is correct
106 Correct 8 ms 31836 KB Output is correct
107 Correct 8 ms 31836 KB Output is correct
108 Correct 567 ms 101572 KB Output is correct
109 Correct 1002 ms 116404 KB Output is correct
110 Correct 1020 ms 129620 KB Output is correct
111 Correct 1063 ms 141136 KB Output is correct
112 Correct 898 ms 124320 KB Output is correct
113 Correct 594 ms 114740 KB Output is correct
114 Correct 681 ms 94588 KB Output is correct
115 Correct 680 ms 94800 KB Output is correct
116 Correct 787 ms 97872 KB Output is correct
117 Correct 812 ms 115080 KB Output is correct
118 Correct 681 ms 95104 KB Output is correct
119 Correct 839 ms 115356 KB Output is correct
120 Correct 966 ms 131448 KB Output is correct
121 Correct 828 ms 115136 KB Output is correct
122 Correct 810 ms 101824 KB Output is correct
123 Correct 600 ms 118172 KB Output is correct
124 Correct 1027 ms 107420 KB Output is correct