제출 #796412

#제출 시각아이디문제언어결과실행 시간메모리
796412caganyanmaz열쇠 (IOI21_keys)C++17
37 / 100
3007 ms45316 KiB
#include <bits/stdc++.h> #define pb push_back using namespace std; constexpr static int SIZE = 300000; constexpr static int MX = 1e6; vector<array<int, 2>> g[SIZE]; vector<int> pending_nodes[SIZE]; bitset<SIZE> processed; int visited[SIZE]; int keys[SIZE]; vector<int> pending_keys; int counts[SIZE]; vector<int> visited_list; vector<int> r; int min_count = MX; int bfs(int node) { queue<int> q; q.push(node); int counter = 0; while (q.size()) { int j = q.front(); q.pop(); if (visited[j] == node) continue; counter++; visited[j] = node; visited_list.pb(j); if (processed[j] || counter > min_count) return MX; if (keys[r[j]] != node) { keys[r[j]] = node; for (int c : pending_nodes[r[j]]) if (visited[c] != node) q.push(c); } for (auto [c, k] : g[j]) { if (visited[c] == node) continue; if (keys[k] != node) { if (pending_nodes[k].empty()) pending_keys.pb(k); pending_nodes[k].pb(c); } else { q.push(c); } } } return counter; } void process(int node) { int res = bfs(node); processed[node] = true; for (int i : pending_keys) pending_nodes[i].clear(); pending_keys.clear(); min_count = min(res, min_count); for (int i : visited_list) counts[i] = min(counts[i], res); visited_list.clear(); } vector<int> find_reachable(vector<int> rr, vector<int> u, vector<int> v, vector<int> c) { r = rr; int n = r.size(), m = u.size(); for (int i = 0; i < m; i++) { g[v[i]].pb({u[i], c[i]}); g[u[i]].pb({v[i], c[i]}); } for (int i = 0; i < n; i++) { visited[i] = -1; keys[i] = -1; counts[i] = MX; } vector<int> ordering(n); for (int i = 0; i < n; i++) ordering[i] = i; for (int i = 0; i < n; i++) { int j = rand() % (i+1); swap(ordering[i], ordering[j]); } for (int i : ordering) process(i); vector<int> res(n, 0); for (int i = 0; i < n; i++) if (counts[i] == min_count) res[i] = 1; return res; }
#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...