Submission #707316

# Submission time Handle Problem Language Result Execution time Memory
707316 2023-03-08T18:39:00 Z Danilo21 Mergers (JOI19_mergers) C++17
100 / 100
1320 ms 235040 KB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

class Tree{

private:
    int n, root;
    vector<int> depth, in, out;
    vector<vector<int> > g, par;
    int _edges_ = 0;
    bool _initialized_ = 0;

    bool Valid(int s) const { return s >= 0 && s < n; }

    int InitDfs(int s, int p = -1, int d = 0, int t = 0){
        par[s][0] = p;
        depth[s] = d;
        in[s] = t;
        for (int u : g[s])
            if (u^p)
                t = InitDfs(u, s, d+1, t+1);
        return out[s] = ++t;
    }

    void Dfs(int s, int p, void bf(int, int), void af(int, int)) const {
        bf(s, p);
        for (int u : g[s])
            if (u^p)
                Dfs(u, s, bf, af);
        af(s, p);
    }

public:
    Tree(int n = 0){ Assign(n); }

    void Assign(int n = 0){
        this->n = n;
        depth.assign(n, 0);
        in.assign(n, 0);
        out.assign(n, 0);
        g.assign(n, vector<int>());
        par.assign(n, vector<int>(lg(n)+1, -1));
    }

    void Edge(int u, int v){
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        g[u].pb(v);
        g[v].pb(u);
        _edges_++;
    }

    void Read(int d = 1){
        for (int i = 0; i < n-1; i++){
            ri(u); ri(v);
            u -= d; v -= d;
            Edge(u, v);
        }
    }

    void Initialize(int s = 0){
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (_edges_ < n-1){
            cerr << "Tree is not connected" << endl;
            exit(1);
        }
        if (_edges_ > n-1){
            cerr << "Tree has cycle(s)" << endl;
            exit(1);
        }
        root = s;
        InitDfs(s);
        for (int d = 1; d <= lg(n); d++)
            for (int i = 0; i < n; i++)
                if (depth[i] >= (1<<d))
                    par[i][d] = par[par[i][d-1]][d-1];
        _initialized_ = 1;
    }

    int Size() const { return n; }

    int Depth(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return depth[s];
    }

    int InTime(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return in[s];
    }

    int OutTime(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return out[s];
    }

    vector<int> GetAdjecent(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return g[s];
    }

    vector<int> GetChilds(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        vector<int> ch;
        for (int u : g[s])
            if (u^par[s][0])
                ch.pb(u);
        return ch;
    }

    int Par(int s, int d = 1) const {
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        if (d < 0 || d > depth[s]) return -1;
        if (!d) return s;
        return Par(par[s][lg(d)], d - highpow(d));
    }

    bool Ancestor(int s, int p) const {
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        return in[p] <= in[s] && out[p] >= out[s];
    }

    int Lca(int u, int v) const {
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        if (depth[u] > depth[v]) swap(u, v);
        if (Ancestor(v, u)) return u;
        v = Par(v, depth[v] - depth[u]);
        for (int d = lg(n); ~d; d--){
            if (par[u][d] != par[v][d]){
                u = par[u][d];
                v = par[v][d];
            }
        }
        return par[u][0];
    }

    int Dist(int u, int v) const {
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        int lca = Lca(u, v);
        return 2*depth[lca] - depth[u] - depth[v];
    }

    void Dfs(void bf(int, int), void af(int, int)) const { Dfs(root, -1, bf, af); }
};
#define Empty [](int, int){}

class dsu{
private:
    int n;
    vector<int> comp, sz;
    vector<vector<int> > g;
    void dfs(int s, int c){
        if (this->comp[s] == c) return;
        this->comp[s] = c;
        for (int u : this->g[s])
            dfs(u, c);
    }
public:
    void init(int n){
        this->n = n;
        this->sz = vector<int>(n, 1);
        this->comp = vector<int>(n);
        for (int i = 0; i < n; i++) this->comp[i] = i;
        this->g = vector<vector<int> >(n);
    }
    void merge(int s, int u){
        if (comp[s] == comp[u]) return;
        this->g[s].pb(u);
        this->g[u].pb(s);
        if (this->sz[this->comp[s]] < this->sz[this->comp[u]]) swap(s, u);
        this->sz[this->comp[s]] += this->sz[this->comp[u]];
        this->sz[this->comp[u]] = 0;
        dfs(u, this->comp[s]);
    }
    int find(int s) const { return this->comp[s]; }
    int clean(){
        int mp[this->n];
        memset(mp, -1, sizeof(mp));
        int m = 0;
        for (int i = 0; i < this->n; i++)
            if (this->sz[this->comp[i]])
                if (!~mp[this->comp[i]])
                    mp[this->comp[i]] = m++;
        for (int i = 0; i < n; i++)
            this->comp[i] = mp[this->comp[i]];
        return m;
    }
};

