Submission #591043

# Submission time Handle Problem Language Result Execution time Memory
591043 2022-07-06T18:51:35 Z piOOE Unique Cities (JOI19_ho_t5) C++17
100 / 100
308 ms 30568 KB
//#define _GLIBCXX_DEBUG

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using normal_queue = priority_queue <T, vector<T>, greater<>>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define ld long double
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define eb emplace_back


template<typename T>
bool ckmn(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool ckmx(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

int bit(int x, int b) {
    return (x >> b) & 1;
}

int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }

const ll infL = 3e18;
const int infI = 1000000000 + 7;
const int infM = 2139062143;
const ll infML = 9187201950435737471LL;
const int N = 200001;
const ll mod = 998244353;
const ld eps = 1e-9;

vector<int> g[N];
bool insus[N], used[N];
int ans[N], cnt[N], c[N], val = 0, d[2], dist[2][N], n, m, pp[N], mxd[N], ww[N];
vector<int> sus;

int calc_farthest(int v) {
    vector<int> dst(n, infI);
    dst[v] = 0;
    queue<int> q;
    q.push(v);
    int last = -1;
    while (!q.empty()) {
        int u = q.front();
        last = u;
        q.pop();
        for (int to: g[u]) {
            if (dst[to] == infI) {
                q.push(to);
                dst[to] = dst[u] + 1;
            }
        }
    }
    d[0] = last;
    return last;
}

void bfs1() {
    fill(dist[0], dist[0] + n, infI);
    dist[0][d[0]] = 0;
    queue<int> q;
    q.push(d[0]);
    pp[d[0]] = -1;
    while (!q.empty()) {
        int v = q.front();
        d[1] = v;
        q.pop();
        for (int to: g[v]) {
            if (dist[0][to] == infI) {
                q.push(to);
                dist[0][to] = dist[0][v] + 1;
                pp[to] = v;
            }
        }
    }
}

void bfs2() {
    fill(dist[1], dist[1] + n, infI);
    dist[1][d[1]] = 0;
    queue<int> q;
    q.push(d[1]);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int to: g[v]) {
            if (dist[1][to] == infI) {
                q.push(to);
                dist[1][to] = dist[1][v] + 1;
            }
        }
    }
}

void calc_sus() {
    int v = d[1];
    while (v != -1) {
        sus.pb(v);
        insus[v] = true;
        v = pp[v];
    }
}

void calc_mxd(int v, int p) {
    for (int to: g[v]) {
        if (!insus[to] && to != p) {
            calc_mxd(to, v);
            ckmx(mxd[v], mxd[to] + 1);
        }
    }
}

vector<int> st;

void add(int x) {
    if (used[x]) return;
    used[x] = true;
    if (cnt[c[x]] == 0)
        ++val;
    ++cnt[c[x]];
    st.pb(x);
}

void del(int x) {
    if (!used[x]) return;
    used[x] = false;
    --cnt[c[x]];
    if (cnt[c[x]] == 0)
        --val;
    assert(!st.empty() && st.back() == x);
    st.pop_back();
}

int iteration = 0;

//don't forget to if this is in sus

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].pb(b), g[b].pb(a);
    }
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }
    calc_farthest(0);
    bfs1();
    bfs2();
