Submission #546694

# Submission time Handle Problem Language Result Execution time Memory
546694 2022-04-08T07:48:23 Z Monarchuwu Unique Cities (JOI19_ho_t5) C++17
0 / 100
254 ms 16340 KB
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
#define ff first
#define ss second

const int N = 2e5 + 8;
int n, m;
vector<int> g[N];
int color[N];

int dp[N], dep[N], ptr;
void dfs_len(int u, int p = 0) {
    dp[u] = dep[u] = dep[p] + 1;
    if (dep[ptr] < dep[u]) ptr = u;

    for (int v : g[u]) if (v != p) {
        dfs_len(v, u);
        dp[u] = max(dp[u], dp[v]);
    }
}

int cnt[N], cnt_unique, ans[N];
vector<int> lst;
void add(int x) {
    if (++cnt[color[x]] == 1) ++cnt_unique;
    lst.push_back(x);
}
void del() {
    int x = lst.back(); lst.pop_back();
    if (--cnt[color[x]] == 0) --cnt_unique;
}
void dfs(int u, int p = 0) {
    if (p) add(p);

    int mx1(0), mx2(0);
    for (int v : g[u]) if (v != p) {
        if (dp[v] > mx1) mx2 = mx1, mx1 = dp[v];
        else if (dp[v] > mx2) mx2 = dp[v];
    }

    for (int v : g[u]) if (v != p) {
        int mx = mx1 + mx2 - max(dp[v], mx2);
        while (!lst.empty() && mx - dep[u] >= dep[u] - dep[lst.back()]) del();
        dfs(v, u);
    }

    while (!lst.empty() && dp[u] - dep[u] >= dep[u] - dep[lst.back()]) del();
    ans[u] = max(ans[u], cnt_unique);
}

int main() {
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1, u, v; i < n; ++i) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i) cin >> color[i];

    dfs_len(ptr = 1);
    for (int k = 0, i; k < 2; ++k) {
        dfs_len(i = ptr);
        dfs(i);
    }
    for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
}
/**  /\_/\
 *  (= ._.)
 *  / >0  \>1
**/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Incorrect 4 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 121 ms 12244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 254 ms 16340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Incorrect 4 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -