Submission #130103

#TimeUsernameProblemLanguageResultExecution timeMemory
130103qkxwsmUnique Cities (JOI19_ho_t5)C++14
100 / 100
543 ms39316 KiB
#include <bits/stdc++.h> using namespace std; template<class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template<class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) #define SZ(x) ((int) ((x).size())) #define ALL(x) (x).begin(), (x).end() #define INF 1000000007 #define LLINF 2696969696969696969ll #define MAXN 200013 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; int N, M, S, T, D; int arr[MAXN]; vi edge[MAXN]; int depth[MAXN], h[MAXN]; int dist[2][MAXN]; int freq[MAXN]; int ans[2][MAXN]; int stor; vi cur; void ins(int u) { cur.PB(u); if (freq[arr[u]]) stor--; freq[arr[u]]++; stor++; } void del() { int u = cur.back(); cur.pop_back(); stor--; freq[arr[u]]--; if (freq[arr[u]]) stor++; } void dfs(int u, int p) { h[u] = 0; for (int v : edge[u]) { if (v == p) continue; depth[v] = depth[u] + 1; dfs(v, u); ckmax(h[u], h[v] + 1); } } void solve(int u, int p, bool f) { int mx2 = 0, c = -1; for (int v : edge[u]) { if (v == p) continue; if (h[v] == h[u] - 1) { c = v; break; } } for (int v : edge[u]) { if (v == p || v == c) continue; ckmax(mx2, h[v] + 1); } if (c != -1) { while(!cur.empty() && depth[cur.back()] >= depth[u] - mx2) del(); ins(u); solve(c, u, f); } for (int v : edge[u]) { if (v == p || v == c) continue; while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del(); ins(u); solve(v, u, f); } while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del(); ans[f][u] = stor; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; FOR(i, 0, N - 1) { int u, v; cin >> u >> v; u--; v--; edge[u].PB(v); edge[v].PB(u); } FOR(i, 0, N) { cin >> arr[i]; arr[i]--; } depth[0] = 0; dfs(0, N); FOR(i, 0, N) { if (depth[i] > depth[S]) S = i; } depth[S] = 0; dfs(S, N); FOR(i, 0, N) { if (depth[i] > depth[T]) T = i; } FOR(i, 0, N) { dist[0][i] = depth[i]; } // cerr << "S = " << S << endl; solve(S, N, 0); while(!cur.empty()) del(); depth[T] = 0; dfs(T, N); FOR(i, 0, N) { dist[1][i] = depth[i]; } solve(T, N, 1); // cerr << "T = " << T << endl; FOR(i, 0, N) { // cerr << dist[0][i] << ' ' << dist[1][i] << ' ' << ans[0][i] << ' ' << ans[1][i] << endl; cout << (dist[0][i] >= dist[1][i] ? ans[0][i] : ans[1][i]) << '\n'; } return 0; //then what do you do? dfsfrom S≤ and maintain unique cities }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...