Submission #986818

# Submission time Handle Problem Language Result Execution time Memory
986818 2024-05-21T09:39:12 Z vyshniak_n Unique Cities (JOI19_ho_t5) C++17
100 / 100
408 ms 76520 KB
//#pragma optimize("Ofast")
//#pragma optimize("unroll-loops")
//#pragma traget("avx,avx2")

#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define el '\n'
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define point pair <ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

#include <random>
mt19937 rnd(time(0));

const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll N = 4e5 + 10;
const ll mod = 1e9 + 7;
const ll K = 200;
const ll LOG = 8;

vector <ll> gr[N], dist[2];
ll a[N], mx[2][N];

void dfs(ll v, ll pr, ll id, ll h = 1) {
    dist[id][v] = h;
    mx[id][v] = h;
    for (ll to : gr[v]) {
        if (to == pr)
            continue;

        dfs(to, v, id, h + 1);
        mx[id][v] = max(mx[id][v], mx[id][to]);
    }
}

ll cnt[N], cur = 0, ans[N];
vector <ll> st;

void add(ll x) {
    cnt[a[x]]++;
    if (cnt[a[x]] == 1)
        cur++;
}
void del(ll x) {
    cnt[a[x]]--;
    if (cnt[a[x]] == 0)
        cur--;
}

void calc(ll v, ll pr, ll id) {
    vector <point> children;
    for (ll to : gr[v]) {
        if (to == pr)
            continue;

        children.pb({mx[id][to], to});
    }

    sort(rall(children));
    while (children.size() > 1 && !st.empty() && dist[id][st.back()] >= 2 * dist[id][v] - children[1].ff) {
        del(st.back());
        st.popb();
    }

    for (ll i = 0; i < children.size(); i++) {
        ll to = children[i].ss;

        add(v);
        st.pb(v);

        calc(to, v, id);

        while (!st.empty() && dist[id][st.back()] >= 2 * dist[id][v] - children[0].ff) {
            del(st.back());
            st.popb();
        }
    }

    if (dist[id][v] >= dist[id ^ 1][v])
        ans[v] = cur;
}

void solve() {
    ll n, m;
    cin >> n >> m;

    for (ll i = 1; i < n; i++) {
        ll u, v;
        cin >> u >> v;

        gr[u].pb(v);
        gr[v].pb(u);
    }

    for (ll i = 1; i <= n; i++)
        cin >> a[i];

    ll d1, d2;
    dist[0].resize(n + 1);
    dist[1].resize(n + 1);

    dfs(1, 1, 0);
    d1 = max_element(all(dist[0])) - dist[0].begin();
    dfs(d1, d1, 0);
    d2 = max_element(all(dist[0])) - dist[0].begin();
    dfs(d2, d2, 1);

    calc(d1, d1, 0);
    calc(d2, d2, 1);

    for (ll i = 1; i <= n; i++)
        cout << ans[i] << el;
    return;
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int tests = 1;
    //cin >> tests;

    while (tests--) 
        solve();
    return 0;
}
/*
*/

Compilation message

