Submission #486617

#TimeUsernameProblemLanguageResultExecution timeMemory
486617cheissmartMergers (JOI19_mergers)C++14
100 / 100
509 ms52976 KiB
#include <bits/stdc++.h> #define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0); #define F first #define S second #define V vector #define PB push_back #define MP make_pair #define EB emplace_back #define ALL(v) (v).begin(), (v).end() using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef V<int> vi; string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m"; void DBG() { cerr << "]" << _reset << endl; } template<class H, class...T> void DBG(H h, T ...t) { cerr << to_string(h); if(sizeof ...(t)) cerr << ", "; DBG(t...); } #ifdef CHEISSMART #define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #else #define debug(...) #endif const int INF = 1e9 + 7, N = 5e5 + 7; vi G[N]; int p[N], lst[N], d[N], parent[N], deg[N]; void dfs(int u, int pa = 0) { d[u] = d[pa] + 1; parent[u] = pa; for(int v:G[u]) if(v != pa) dfs(v, u); } signed main() { IO_OP; int n, k; cin >> n >> k; for(int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; G[a].PB(b); G[b].PB(a); } dfs(1); iota(p + 1, p + n + 1, 1); function<int(int)> find = [&] (int u) { return p[u] == u ? u : p[u] = find(p[u]); }; auto cover = [&] (int u, int v) { while(find(u) != find(v)){ if(d[p[u]] > d[p[v]]) swap(u, v); p[p[v]] = parent[p[v]]; } }; for(int i = 1; i <= n; i++) { int s; cin >> s; if(lst[s]) cover(lst[s], i); lst[s] = i; } for(int i = 1; i <= n; i++) for(int j:G[i]) if(find(i) != find(j)) deg[p[i]]++; int cnt = 0; for(int i = 1; i <= n; i++) if(deg[i] == 1) cnt++; cout << (cnt + 1) / 2 << '\n'; }
#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...