Submission #1279948

#TimeUsernameProblemLanguageResultExecution timeMemory
1279948phuocrucpp수도 (JOI20_capital_city)C++20
41 / 100
289 ms143256 KiB
/*ㅤ∧_∧  ( ・∀・)  ( つ┳⊃ ε (_)へ⌒ヽフ (  ( ・ω・) ◎―◎ ⊃ ⊃ BePhuongSuperSuwi From TK4 - CHT ㅤㅤ/ ⌒\____   /・   )  \  /ノへ ノ    /| ノ   \\ |/_/_/*/ #include<bits/stdc++.h> #define task "main" //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define endl '\n' #define int long long #define pb push_back #define fi first #define se second #define ii pair<int,int> #define iii pair<int,ii> #define iv pair<ii, ii> #define base 341 #define MASK(i) (1ll << i) #define oo 1e18 #define isOn(x,i) ((x) & MASK(i)) #define bitOn(x,i) ((x) | MASK(i)) #define bitOff(x,i) ((x) & ~MASK(i)) #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b)) using namespace std; //using namespace __gnu_pbds; const int maxn = 1e6 + 5; const int lg = 18; int n, k, val[maxn]; vector <int> g[maxn], c[maxn]; namespace giai1{ int h[maxn], par[maxn][lg + 2]; void dfs(int u, int p) { for (int x : g[u]) { if (x == p) continue; h[x] = h[u] + 1; par[x][0] = u; dfs(x, u); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u,v); for (int i = lg; i >= 0; i--) { if ((h[u] - h[v]) >= MASK(i)) u = par[u][i]; } if (u == v) return u; for (int i = lg; i >= 0; i--) { if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } } return par[u][0]; } void prelca() { dfs(1, -1); for (int j = 1; j <= lg; j++) { for (int i = 1; i <= n; i++) { par[i][j] = par[par[i][j-1]][j-1]; } } } bool ok[maxn]; int parent[maxn]; vector <int> cur; int acs(int u) { if (parent[u] == u) return u; else return parent[u] = acs(parent[u]); } void join(int u, int v) { int x = acs(u); int y = acs(v); if (x != y) { parent[x] = y; } } void connect(int u, int v) { int p = lca(u, v); while(h[u] > h[p]) { if (!ok[val[u]]) ok[val[u]] = 1, cur.pb(val[u]); join(u, par[u][0]); u = acs(u); } while(h[v] > h[p]) { if (!ok[val[v]]) ok[val[v]] = 1, cur.pb(val[v]); join(v, par[v][0]); v = acs(v); } if (!ok[val[p]]) ok[val[p]] = 1, cur.pb(val[p]); } int mini = oo; void tinh(int id) { for (int i = 1; i <= n; i++) parent[i] = i, ok[i] = 0; cur.clear(); ok[id] = 1; cur.pb(id); for (int i = 0; i < cur.size(); i++) { int x = cur[i]; for (int i = 1; i < c[x].size(); i++) { connect(c[x][0], c[x][i]); } // cout << "!" << x << " "; } // cout << id << " " << cur.size() << endl; // cout << "hi "; // for (int x : cur) // cout << x << " "; // cout << endl; mini = min(mini, (int)cur.size() - 1); } void solve() { prelca(); for (int i = 1; i <= k; i++) { tinh(i); // break; } cout << mini; } } namespace giai2{ int tin[maxn], timer = 0, tour[maxn]; int last[maxn], l[maxn], r[maxn]; vector <ii> query[maxn]; int st[4 * maxn]; vector <int> adj[maxn]; int lim = 0; vector <ii> ed; void build(int id, int l, int r) { st[id] = ++lim; if (l == r) { adj[st[id]].pb(val[tour[l]]); ed.pb({st[id], val[tour[l]]}); // cout << st[id] << " " << val[tour[l]] << endl; return; } int m = (l + r) / 2; build(id * 2, l, m); build(id * 2 + 1, m + 1, r); adj[st[id]].pb(st[id * 2]); adj[st[id]].pb(st[id * 2 + 1]); ed.pb({st[id], st[id * 2]}); ed.pb({st[id], st[id * 2 + 1]}); // cout << st[id] << " " << st[id * 2] << endl; // cout << st[id] << " " << st[id * 2 + 1] << endl; } void update(int id, int l, int r, int u, int v, int idx) { if (u > r || v < l) return; if (l >= u && r <= v) { adj[idx].pb(st[id]); ed.pb({idx, st[id]}); // cout << "! " << idx << " " << st[id] << endl; return; } int m = (l + r) / 2; update(id * 2, l, m, u, v, idx); update(id * 2 + 1, m + 1, r, u, v, idx); } void DFS(int u, int p) { tin[u] = ++timer; tour[timer] = u; for (int x : g[u]) { if (x == p) continue; DFS(x, u); } } int d[maxn], low[maxn], ssc[maxn], tplt, sz[maxn]; bool in[maxn]; stack <int> stk; void dfs(int u) { d[u] = low[u] = ++timer; stk.push(u); for (int x : adj[u]) { if (!in[x]) { if (!d[x]) dfs(x); low[u] = min(low[u], low[x]); } } if (d[u] == low[u]) { int p; tplt++; do{ p = stk.top(); in[p] = true; ssc[p] = tplt; if (p <= k) sz[tplt]++; stk.pop(); }while(p!=u); } } bool ok[maxn]; void solve() { timer = 0; int start = 1; for (int i = 1; i <= n; ++i) if ((int)g[i].size() == 1) { start = i; break; } DFS(start, -1); // cout << start << endl; lim = k; build(1, 1, n); for (int i = 1; i <= k; i++){ int t1 = oo, t2 = -oo; for (auto x : c[i]) { t1 = min(t1, tin[x]); t2 = max(t2, tin[x]); } update(1, 1, n, t1, t2, i); } timer = 0; for (int i = 1; i <= lim; i++) { if (!d[i]) dfs(i); } // cout << tplt << endl; for (auto e : ed) { int l = ssc[e.fi], r = ssc[e.se]; if (l != r) { ok[l] = 1; //cout << "! " << l << " " << r << endl; } // cout << l << " " << r << endl; } int mini = oo; for (int i = 1; i <= tplt; i++) { if (!ok[i]) { mini = min(mini, sz[i] - 1); //cout << "ssc " << i << " " << sz[i] << endl; } } cout << mini; } } main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin >> n >> k; for (int i = 1; i < n; i++) { int l, r; cin >> l >> r; g[l].pb(r); g[r].pb(l); } for (int i = 1; i <= n; i++) { cin >> val[i]; c[val[i]].pb(i); } // giai2::solve(); // giai2::solve(); if (n <= 2000) giai1::solve(); else giai2::solve(); }

Compilation message (stderr)

capital_city.cpp:269:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  269 | main() {
      | ^~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:272:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  272 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:273:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  273 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...