joi2019_ho_t5.cpp: In function 'void calc(ll, ll, ll)':
joi2019_ho_t5.cpp:107:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for (ll i = 0; i < children.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16984 KB Output is correct
2 Correct 4 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 4 ms 17336 KB Output is correct
5 Correct 4 ms 17244 KB Output is correct
6 Correct 4 ms 17500 KB Output is correct
7 Correct 4 ms 17244 KB Output is correct
8 Correct 5 ms 17316 KB Output is correct
9 Correct 4 ms 17240 KB Output is correct
10 Correct 4 ms 17244 KB Output is correct
11 Correct 5 ms 17244 KB Output is correct
12 Correct 4 ms 17244 KB Output is correct
13 Correct 5 ms 17500 KB Output is correct
14 Correct 5 ms 17240 KB Output is correct
15 Correct 4 ms 17244 KB Output is correct
16 Correct 4 ms 17136 KB Output is correct
17 Correct 5 ms 17500 KB Output is correct
18 Correct 5 ms 17396 KB Output is correct
19 Correct 5 ms 17244 KB Output is correct
20 Correct 5 ms 17500 KB Output is correct
21 Correct 5 ms 17244 KB Output is correct
22 Correct 4 ms 17244 KB Output is correct
23 Correct 4 ms 17216 KB Output is correct
24 Correct 4 ms 17244 KB Output is correct
25 Correct 5 ms 17244 KB Output is correct
26 Correct 5 ms 17132 KB Output is correct
27 Correct 5 ms 15452 KB Output is correct
28 Correct 4 ms 15452 KB Output is correct
29 Correct 5 ms 17244 KB Output is correct
30 Correct 6 ms 17244 KB Output is correct
31 Correct 5 ms 17384 KB Output is correct
32 Correct 5 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 27988 KB Output is correct
2 Correct 191 ms 49804 KB Output is correct
3 Correct 22 ms 23896 KB Output is correct
4 Correct 251 ms 34896 KB Output is correct
5 Correct 329 ms 72984 KB Output is correct
6 Correct 300 ms 53292 KB Output is correct
7 Correct 226 ms 36360 KB Output is correct
8 Correct 235 ms 39248 KB Output is correct
9 Correct 224 ms 37968 KB Output is correct
10 Correct 281 ms 38340 KB Output is correct
11 Correct 169 ms 42060 KB Output is correct
12 Correct 322 ms 60108 KB Output is correct
13 Correct 278 ms 55008 KB Output is correct
14 Correct 361 ms 53368 KB Output is correct
15 Correct 129 ms 42240 KB Output is correct
16 Correct 264 ms 63140 KB Output is correct
17 Correct 307 ms 54312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 33108 KB Output is correct
2 Correct 378 ms 73348 KB Output is correct
3 Correct 25 ms 24916 KB Output is correct
4 Correct 249 ms 36992 KB Output is correct
5 Correct 408 ms 76520 KB Output is correct
6 Correct 322 ms 55752 KB Output is correct
7 Correct 229 ms 37504 KB Output is correct
8 Correct 298 ms 45984 KB Output is correct
9 Correct 271 ms 42700 KB Output is correct
10 Correct 248 ms 40784 KB Output is correct
11 Correct 277 ms 39224 KB Output is correct
12 Correct 342 ms 67780 KB Output is correct
13 Correct 363 ms 55864 KB Output is correct
14 Correct 298 ms 54340 KB Output is correct
15 Correct 148 ms 43184 KB Output is correct
16 Correct 301 ms 65136 KB Output is correct
17 Correct 312 ms 56560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16984 KB Output is correct
2 Correct 4 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 4 ms 17336 KB Output is correct
5 Correct 4 ms 17244 KB Output is correct
6 Correct 4 ms 17500 KB Output is correct
7 Correct 4 ms 17244 KB Output is correct
8 Correct 5 ms 17316 KB Output is correct
9 Correct 4 ms 17240 KB Output is correct
10 Correct 4 ms 17244 KB Output is correct
11 Correct 5 ms 17244 KB Output is correct
12 Correct 4 ms 17244 KB Output is correct
13 Correct 5 ms 17500 KB Output is correct
14 Correct 5 ms 17240 KB Output is correct
15 Correct 4 ms 17244 KB Output is correct
16 Correct 4 ms 17136 KB Output is correct
17 Correct 5 ms 17500 KB Output is correct
18 Correct 5 ms 17396 KB Output is correct
19 Correct 5 ms 17244 KB Output is correct
20 Correct 5 ms 17500 KB Output is correct
21 Correct 5 ms 17244 KB Output is correct
22 Correct 4 ms 17244 KB Output is correct
23 Correct 4 ms 17216 KB Output is correct
24 Correct 4 ms 17244 KB Output is correct
25 Correct 5 ms 17244 KB Output is correct
26 Correct 5 ms 17132 KB Output is correct
27 Correct 5 ms 15452 KB Output is correct
28 Correct 4 ms 15452 KB Output is correct
29 Correct 5 ms 17244 KB Output is correct
30 Correct 6 ms 17244 KB Output is correct
31 Correct 5 ms 17384 KB Output is correct
32 Correct 5 ms 17500 KB Output is correct
33 Correct 85 ms 27988 KB Output is correct
34 Correct 191 ms 49804 KB Output is correct
35 Correct 22 ms 23896 KB Output is correct
36 Correct 251 ms 34896 KB Output is correct
37 Correct 329 ms 72984 KB Output is correct
38 Correct 300 ms 53292 KB Output is correct
39 Correct 226 ms 36360 KB Output is correct
40 Correct 235 ms 39248 KB Output is correct
41 Correct 224 ms 37968 KB Output is correct
42 Correct 281 ms 38340 KB Output is correct
43 Correct 169 ms 42060 KB Output is correct
44 Correct 322 ms 60108 KB Output is correct
45 Correct 278 ms 55008 KB Output is correct
46 Correct 361 ms 53368 KB Output is correct
47 Correct 129 ms 42240 KB Output is correct
48 Correct 264 ms 63140 KB Output is correct
49 Correct 307 ms 54312 KB Output is correct
50 Correct 163 ms 33108 KB Output is correct
51 Correct 378 ms 73348 KB Output is correct
52 Correct 25 ms 24916 KB Output is correct
53 Correct 249 ms 36992 KB Output is correct
54 Correct 408 ms 76520 KB Output is correct
55 Correct 322 ms 55752 KB Output is correct
56 Correct 229 ms 37504 KB Output is correct
57 Correct 298 ms 45984 KB Output is correct
58 Correct 271 ms 42700 KB Output is correct
59 Correct 248 ms 40784 KB Output is correct
60 Correct 277 ms 39224 KB Output is correct
61 Correct 342 ms 67780 KB Output is correct
62 Correct 363 ms 55864 KB Output is correct
63 Correct 298 ms 54340 KB Output is correct
64 Correct 148 ms 43184 KB Output is correct
65 Correct 301 ms 65136 KB Output is correct
66 Correct 312 ms 56560 KB Output is correct
67 Correct 21 ms 21336 KB Output is correct
68 Correct 109 ms 42944 KB Output is correct
69 Correct 201 ms 40652 KB Output is correct
70 Correct 227 ms 34900 KB Output is correct
71 Correct 389 ms 71972 KB Output is correct
72 Correct 321 ms 49896 KB Output is correct
73 Correct 256 ms 34336 KB Output is correct
74 Correct 294 ms 41556 KB Output is correct
75 Correct 292 ms 38984 KB Output is correct
76 Correct 304 ms 38416 KB Output is correct
77 Correct 180 ms 38316 KB Output is correct
78 Correct 323 ms 64252 KB Output is correct
79 Correct 289 ms 59100 KB Output is correct
80 Correct 290 ms 51412 KB Output is correct
81 Correct 139 ms 41980 KB Output is correct
82 Correct 263 ms 63136 KB Output is correct
83 Correct 325 ms 54248 KB Output is correct
84 Correct 270 ms 35668 KB Output is correct
85 Correct 329 ms 74212 KB Output is correct
86 Correct 300 ms 53708 KB Output is correct
87 Correct 328 ms 35780 KB Output is correct
88 Correct 245 ms 44300 KB Output is correct
89 Correct 256 ms 38700 KB Output is correct
90 Correct 238 ms 37152 KB Output is correct
91 Correct 208 ms 35204 KB Output is correct
92 Correct 344 ms 70908 KB Output is correct
93 Correct 268 ms 48224 KB Output is correct
94 Correct 326 ms 44708 KB Output is correct
95 Correct 131 ms 41476 KB Output is correct
96 Correct 258 ms 61500 KB Output is correct
97 Correct 294 ms 53104 KB Output is correct
98 Correct 220 ms 35332 KB Output is correct
99 Correct 344 ms 71308 KB Output is correct
100 Correct 322 ms 53960 KB Output is correct
101 Correct 262 ms 35544 KB Output is correct
102 Correct 268 ms 41240 KB Output is correct
103 Correct 305 ms 38712 KB Output is correct
104 Correct 231 ms 37800 KB Output is correct
105 Correct 208 ms 37984 KB Output is correct
106 Correct 294 ms 57256 KB Output is correct
107 Correct 303 ms 57824 KB Output is correct
108 Correct 321 ms 48472 KB Output is correct
109 Correct 179 ms 41724 KB Output is correct
110 Correct 290 ms 63408 KB Output is correct
111 Correct 295 ms 54504 KB Output is correct