Submission #1067866

#TimeUsernameProblemLanguageResultExecution timeMemory
1067866PlurmTorrent (COI16_torrent)C++11
0 / 100
2033 ms40892 KiB
#include <bits/stdc++.h> using namespace std; vector<int> g[300005]; int dep[300005]; vector<int> par[300005]; int chcnt[300005]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, a, b; cin >> n >> a >> b; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } memset(dep, 0x3f, sizeof(dep)); dep[a] = dep[b] = 0; queue<int> q; q.push(a); q.push(b); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (dep[v] > dep[u] + 1) { dep[v] = dep[u] + 1; par[v] = {u}; q.push(v); } else if (dep[v] == dep[u] + 1) { par[v].push_back(u); } } } priority_queue<pair<int, int>> pq; for (int i = 1; i <= n; i++) { for (int p : par[i]) chcnt[p]++; } for (int i = 1; i <= n; i++) { if (chcnt[i] == 0 && i != a && i != b) pq.push({dep[i], i}); } int rnd = 0; while (!pq.empty()) { rnd++; vector<int> waitlist; set<int> parf; while (!pq.empty()) { auto p = pq.top(); pq.pop(); int chosen = -1; for (int pr : par[p.second]) { if (!parf.count(pr)) { chosen = pr; } } if (chosen == -1) { waitlist.push_back(p.second); } else { parf.insert(chosen); for (int pr : par[p.second]) { chcnt[pr]--; } } } for (int u : parf) { if (chcnt[u] == 0 && u != 0 && u != a && u != b) pq.push({dep[u], u}); } for (int u : waitlist) pq.push({dep[u], u}); } cout << rnd << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...