//    cout << d[0] << " " << d[1] << endl;
    calc_sus();
    for (int x: sus) calc_mxd(x, -1);
    //sus = d[1], v1, v2, ..., vn, d[0]
    m = sz(sus);
    auto del_stack = [](int v, int len) {
        while (!st.empty()) {
            int u = st.back();
            if (dist[iteration][u] + len >= dist[iteration][v]) {
                del(u);
            } else {
                break;
            }
        }
    };

    function<void(int, int)> dfs = [&](int v, int p) {
        int mx = -1;
        for (int i = 0; i < sz(g[v]); ++i) {
            int to = g[v][i];
            if (insus[to] || to == p) continue;
            if (mx == -1 || mxd[mx] < mxd[to]) {
                mx = to;
            }
        }
        if (mx == -1) {
            ans[v] = val;
            return;
        }
        for (int to: g[v]) {
            if (!insus[to] && to != p && to != mx) {
                del_stack(v, mxd[to] + 1);
            }
        }
        add(v);
        dfs(mx, v);
        del_stack(v, mxd[mx] + 1);
        add(v);
        for (int to: g[v]) {
            if (!insus[to] && to != p && to != mx) {
                add(v);
                dfs(to, v);
            }
        }
        del(v);
//        del_stack(v, mxd[v]);
        ans[v] = val;
    };
    int last = m - 1;
    int rr = m;
    memset(ww, 127, sizeof(ww));
    for (int i = m - 1; i >= 0; --i) {
        int lenL = i;
        while (last - i > lenL) {
            ckmn(rr, ww[last]);
            if (rr > last) {
                add(sus[last]);
            }
            --last;
        }
        if (i <= m - 1 - m / 2) {
            dfs(sus[i], -1);
        }
        del_stack(sus[i], mxd[sus[i]]);
        if (i + mxd[sus[i]] >= last) ckmn(rr, i + 1);
        else ckmn(ww[i + mxd[sus[i]]], i + 1);
    }
    last = 0;
    while (!st.empty()) {
        del(st.back());
    }
    iteration = 1;
    fill(ww, ww + m, -infI);
    int LL = -1;
    for (int i = 0; i < m; ++i) {
        int lenR = m - i - 1;
        while (i - last > lenR) {
            ckmx(LL, ww[last]);
            if (last > LL) {
                add(sus[last]);
            }
            ++last;
        }
        if (i > m - 1 - m / 2) {
            dfs(sus[i], -1);
        }
        del_stack(sus[i], mxd[sus[i]]);
        if (i - mxd[sus[i]] <= last) ckmx(LL, i - 1);
        else ckmx(ww[i - mxd[sus[i]]], i - 1);
    }
    for (int i = 0; i < n; ++i) {
        cout << ans[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 4 ms 5944 KB Output is correct
3 Correct 4 ms 5844 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5948 KB Output is correct
6 Correct 4 ms 5948 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 6 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 4 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 6 ms 5972 KB Output is correct
15 Correct 5 ms 5940 KB Output is correct
16 Correct 4 ms 5972 KB Output is correct
17 Correct 5 ms 5948 KB Output is correct
18 Correct 5 ms 6004 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5972 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 5 ms 5972 KB Output is correct
24 Correct 4 ms 5952 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 5 ms 6004 KB Output is correct
27 Correct 5 ms 5944 KB Output is correct
28 Correct 5 ms 5992 KB Output is correct
29 Correct 5 ms 5972 KB Output is correct
30 Correct 4 ms 5972 KB Output is correct
31 Correct 4 ms 5972 KB Output is correct
32 Correct 4 ms 6072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 14172 KB Output is correct
2 Correct 118 ms 15096 KB Output is correct
3 Correct 24 ms 9052 KB Output is correct
4 Correct 246 ms 20520 KB Output is correct
5 Correct 256 ms 22232 KB Output is correct
6 Correct 262 ms 28460 KB Output is correct
7 Correct 251 ms 20760 KB Output is correct
8 Correct 234 ms 20760 KB Output is correct
9 Correct 241 ms 20668 KB Output is correct
10 Correct 231 ms 21652 KB Output is correct
11 Correct 159 ms 21936 KB Output is correct
12 Correct 260 ms 21700 KB Output is correct
13 Correct 236 ms 21944 KB Output is correct
14 Correct 230 ms 28096 KB Output is correct
15 Correct 98 ms 20844 KB Output is correct
16 Correct 224 ms 22120 KB Output is correct
17 Correct 272 ms 28560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 18700 KB Output is correct
2 Correct 260 ms 23996 KB Output is correct
3 Correct 25 ms 9604 KB Output is correct
4 Correct 251 ms 22260 KB Output is correct
5 Correct 262 ms 24756 KB Output is correct
6 Correct 308 ms 30340 KB Output is correct
7 Correct 250 ms 22312 KB Output is correct
8 Correct 261 ms 23100 KB Output is correct
9 Correct 233 ms 22732 KB Output is correct
10 Correct 240 ms 23588 KB Output is correct
11 Correct 197 ms 22784 KB Output is correct
12 Correct 254 ms 24596 KB Output is correct
13 Correct 235 ms 23676 KB Output is correct
14 Correct 258 ms 29548 KB Output is correct
15 Correct 110 ms 21804 KB Output is correct
16 Correct 224 ms 24308 KB Output is correct
17 Correct 240 ms 30568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 4 ms 5944 KB Output is correct
3 Correct 4 ms 5844 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 5948 KB Output is correct
6 Correct 4 ms 5948 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 6 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 4 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 6 ms 5972 KB Output is correct
15 Correct 5 ms 5940 KB Output is correct
16 Correct 4 ms 5972 KB Output is correct
17 Correct 5 ms 5948 KB Output is correct
18 Correct 5 ms 6004 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5972 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 5 ms 5972 KB Output is correct
24 Correct 4 ms 5952 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 5 ms 6004 KB Output is correct
27 Correct 5 ms 5944 KB Output is correct
28 Correct 5 ms 5992 KB Output is correct
29 Correct 5 ms 5972 KB Output is correct
30 Correct 4 ms 5972 KB Output is correct
31 Correct 4 ms 5972 KB Output is correct
32 Correct 4 ms 6072 KB Output is correct
33 Correct 102 ms 14172 KB Output is correct
34 Correct 118 ms 15096 KB Output is correct
35 Correct 24 ms 9052 KB Output is correct
36 Correct 246 ms 20520 KB Output is correct
37 Correct 256 ms 22232 KB Output is correct
38 Correct 262 ms 28460 KB Output is correct
39 Correct 251 ms 20760 KB Output is correct
40 Correct 234 ms 20760 KB Output is correct
41 Correct 241 ms 20668 KB Output is correct
42 Correct 231 ms 21652 KB Output is correct
43 Correct 159 ms 21936 KB Output is correct
44 Correct 260 ms 21700 KB Output is correct
45 Correct 236 ms 21944 KB Output is correct
46 Correct 230 ms 28096 KB Output is correct
47 Correct 98 ms 20844 KB Output is correct
48 Correct 224 ms 22120 KB Output is correct
49 Correct 272 ms 28560 KB Output is correct
50 Correct 218 ms 18700 KB Output is correct
51 Correct 260 ms 23996 KB Output is correct
52 Correct 25 ms 9604 KB Output is correct
53 Correct 251 ms 22260 KB Output is correct
54 Correct 262 ms 24756 KB Output is correct
55 Correct 308 ms 30340 KB Output is correct
56 Correct 250 ms 22312 KB Output is correct
57 Correct 261 ms 23100 KB Output is correct
58 Correct 233 ms 22732 KB Output is correct
59 Correct 240 ms 23588 KB Output is correct
60 Correct 197 ms 22784 KB Output is correct
61 Correct 254 ms 24596 KB Output is correct
62 Correct 235 ms 23676 KB Output is correct
63 Correct 258 ms 29548 KB Output is correct
64 Correct 110 ms 21804 KB Output is correct
65 Correct 224 ms 24308 KB Output is correct
66 Correct 240 ms 30568 KB Output is correct
67 Correct 22 ms 8008 KB Output is correct
68 Correct 75 ms 13648 KB Output is correct
69 Correct 133 ms 20448 KB Output is correct
70 Correct 231 ms 20532 KB Output is correct
71 Correct 268 ms 22268 KB Output is correct
72 Correct 265 ms 28320 KB Output is correct
73 Correct 224 ms 20692 KB Output is correct
74 Correct 250 ms 20864 KB Output is correct
75 Correct 243 ms 20772 KB Output is correct
76 Correct 229 ms 21820 KB Output is correct
77 Correct 168 ms 21260 KB Output is correct
78 Correct 257 ms 22064 KB Output is correct
79 Correct 222 ms 22128 KB Output is correct
80 Correct 238 ms 27008 KB Output is correct
81 Correct 107 ms 20648 KB Output is correct
82 Correct 215 ms 21960 KB Output is correct
83 Correct 258 ms 28592 KB Output is correct
84 Correct 229 ms 20824 KB Output is correct
85 Correct 269 ms 22880 KB Output is correct
86 Correct 285 ms 28808 KB Output is correct
87 Correct 274 ms 20872 KB Output is correct
88 Correct 278 ms 21492 KB Output is correct
89 Correct 244 ms 21304 KB Output is correct
90 Correct 253 ms 22136 KB Output is correct
91 Correct 190 ms 21636 KB Output is correct
92 Correct 262 ms 23648 KB Output is correct
93 Correct 229 ms 21956 KB Output is correct
94 Correct 234 ms 25148 KB Output is correct
95 Correct 110 ms 21164 KB Output is correct
96 Correct 216 ms 22600 KB Output is correct
97 Correct 231 ms 29100 KB Output is correct
98 Correct 240 ms 22192 KB Output is correct
99 Correct 265 ms 23316 KB Output is correct
100 Correct 271 ms 30076 KB Output is correct
101 Correct 223 ms 21512 KB Output is correct
102 Correct 245 ms 21980 KB Output is correct
103 Correct 240 ms 21932 KB Output is correct
104 Correct 245 ms 22740 KB Output is correct
105 Correct 162 ms 22068 KB Output is correct
106 Correct 256 ms 23204 KB Output is correct
107 Correct 212 ms 23324 KB Output is correct
108 Correct 236 ms 27180 KB Output is correct
109 Correct 105 ms 21796 KB Output is correct
110 Correct 222 ms 23376 KB Output is correct
111 Correct 240 ms 30148 KB Output is correct