Submission #877229

#TimeUsernameProblemLanguageResultExecution timeMemory
877229CDuongPastiri (COI20_pastiri)C++17
23 / 100
77 ms35704 KiB
/* #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt") */ #include <bits/stdc++.h> #define taskname "SHEEP" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define i64 long long #define pb push_back #define ff first #define ss second #define isz(x) (int)x.size() using namespace std; const int mxN = 2e5 + 5; const int mod = 1e9 + 7; const i64 oo = 1e18; int n, k; int dis[mxN], par[mxN], dep[mxN]; bool vis[mxN], visbfs[mxN]; vector<int> G[mxN], res, cand; void dfs(int v, int p) { par[v] = p; dep[v] = dep[p] + 1; for (int u : G[v]) { if (u == p) continue; dfs(u, v); } } void bfs(int v, int len) { vector<int> reset; queue<pair<int, int>> q; q.emplace(v, 0); visbfs[v] = true; while (not q.empty()) { int v = q.front().ff, cdis = q.front().ss; q.pop(); reset.emplace_back(v); if (cdis < len) { for (int u : G[v]) if (!visbfs[u]) { q.emplace(u, cdis + 1); visbfs[u] = true; } } else { vis[v] = false; } } for (int v : reset) visbfs[v] = false; } void solve() { cin >> n >> k; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); } fill(dis + 1, dis + n + 1, -1); queue<int> q; for (int i = 1; i <= k; ++i) { int v; cin >> v; vis[v] = true; q.emplace(v); dis[v] = 0; cand.emplace_back(v); } while (not q.empty()) { int v = q.front(); q.pop(); for (int u : G[v]) { if (dis[u] == -1) { dis[u] = dis[v] + 1; q.emplace(u); } } } dfs(1, 0); sort(all(cand), [&](int x, int y) { return dep[x] > dep[y]; }); for (int v : cand) if (vis[v]) { // cout << v << endl; int cur = 0; while (v != 1 && dis[par[v]] == cur + 1) { ++cur, v = par[v]; } // cout << "chosen: " << v << " " << endl; res.emplace_back(v); bfs(v, dis[v]); } cout << isz(res) << "\n"; for (int v : res) cout << v << " "; cout << "\n"; } signed main() { #ifndef CDuongg if(fopen(taskname".inp", "r")) assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout)); #else freopen("bai3.inp", "r", stdin); freopen("bai3.out", "w", stdout); auto start = chrono::high_resolution_clock::now(); #endif ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; //cin >> t; while(t--) solve(); #ifdef CDuongg auto end = chrono::high_resolution_clock::now(); cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '='; cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl; #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...