제출 #1127125

#제출 시각아이디문제언어결과실행 시간메모리
1127125underwaterkillerwhale수도 (JOI20_capital_city)C++20
0 / 100
251 ms65284 KiB
//#include"holiday.h" #include <bits/stdc++.h> #define ll long long #define rep(i,m,n) for(int i=(m); i<=(n); i++) #define REB(i,m,n) for(int i=(m); i>=(n); i--) #define pii pair<int,int> #define pll pair<ll,ll> #define MP make_pair #define fs first #define se second #define bit(msk, i) ((msk >> i) & 1) #define iter(id, v) for(auto id : v) #define pb push_back #define SZ(v) (ll)v.size() #define ALL(v) v.begin(),v.end() using namespace std; mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count()); ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); } const int N = 2e5 + 7; const int Mod = 1e9 + 7;///lon const ll INF = 1e18 + 7; const ll BASE = 137; const int szBL = 450; int n, K; int a[N]; vector<int> ke[N], adj[N], vec[N]; int high[N], par[N][25], Anc[N]; int num[N], low[N], del[N]; int dp[N]; void pdfs (int u, int p) { high[u] = high[p] + 1; par[u][0] = p; rep (i, 1, 20) par[u][i] = par[par[u][i - 1]][i - 1]; iter (&v, ke[u]) { if (v != p) { pdfs(v, u); } } } int LCA (int u, int v) { if (high[u] > high[v]) swap(u, v); rep (i, 0, 20) if (bit(high[v] - high[u], i)) v = par[v][i]; if (v == u) return v; REB (i, 20, 0) if (par[v][i] != par[u][i]) { v = par[v][i]; u = par[u][i]; } return par[v][0]; } void solution() { cin >> n >> K; rep (i, 2, n) { int u, v; cin >> u >> v; ke[u].pb(v); ke[v].pb(u); } pdfs(1, 0); rep (i, 1, n) { int X; cin >> X; a[i] = X; vec[X].pb(i); } rep (X, 1, K) { int anc = 0; iter (&u, vec[X]) { if (anc == 0) anc = u; anc = LCA(anc, u); } Anc[X] = anc; } function<void(int, int)> dfs = [&] (int u, int p) { if (u != Anc[a[u]]) { adj[a[u]].pb(a[p]); } iter (&v, ke[u]) { if (v == p) continue; dfs(v, u); } }; dfs(1, 0); stack<int> st; int res = K; function<void(int)> dfs2 = [&] (int u) { static int time_dfs = 0; num[u] = low[u] = ++time_dfs; st.push(u); iter (&v, adj[u]) { if (del[v]) continue; if (!num[v]) { dfs2(v); dp[u] = max(dp[u], dp[v]); low[u] = min(low[u], low[v]); } else low[u] = min(low[u], num[v]); } if (low[u] == num[u]) { int v; int sz = 0; do { v = st.top(); st.pop(); del[v] = 1; ++sz; }while (v != u); if (dp[u] == 0) { res = min(res, sz - 1); } dp[u] = 1; } }; rep (i, 1, K) { if (!num[i]) dfs2(i); } cout << res <<"\n"; } #define file(name) freopen(name".inp","r",stdin); \ freopen(name".out","w",stdout); int main () { // file("c"); ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0); int num_Test = 1; // cin >> num_Test; while (num_Test--) solution(); } /* no bug challenge +36 2 1 0 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...