Submission #1114782

#TimeUsernameProblemLanguageResultExecution timeMemory
1114782_8_8_Unique Cities (JOI19_ho_t5)C++17
0 / 100
212 ms121572 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = (1 << 20) + 12, MOD = (int)1e9 + 7; int n, m, a[N], vis[N], timer, p[N]; vector<int> g[N], gt[N]; bool is[N]; int f(int d) { timer++; vis[d] = timer; int lst = d; queue<int> q; q.push(d); while(!q.empty()) { int v = q.front(); lst = v; q.pop(); for(int to : g[v]) { if(vis[to] != timer) { q.push(to); p[to] = v; vis[to] = timer; } } } int x = lst; timer++; vis[d] = timer; while(x != d) { vis[x] = timer; x = p[x]; } return lst; } int dist[N], col[N], dep[N], mxd[N], res[N]; void dfs(int v, int pr = -1) { mxd[v] = dep[v]; for(int to:g[v]) if(to != pr) { gt[v].push_back(to); dep[to] = dep[v] + 1; dfs(to, v); mxd[v] = max(mxd[v], mxd[to]); } } vector<int> st; set<int> cur[N]; void go(int v) { set<int> raz; for(int i = 0; i < (int)st.size() && mxd[v] - dep[v] < dep[v] - dep[st[i]]; i++) { raz.insert(a[st[i]]); cur[v].insert(a[st[i]]); } res[v] += (int)raz.size(); if(gt[v].empty()) return; int sz = (int)gt[v].size(); vector<int> p(sz), s(sz); p[0] = mxd[gt[v][0]]; s[sz - 1] = mxd[gt[v][sz - 1]]; for(int i = 1; i < sz; i++) { p[i] = max(p[i - 1], mxd[gt[v][i]]); } for(int i = sz - 1; i >= 0; i--) { s[i] = max(s[i + 1], mxd[gt[v][i]]); } for(int i = 0; i < sz; i++) { int to = gt[v][i]; int mx = dep[v]; if(i) mx = max(mx, p[i - 1]); if(i < sz - 1) mx = max(mx, s[i + 1]); mx -= dep[v]; vector<int> bf = st; while(!st.empty() && dep[v] - dep[st.back()] <= mx) { st.pop_back(); } st.push_back(v); go(to); st = bf; } } void solve(int root) { for(int i = 1; i <= n; i++) { gt[i].clear(); } st.clear(); dep[root] = 1; dfs(root); go(root); } void test() { cin >> n >> m; for(int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for(int i = 1; i <= n; i++) { cin >> a[i]; if((int)g[i].size() == 1) { is[i] = 1; } } int d = f(1), d1 = f(d); solve(d);solve(d1); for(int i = 1; i <= n; i++) { // if(is[i] && i != d && i != d1) res[i]--; // cout << res[i] << '\n'; cout << (int)cur[i].size() << '\n'; } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while(t--) test(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...