const ll LINF = 4e18;
const int mxN = 1e6+10, INF = 2e9;
ll n, m, cnt, a[mxN], lca[mxN], sum[mxN];
Tree tree;
set<int> g[mxN];
dsu ds;

void dfs(int s, int p){

    for (int u : tree.GetChilds(s)) sum[s] += sum[u];
    if (sum[s] && ~p) ds.merge(s, p);
}

void Solve(){

    cin >> n >> m;
    tree.Assign(n);
    tree.Read();
    tree.Initialize();
    memset(lca, -1, sizeof(lca));
    for (int i = 0; i < n; i++){
        cin >> a[i]; a[i]--;
        if (!~lca[a[i]]) lca[a[i]] = i;
        else lca[a[i]] = tree.Lca(lca[a[i]], i);
    }
    for (int i = 0; i < n; i++){
        sum[i]++; sum[lca[a[i]]]--;
    }
    ds.init(n);
    tree.Dfs(Empty, &dfs);
    ds.clean();
    for (int s = 1; s < n; s++){
        if (ds.find(s) != ds.find(tree.Par(s))){
            g[ds.find(s)].insert(ds.find(tree.Par(s)));
            g[ds.find(tree.Par(s))].insert(ds.find(s));
        }
    }
    int cnt = 0;
    for (int s = 0; s < n; s++)
        if (g[s].size() == 1) cnt++;
    cout << (cnt+1)/2 << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 55124 KB Output is correct
2 Correct 24 ms 55096 KB Output is correct
3 Correct 33 ms 55076 KB Output is correct
4 Correct 29 ms 55080 KB Output is correct
5 Correct 29 ms 55124 KB Output is correct
6 Correct 23 ms 55124 KB Output is correct
7 Correct 25 ms 55088 KB Output is correct
8 Correct 23 ms 55124 KB Output is correct
9 Correct 29 ms 55048 KB Output is correct
10 Correct 28 ms 55012 KB Output is correct
11 Correct 27 ms 55124 KB Output is correct
12 Correct 29 ms 55124 KB Output is correct
13 Correct 26 ms 55116 KB Output is correct
14 Correct 31 ms 55132 KB Output is correct
15 Correct 30 ms 55112 KB Output is correct
16 Correct 26 ms 54992 KB Output is correct
17 Correct 27 ms 55108 KB Output is correct
18 Correct 29 ms 55076 KB Output is correct
19 Correct 26 ms 55120 KB Output is correct
20 Correct 27 ms 55104 KB Output is correct
21 Correct 29 ms 55124 KB Output is correct
22 Correct 26 ms 55164 KB Output is correct
23 Correct 43 ms 55088 KB Output is correct
24 Correct 28 ms 55012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 55124 KB Output is correct
2 Correct 24 ms 55096 KB Output is correct
3 Correct 33 ms 55076 KB Output is correct
4 Correct 29 ms 55080 KB Output is correct
5 Correct 29 ms 55124 KB Output is correct
6 Correct 23 ms 55124 KB Output is correct
7 Correct 25 ms 55088 KB Output is correct
8 Correct 23 ms 55124 KB Output is correct
9 Correct 29 ms 55048 KB Output is correct
10 Correct 28 ms 55012 KB Output is correct
11 Correct 27 ms 55124 KB Output is correct
12 Correct 29 ms 55124 KB Output is correct
13 Correct 26 ms 55116 KB Output is correct
14 Correct 31 ms 55132 KB Output is correct
15 Correct 30 ms 55112 KB Output is correct
16 Correct 26 ms 54992 KB Output is correct
17 Correct 27 ms 55108 KB Output is correct
18 Correct 29 ms 55076 KB Output is correct
19 Correct 26 ms 55120 KB Output is correct
20 Correct 27 ms 55104 KB Output is correct
21 Correct 29 ms 55124 KB Output is correct
22 Correct 26 ms 55164 KB Output is correct
23 Correct 43 ms 55088 KB Output is correct
24 Correct 28 ms 55012 KB Output is correct
25 Correct 25 ms 55112 KB Output is correct
26 Correct 28 ms 56020 KB Output is correct
27 Correct 28 ms 55876 KB Output is correct
28 Correct 34 ms 56024 KB Output is correct
29 Correct 37 ms 55956 KB Output is correct
30 Correct 27 ms 55756 KB Output is correct
31 Correct 27 ms 55124 KB Output is correct
32 Correct 29 ms 55916 KB Output is correct
33 Correct 26 ms 55116 KB Output is correct
34 Correct 27 ms 55884 KB Output is correct
35 Correct 26 ms 55928 KB Output is correct
36 Correct 38 ms 55868 KB Output is correct
37 Correct 30 ms 55884 KB Output is correct
38 Correct 26 ms 55104 KB Output is correct
39 Correct 29 ms 56008 KB Output is correct
40 Correct 30 ms 55848 KB Output is correct
41 Correct 32 ms 55836 KB Output is correct
42 Correct 27 ms 55872 KB Output is correct
43 Correct 28 ms 55892 KB Output is correct
44 Correct 30 ms 55124 KB Output is correct
45 Correct 28 ms 55892 KB Output is correct
46 Correct 36 ms 55756 KB Output is correct
47 Correct 26 ms 55104 KB Output is correct
48 Correct 28 ms 55872 KB Output is correct
49 Correct 28 ms 56064 KB Output is correct
50 Correct 29 ms 55944 KB Output is correct
51 Correct 28 ms 55908 KB Output is correct
52 Correct 29 ms 55892 KB Output is correct
53 Correct 29 ms 55948 KB Output is correct
54 Correct 29 ms 55764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 55124 KB Output is correct
2 Correct 24 ms 55096 KB Output is correct
3 Correct 33 ms 55076 KB Output is correct
4 Correct 29 ms 55080 KB Output is correct
5 Correct 29 ms 55124 KB Output is correct
6 Correct 23 ms 55124 KB Output is correct
7 Correct 25 ms 55088 KB Output is correct
8 Correct 23 ms 55124 KB Output is correct
9 Correct 29 ms 55048 KB Output is correct
10 Correct 28 ms 55012 KB Output is correct
11 Correct 27 ms 55124 KB Output is correct
12 Correct 29 ms 55124 KB Output is correct
13 Correct 26 ms 55116 KB Output is correct
14 Correct 31 ms 55132 KB Output is correct
15 Correct 30 ms 55112 KB Output is correct
16 Correct 26 ms 54992 KB Output is correct
17 Correct 27 ms 55108 KB Output is correct
18 Correct 29 ms 55076 KB Output is correct
19 Correct 26 ms 55120 KB Output is correct
20 Correct 27 ms 55104 KB Output is correct
21 Correct 29 ms 55124 KB Output is correct
22 Correct 26 ms 55164 KB Output is correct
23 Correct 43 ms 55088 KB Output is correct
24 Correct 28 ms 55012 KB Output is correct
25 Correct 26 ms 55028 KB Output is correct
26 Correct 105 ms 82700 KB Output is correct
27 Correct 153 ms 81776 KB Output is correct
28 Correct 28 ms 55804 KB Output is correct
29 Correct 26 ms 55104 KB Output is correct
30 Correct 26 ms 55072 KB Output is correct
31 Correct 188 ms 81744 KB Output is correct
32 Correct 36 ms 55892 KB Output is correct
33 Correct 194 ms 84280 KB Output is correct
34 Correct 152 ms 81740 KB Output is correct
35 Correct 31 ms 55892 KB Output is correct
36 Correct 191 ms 81616 KB Output is correct
37 Correct 32 ms 55848 KB Output is correct
38 Correct 31 ms 55888 KB Output is correct
39 Correct 110 ms 82496 KB Output is correct
40 Correct 28 ms 55884 KB Output is correct
41 Correct 148 ms 81760 KB Output is correct
42 Correct 177 ms 82224 KB Output is correct
43 Correct 26 ms 55156 KB Output is correct
44 Correct 187 ms 84332 KB Output is correct
45 Correct 185 ms 82836 KB Output is correct
46 Correct 29 ms 55892 KB Output is correct
47 Correct 30 ms 55812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 81256 KB Output is correct
2 Correct 118 ms 86796 KB Output is correct
3 Correct 29 ms 55884 KB Output is correct
4 Correct 28 ms 55836 KB Output is correct
5 Correct 27 ms 55072 KB Output is correct
6 Correct 26 ms 55156 KB Output is correct
7 Correct 28 ms 55840 KB Output is correct
8 Correct 160 ms 82772 KB Output is correct
9 Correct 28 ms 55764 KB Output is correct
10 Correct 154 ms 81900 KB Output is correct
11 Correct 27 ms 55104 KB Output is correct
12 Correct 196 ms 81772 KB Output is correct
13 Correct 166 ms 83568 KB Output is correct
14 Correct 165 ms 88228 KB Output is correct
15 Correct 111 ms 82392 KB Output is correct
16 Correct 27 ms 55892 KB Output is correct
17 Correct 25 ms 55104 KB Output is correct
18 Correct 125 ms 87312 KB Output is correct
19 Correct 221 ms 89524 KB Output is correct
20 Correct 35 ms 55892 KB Output is correct
21 Correct 26 ms 55068 KB Output is correct
22 Correct 120 ms 83596 KB Output is correct
23 Correct 34 ms 55892 KB Output is correct
24 Correct 188 ms 82684 KB Output is correct
25 Correct 188 ms 88788 KB Output is correct
26 Correct 27 ms 56088 KB Output is correct
27 Correct 27 ms 56028 KB Output is correct
28 Correct 32 ms 55828 KB Output is correct
29 Correct 27 ms 56012 KB Output is correct
30 Correct 28 ms 56056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 55124 KB Output is correct
2 Correct 24 ms 55096 KB Output is correct
3 Correct 33 ms 55076 KB Output is correct
4 Correct 29 ms 55080 KB Output is correct
5 Correct 29 ms 55124 KB Output is correct
6 Correct 23 ms 55124 KB Output is correct
7 Correct 25 ms 55088 KB Output is correct
8 Correct 23 ms 55124 KB Output is correct
9 Correct 29 ms 55048 KB Output is correct
10 Correct 28 ms 55012 KB Output is correct
11 Correct 27 ms 55124 KB Output is correct
12 Correct 29 ms 55124 KB Output is correct
13 Correct 26 ms 55116 KB Output is correct
14 Correct 31 ms 55132 KB Output is correct
15 Correct 30 ms 55112 KB Output is correct
16 Correct 26 ms 54992 KB Output is correct
17 Correct 27 ms 55108 KB Output is correct
18 Correct 29 ms 55076 KB Output is correct
19 Correct 26 ms 55120 KB Output is correct
20 Correct 27 ms 55104 KB Output is correct
21 Correct 29 ms 55124 KB Output is correct
22 Correct 26 ms 55164 KB Output is correct
23 Correct 43 ms 55088 KB Output is correct
24 Correct 28 ms 55012 KB Output is correct
25 Correct 25 ms 55112 KB Output is correct
26 Correct 28 ms 56020 KB Output is correct
27 Correct 28 ms 55876 KB Output is correct
28 Correct 34 ms 56024 KB Output is correct
29 Correct 37 ms 55956 KB Output is correct
30 Correct 27 ms 55756 KB Output is correct
31 Correct 27 ms 55124 KB Output is correct
32 Correct 29 ms 55916 KB Output is correct
33 Correct 26 ms 55116 KB Output is correct
34 Correct 27 ms 55884 KB Output is correct
35 Correct 26 ms 55928 KB Output is correct
36 Correct 38 ms 55868 KB Output is correct
37 Correct 30 ms 55884 KB Output is correct
38 Correct 26 ms 55104 KB Output is correct
39 Correct 29 ms 56008 KB Output is correct
40 Correct 30 ms 55848 KB Output is correct
41 Correct 32 ms 55836 KB Output is correct
42 Correct 27 ms 55872 KB Output is correct
43 Correct 28 ms 55892 KB Output is correct
44 Correct 30 ms 55124 KB Output is correct
45 Correct 28 ms 55892 KB Output is correct
46 Correct 36 ms 55756 KB Output is correct
47 Correct 26 ms 55104 KB Output is correct
48 Correct 28 ms 55872 KB Output is correct
49 Correct 28 ms 56064 KB Output is correct
50 Correct 29 ms 55944 KB Output is correct
51 Correct 28 ms 55908 KB Output is correct
52 Correct 29 ms 55892 KB Output is correct
53 Correct 29 ms 55948 KB Output is correct
54 Correct 29 ms 55764 KB Output is correct
55 Correct 26 ms 55028 KB Output is correct
56 Correct 105 ms 82700 KB Output is correct
57 Correct 153 ms 81776 KB Output is correct
58 Correct 28 ms 55804 KB Output is correct
59 Correct 26 ms 55104 KB Output is correct
60 Correct 26 ms 55072 KB Output is correct
61 Correct 188 ms 81744 KB Output is correct
62 Correct 36 ms 55892 KB Output is correct
63 Correct 194 ms 84280 KB Output is correct
64 Correct 152 ms 81740 KB Output is correct
65 Correct 31 ms 55892 KB Output is correct
66 Correct 191 ms 81616 KB Output is correct
67 Correct 32 ms 55848 KB Output is correct
68 Correct 31 ms 55888 KB Output is correct
69 Correct 110 ms 82496 KB Output is correct
70 Correct 28 ms 55884 KB Output is correct
71 Correct 148 ms 81760 KB Output is correct
72 Correct 177 ms 82224 KB Output is correct
73 Correct 26 ms 55156 KB Output is correct
74 Correct 187 ms 84332 KB Output is correct
75 Correct 185 ms 82836 KB Output is correct
76 Correct 29 ms 55892 KB Output is correct
77 Correct 30 ms 55812 KB Output is correct
78 Correct 106 ms 81256 KB Output is correct
79 Correct 118 ms 86796 KB Output is correct
80 Correct 29 ms 55884 KB Output is correct
81 Correct 28 ms 55836 KB Output is correct
82 Correct 27 ms 55072 KB Output is correct
83 Correct 26 ms 55156 KB Output is correct
84 Correct 28 ms 55840 KB Output is correct
85 Correct 160 ms 82772 KB Output is correct
86 Correct 28 ms 55764 KB Output is correct
87 Correct 154 ms 81900 KB Output is correct
88 Correct 27 ms 55104 KB Output is correct
89 Correct 196 ms 81772 KB Output is correct
90 Correct 166 ms 83568 KB Output is correct
91 Correct 165 ms 88228 KB Output is correct
92 Correct 111 ms 82392 KB Output is correct
93 Correct 27 ms 55892 KB Output is correct
94 Correct 25 ms 55104 KB Output is correct
95 Correct 125 ms 87312 KB Output is correct
96 Correct 221 ms 89524 KB Output is correct
97 Correct 35 ms 55892 KB Output is correct
98 Correct 26 ms 55068 KB Output is correct
99 Correct 120 ms 83596 KB Output is correct
100 Correct 34 ms 55892 KB Output is correct
101 Correct 188 ms 82684 KB Output is correct
102 Correct 188 ms 88788 KB Output is correct
103 Correct 27 ms 56088 KB Output is correct
104 Correct 27 ms 56028 KB Output is correct
105 Correct 32 ms 55828 KB Output is correct
106 Correct 27 ms 56012 KB Output is correct
107 Correct 28 ms 56056 KB Output is correct
108 Correct 689 ms 204556 KB Output is correct
109 Correct 1169 ms 201640 KB Output is correct
110 Correct 1262 ms 206540 KB Output is correct
111 Correct 1320 ms 235040 KB Output is correct
112 Correct 1158 ms 218720 KB Output is correct
113 Correct 703 ms 225920 KB Output is correct
114 Correct 955 ms 196900 KB Output is correct
115 Correct 951 ms 196912 KB Output is correct
116 Correct 1060 ms 198824 KB Output is correct
117 Correct 1004 ms 229972 KB Output is correct
118 Correct 900 ms 195012 KB Output is correct
119 Correct 1004 ms 230044 KB Output is correct
120 Correct 1166 ms 233544 KB Output is correct
121 Correct 986 ms 229796 KB Output is correct
122 Correct 1097 ms 204336 KB Output is correct
123 Correct 638 ms 231408 KB Output is correct
124 Correct 1163 ms 215232 KB Output is correct