제출 #960897

#제출 시각아이디문제언어결과실행 시간메모리
960897GrindMachineMergers (JOI19_mergers)C++17
100 / 100
1588 ms166728 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* refs: hints from edi */ const int MOD = 1e9 + 7; const int N = 5e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; vector<int> adj1[N], adj2[N]; struct lca_algo { // LCA template (for graphs with 1-based indexing) int LOG = 1; vector<int> depth; vector<vector<int>> up; vector<int> tin, tout; int timer = 1; lca_algo() { } lca_algo(int n) { lca_init(n); } void lca_init(int n) { while ((1 << LOG) < n) LOG++; up = vector<vector<int>>(n + 1, vector<int>(LOG, 1)); depth = vector<int>(n + 1); tin = vector<int>(n + 1); tout = vector<int>(n + 1); lca_dfs(1, -1); } void lca_dfs(int node, int par) { tin[node] = timer++; trav(child, adj1[node]) { if (child == par) conts; up[child][0] = node; rep1(j, LOG - 1) { up[child][j] = up[up[child][j - 1]][j - 1]; } depth[child] = depth[node] + 1; lca_dfs(child, node); } tout[node] = timer-1; } int lift(int u, int k) { rep(j, LOG) { if (k & (1 << j)) { u = up[u][j]; } } return u; } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; u = lift(u, k); if (u == v) return u; rev(j, LOG - 1, 0) { if (up[u][j] != up[v][j]) { u = up[u][j]; v = up[v][j]; } } u = up[u][0]; return u; } int get_dis(int u, int v) { int lca = query(u, v); return depth[u] + depth[v] - 2 * depth[lca]; } bool is_ances(int u, int v){ return tin[u] <= tin[v] and tout[u] >= tout[v]; } }; vector<int> here[N]; vector<int> a(N); vector<int> bad_cnt(N); void dfs1(int u, int p){ trav(v,adj1[u]){ if(v == p) conts; dfs1(v,u); bad_cnt[u] += bad_cnt[v]; } } vector<int> col(N); vector<bool> vis(N); void dfs2(int u, int p, int c){ vis[u] = 1; col[u] = c; trav(v,adj2[u]){ if(v == p) conts; dfs2(v,u,c); } } void solve(int test_case) { int n,k; cin >> n >> k; rep1(i,n-1){ int u,v; cin >> u >> v; adj1[u].pb(v), adj1[v].pb(u); } rep1(i,n) cin >> a[i]; rep1(i,n) here[a[i]].pb(i); lca_algo LCA(n); rep1(c,k){ auto &nodes = here[c]; int lca = nodes[0]; trav(u,nodes){ lca = LCA.query(lca,u); } trav(u,nodes){ bad_cnt[u]++; bad_cnt[lca]--; } } dfs1(1,-1); rep1(u,n){ trav(v,adj1[u]){ if(LCA.depth[v] > LCA.depth[u]){ if(bad_cnt[v]){ adj2[u].pb(v), adj2[v].pb(u); } } } } int ptr = 0; rep1(u,n){ if(vis[u]) conts; dfs2(u,-1,++ptr); } vector<int> deg(n+5); rep1(u,n){ trav(v,adj1[u]){ if(u < v and col[u] != col[v]){ deg[col[u]]++, deg[col[v]]++; } } } int ans = 0; if(ptr > 1){ int leaves = 0; rep1(c,ptr) leaves += (deg[c] == 1); ans = ceil2(leaves,2); } cout << ans << endl; } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...