Submission #1083102

# Submission time Handle Problem Language Result Execution time Memory
1083102 2024-09-02T14:12:42 Z phong Mergers (JOI19_mergers) C++17
100 / 100
984 ms 216056 KB
#include<bits/stdc++.h>

#define ll long long
#define int long long
const int nmax = 5e5 + 5, N = 1e5;
const ll oo = 1e18 + 1, base = 311;
const int lg = 19, M = 10;
const ll mod = 998244353, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;

int n, k, a[nmax];
vector<int> adj[nmax], one[nmax];
int st[nmax], en[nmax], tour[nmax], sc = 0;
int h[nmax], up[nmax][lg + 1], par[nmax];

void dfs_1(int u, int p){
    tour[++sc] = u;
    st[u] = sc;
    for(auto v : adj[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        up[v][0] = u;
        for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
        dfs_1(v, u);
    }
    en[u] = sc;
}

int lca(int u, int v){
    if(h[u] != h[v]){
        if(h[u] < h[v]) swap(u, v);
        int k = h[u] - h[v];
        for(int j = 0; j <= lg; ++j){
            if(k >> j & 1){
                u = up[u][j];
            }
        }
    }
    if(u == v) return u;
    for(int j = __lg(h[u]); j >= 0; --j){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
    return up[u][0];
}
int c[nmax];
void dfs_2(int u, int p){
    for(auto v : adj[u]){
        if(v == p) continue;
        dfs_2(v, u);
        c[u] += c[v];
    }
}
int r[nmax];

int get(int u){
    return r[u] ? r[u] = get(r[u]) : u;
}
void Union(int u, int v){
    u = get(u);
    v = get(v);
    if(u != v){
        r[u] = v;
    }
}
vector<int> gg[nmax], leaf;
int cnt = 0, deg = 0;
void dfs_3(int u, int p){
    st[u] = ++sc;
    deg++;
    bool ok = 1;
    for(auto v : gg[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        up[v][0] = u;
        for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
        dfs_3(v, u);
        ok = 0;
    }
    if(ok) ++cnt, leaf.push_back(u);
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> k;
    for(int i = 1, u, v; i < n;++i){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i =1; i <= n; ++i){
        cin >> a[i];
        one[a[i]].push_back(i);
    }
    dfs_1(1, -1);
    vector<int> tmp, two;
    for(int i = 1; i <= k; ++i){
        sort(one[i].begin(), one[i].end(), [](int a, int b){
             return st[a] < st[b];
             });
        tmp = one[i];
        for(int j = 1; j < one[i].size(); ++j){
            tmp.push_back(lca(one[i][j - 1], one[i][j]));
        }
        sort(tmp.begin(), tmp.end());
        tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
        sort(tmp.begin(), tmp.end(), [](int a, int b){
             return st[a] < st[b];
            });

        for(int j = 0; j < tmp.size(); ++j){
            int u = tmp[j];
            if(j == 0) two.push_back(u);
            else{
                while(1){
                    int x = two.back();
                    if(st[x] <= st[u] && en[u] <= en[x])break;
                    two.pop_back();
                }
                int x = two.back();
                two.push_back(u);
                c[x]--;
                c[u]++;
            }
        }
        two.clear();
    }
    dfs_2(1, -1);
    for(int i = 2; i <= n; ++i){
        int x = up[i][0];
        if(c[i]){
            Union(i, x);
        }
    }
    for(int i = 1; i <= n; ++i){
        int u = get(i);
        for(auto v : adj[i]){
            int x = get(i), y = get(v);
            if(x != y){
                gg[x].push_back(y);
            }
        }
    }
    dfs_3(get(1), -1);
//    cout << deg<< ' ';
    if(deg == 1) cout << 0, exit(0);
    if(cnt & 1) cout << (cnt + 1) / 2;
    else{
        sort(leaf.begin(), leaf.end(),[](int a, int b){
             return st[a] < st[b];
             });
        int lc = lca(leaf[0], leaf.back());
        cnt /= 2;
        if(lc != get(1)) ++cnt;
        cout << cnt;
    }

}
/*
5 4
1 2
2 3
3 4
3 5
1 2 1 3 4


*/

Compilation message

mergers.cpp:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   89 | main(){
      | ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:111:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         for(int j = 1; j < one[i].size(); ++j){
      |                        ~~^~~~~~~~~~~~~~~
mergers.cpp:120:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for(int j = 0; j < tmp.size(); ++j){
      |                        ~~^~~~~~~~~~~~
mergers.cpp:145:13: warning: unused variable 'u' [-Wunused-variable]
  145 |         int u = get(i);
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 35672 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 14 ms 35512 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 13 ms 35676 KB Output is correct
7 Correct 13 ms 35676 KB Output is correct
8 Correct 15 ms 35636 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35648 KB Output is correct
12 Correct 13 ms 35676 KB Output is correct
13 Correct 14 ms 35676 KB Output is correct
14 Correct 12 ms 35676 KB Output is correct
15 Correct 13 ms 35676 KB Output is correct
16 Correct 14 ms 35744 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 13 ms 35768 KB Output is correct
19 Correct 15 ms 35676 KB Output is correct
20 Correct 14 ms 35672 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35712 KB Output is correct
23 Correct 12 ms 35684 KB Output is correct
24 Correct 15 ms 35672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 35672 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 14 ms 35512 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 13 ms 35676 KB Output is correct
7 Correct 13 ms 35676 KB Output is correct
8 Correct 15 ms 35636 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35648 KB Output is correct
12 Correct 13 ms 35676 KB Output is correct
13 Correct 14 ms 35676 KB Output is correct
14 Correct 12 ms 35676 KB Output is correct
15 Correct 13 ms 35676 KB Output is correct
16 Correct 14 ms 35744 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 13 ms 35768 KB Output is correct
19 Correct 15 ms 35676 KB Output is correct
20 Correct 14 ms 35672 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35712 KB Output is correct
23 Correct 12 ms 35684 KB Output is correct
24 Correct 15 ms 35672 KB Output is correct
25 Correct 18 ms 35676 KB Output is correct
26 Correct 15 ms 36440 KB Output is correct
27 Correct 16 ms 36440 KB Output is correct
28 Correct 15 ms 36676 KB Output is correct
29 Correct 16 ms 36748 KB Output is correct
30 Correct 16 ms 36440 KB Output is correct
31 Correct 14 ms 35676 KB Output is correct
32 Correct 17 ms 36740 KB Output is correct
33 Correct 14 ms 35676 KB Output is correct
34 Correct 16 ms 36480 KB Output is correct
35 Correct 23 ms 36728 KB Output is correct
36 Correct 16 ms 36444 KB Output is correct
37 Correct 17 ms 36440 KB Output is correct
38 Correct 14 ms 35676 KB Output is correct
39 Correct 17 ms 36444 KB Output is correct
40 Correct 16 ms 36636 KB Output is correct
41 Correct 19 ms 36956 KB Output is correct
42 Correct 15 ms 36444 KB Output is correct
43 Correct 17 ms 36696 KB Output is correct
44 Correct 15 ms 35708 KB Output is correct
45 Correct 16 ms 36444 KB Output is correct
46 Correct 18 ms 36440 KB Output is correct
47 Correct 15 ms 35672 KB Output is correct
48 Correct 17 ms 36444 KB Output is correct
49 Correct 16 ms 36740 KB Output is correct
50 Correct 16 ms 36696 KB Output is correct
51 Correct 17 ms 36444 KB Output is correct
52 Correct 16 ms 36596 KB Output is correct
53 Correct 16 ms 36440 KB Output is correct
54 Correct 20 ms 36548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 35672 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 14 ms 35512 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 13 ms 35676 KB Output is correct
7 Correct 13 ms 35676 KB Output is correct
8 Correct 15 ms 35636 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35648 KB Output is correct
12 Correct 13 ms 35676 KB Output is correct
13 Correct 14 ms 35676 KB Output is correct
14 Correct 12 ms 35676 KB Output is correct
15 Correct 13 ms 35676 KB Output is correct
16 Correct 14 ms 35744 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 13 ms 35768 KB Output is correct
19 Correct 15 ms 35676 KB Output is correct
20 Correct 14 ms 35672 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35712 KB Output is correct
23 Correct 12 ms 35684 KB Output is correct
24 Correct 15 ms 35672 KB Output is correct
25 Correct 14 ms 35676 KB Output is correct
26 Correct 72 ms 63348 KB Output is correct
27 Correct 104 ms 63072 KB Output is correct
28 Correct 16 ms 36444 KB Output is correct
29 Correct 15 ms 35696 KB Output is correct
30 Correct 14 ms 35676 KB Output is correct
31 Correct 107 ms 62800 KB Output is correct
32 Correct 16 ms 36444 KB Output is correct
33 Correct 120 ms 68180 KB Output is correct
34 Correct 108 ms 63056 KB Output is correct
35 Correct 17 ms 36440 KB Output is correct
36 Correct 99 ms 63064 KB Output is correct
37 Correct 16 ms 36444 KB Output is correct
38 Correct 15 ms 36444 KB Output is correct
39 Correct 85 ms 63300 KB Output is correct
40 Correct 15 ms 36700 KB Output is correct
41 Correct 115 ms 64432 KB Output is correct
42 Correct 109 ms 64336 KB Output is correct
43 Correct 14 ms 35676 KB Output is correct
44 Correct 115 ms 71304 KB Output is correct
45 Correct 141 ms 66640 KB Output is correct
46 Correct 16 ms 36444 KB Output is correct
47 Correct 16 ms 36440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 61892 KB Output is correct
2 Correct 83 ms 67724 KB Output is correct
3 Correct 17 ms 36440 KB Output is correct
4 Correct 16 ms 36444 KB Output is correct
5 Correct 15 ms 35676 KB Output is correct
6 Correct 16 ms 35676 KB Output is correct
7 Correct 14 ms 36444 KB Output is correct
8 Correct 97 ms 64644 KB Output is correct
9 Correct 16 ms 36440 KB Output is correct
10 Correct 123 ms 63056 KB Output is correct
11 Correct 14 ms 35700 KB Output is correct
12 Correct 86 ms 62548 KB Output is correct
13 Correct 99 ms 64848 KB Output is correct
14 Correct 109 ms 69072 KB Output is correct
15 Correct 74 ms 63296 KB Output is correct
16 Correct 16 ms 36440 KB Output is correct
17 Correct 15 ms 35772 KB Output is correct
18 Correct 107 ms 70040 KB Output is correct
19 Correct 116 ms 70224 KB Output is correct
20 Correct 15 ms 36440 KB Output is correct
21 Correct 14 ms 35772 KB Output is correct
22 Correct 76 ms 65308 KB Output is correct
23 Correct 16 ms 36464 KB Output is correct
24 Correct 105 ms 63608 KB Output is correct
25 Correct 130 ms 71376 KB Output is correct
26 Correct 17 ms 36696 KB Output is correct
27 Correct 17 ms 36700 KB Output is correct
28 Correct 17 ms 36444 KB Output is correct
29 Correct 16 ms 36444 KB Output is correct
30 Correct 15 ms 36444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 35672 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 14 ms 35512 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 13 ms 35676 KB Output is correct
7 Correct 13 ms 35676 KB Output is correct
8 Correct 15 ms 35636 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35648 KB Output is correct
12 Correct 13 ms 35676 KB Output is correct
13 Correct 14 ms 35676 KB Output is correct
14 Correct 12 ms 35676 KB Output is correct
15 Correct 13 ms 35676 KB Output is correct
16 Correct 14 ms 35744 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 13 ms 35768 KB Output is correct
19 Correct 15 ms 35676 KB Output is correct
20 Correct 14 ms 35672 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35712 KB Output is correct
23 Correct 12 ms 35684 KB Output is correct
24 Correct 15 ms 35672 KB Output is correct
25 Correct 18 ms 35676 KB Output is correct
26 Correct 15 ms 36440 KB Output is correct
27 Correct 16 ms 36440 KB Output is correct
28 Correct 15 ms 36676 KB Output is correct
29 Correct 16 ms 36748 KB Output is correct
30 Correct 16 ms 36440 KB Output is correct
31 Correct 14 ms 35676 KB Output is correct
32 Correct 17 ms 36740 KB Output is correct
33 Correct 14 ms 35676 KB Output is correct
34 Correct 16 ms 36480 KB Output is correct
35 Correct 23 ms 36728 KB Output is correct
36 Correct 16 ms 36444 KB Output is correct
37 Correct 17 ms 36440 KB Output is correct
38 Correct 14 ms 35676 KB Output is correct
39 Correct 17 ms 36444 KB Output is correct
40 Correct 16 ms 36636 KB Output is correct
41 Correct 19 ms 36956 KB Output is correct
42 Correct 15 ms 36444 KB Output is correct
43 Correct 17 ms 36696 KB Output is correct
44 Correct 15 ms 35708 KB Output is correct
45 Correct 16 ms 36444 KB Output is correct
46 Correct 18 ms 36440 KB Output is correct
47 Correct 15 ms 35672 KB Output is correct
48 Correct 17 ms 36444 KB Output is correct
49 Correct 16 ms 36740 KB Output is correct
50 Correct 16 ms 36696 KB Output is correct
51 Correct 17 ms 36444 KB Output is correct
52 Correct 16 ms 36596 KB Output is correct
53 Correct 16 ms 36440 KB Output is correct
54 Correct 20 ms 36548 KB Output is correct
55 Correct 14 ms 35676 KB Output is correct
56 Correct 72 ms 63348 KB Output is correct
57 Correct 104 ms 63072 KB Output is correct
58 Correct 16 ms 36444 KB Output is correct
59 Correct 15 ms 35696 KB Output is correct
60 Correct 14 ms 35676 KB Output is correct
61 Correct 107 ms 62800 KB Output is correct
62 Correct 16 ms 36444 KB Output is correct
63 Correct 120 ms 68180 KB Output is correct
64 Correct 108 ms 63056 KB Output is correct
65 Correct 17 ms 36440 KB Output is correct
66 Correct 99 ms 63064 KB Output is correct
67 Correct 16 ms 36444 KB Output is correct
68 Correct 15 ms 36444 KB Output is correct
69 Correct 85 ms 63300 KB Output is correct
70 Correct 15 ms 36700 KB Output is correct
71 Correct 115 ms 64432 KB Output is correct
72 Correct 109 ms 64336 KB Output is correct
73 Correct 14 ms 35676 KB Output is correct
74 Correct 115 ms 71304 KB Output is correct
75 Correct 141 ms 66640 KB Output is correct
76 Correct 16 ms 36444 KB Output is correct
77 Correct 16 ms 36440 KB Output is correct
78 Correct 73 ms 61892 KB Output is correct
79 Correct 83 ms 67724 KB Output is correct
80 Correct 17 ms 36440 KB Output is correct
81 Correct 16 ms 36444 KB Output is correct
82 Correct 15 ms 35676 KB Output is correct
83 Correct 16 ms 35676 KB Output is correct
84 Correct 14 ms 36444 KB Output is correct
85 Correct 97 ms 64644 KB Output is correct
86 Correct 16 ms 36440 KB Output is correct
87 Correct 123 ms 63056 KB Output is correct
88 Correct 14 ms 35700 KB Output is correct
89 Correct 86 ms 62548 KB Output is correct
90 Correct 99 ms 64848 KB Output is correct
91 Correct 109 ms 69072 KB Output is correct
92 Correct 74 ms 63296 KB Output is correct
93 Correct 16 ms 36440 KB Output is correct
94 Correct 15 ms 35772 KB Output is correct
95 Correct 107 ms 70040 KB Output is correct
96 Correct 116 ms 70224 KB Output is correct
97 Correct 15 ms 36440 KB Output is correct
98 Correct 14 ms 35772 KB Output is correct
99 Correct 76 ms 65308 KB Output is correct
100 Correct 16 ms 36464 KB Output is correct
101 Correct 105 ms 63608 KB Output is correct
102 Correct 130 ms 71376 KB Output is correct
103 Correct 17 ms 36696 KB Output is correct
104 Correct 17 ms 36700 KB Output is correct
105 Correct 17 ms 36444 KB Output is correct
106 Correct 16 ms 36444 KB Output is correct
107 Correct 15 ms 36444 KB Output is correct
108 Correct 532 ms 181348 KB Output is correct
109 Correct 814 ms 191148 KB Output is correct
110 Correct 850 ms 207032 KB Output is correct
111 Correct 954 ms 215904 KB Output is correct
112 Correct 813 ms 211132 KB Output is correct
113 Correct 568 ms 206860 KB Output is correct
114 Correct 689 ms 175540 KB Output is correct
115 Correct 705 ms 179360 KB Output is correct
116 Correct 694 ms 176968 KB Output is correct
117 Correct 717 ms 202444 KB Output is correct
118 Correct 620 ms 171996 KB Output is correct
119 Correct 708 ms 202684 KB Output is correct
120 Correct 984 ms 216056 KB Output is correct
121 Correct 720 ms 205768 KB Output is correct
122 Correct 685 ms 184868 KB Output is correct
123 Correct 525 ms 204340 KB Output is correct
124 Correct 771 ms 189908 KB Output is correct