제출 #596698

#제출 시각아이디문제언어결과실행 시간메모리
596698wiwiho열쇠 (IOI21_keys)C++17
9 / 100
694 ms120548 KiB
#include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define ef(a) emplace_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define topos(a) ((a) = (((a) % MOD + MOD) % MOD)) #define uni(a) a.resize(unique(iter(a)) - a.begin()) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } ll ifloor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a < 0) return (a - b + 1) / b; else return a / b; } ll iceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a > 0) return (a + b - 1) / b; else return a / b; } int n, m; vector<set<pii>> g, todo; // color, to vector<int> dsu, sz; vector<int> dsu2; vector<int> f; vector<set<int>> key; void init(){ g.resize(n); dsu.resize(n); sz.resize(n, 1); iota(iter(dsu), 0); f.resize(n); iota(iter(f), 0); key.resize(n); todo.resize(n); dsu2.resize(n); iota(iter(dsu2), 0); } int findDSU(int a){ if(dsu[a] != a) dsu[a] = findDSU(dsu[a]); return dsu[a]; } void unionDSU(int a, int b){ a = findDSU(a); b = findDSU(b); if(dsu[a] == dsu[b]) return; if(sz[a] < sz[b]){ swap(sz[a], sz[b]); g[a].swap(g[b]); todo[a].swap(todo[b]); } if(key[a].size() < key[b].size()) key[a].swap(key[b]); for(int i : key[b]){ set<pii>::iterator it; while(it = g[a].lower_bound(mp(i, -1)), it != g[a].end() && it->F == i){ todo[a].insert(*it); g[a].erase(it); } } for(pii i : todo[b]) todo[a].insert(i); for(pii i : g[b]){ if(key[a].find(i.F) != key[a].end()){ todo[a].insert(i); } else{ g[a].insert(i); } } sz[a] += sz[b]; dsu[b] = a; } int findDSU2(int a){ if(dsu2[a] != a) dsu2[a] = findDSU2(dsu2[a]); return dsu2[a]; } void unionDSU2(int a, int b){ a = findDSU2(a); b = findDSU2(b); dsu2[b] = a; } bool sameWCC(int a, int b){ return findDSU2(a) == findDSU2(b); } bool sameSCC(int a, int b){ return findDSU(a) == findDSU(b); } int outver(int a){ int p = findDSU(a); int v = f[p]; return findDSU(v); } void setout(int a, int v){ int p = findDSU(a); f[p] = v; } bool isroot(int a){ a = findDSU(a); return outver(a) == a; } vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c){ n = r.size(); m = u.size(); init(); for(int i = 0; i < m; i++){ if(c[i] == r[u[i]]) todo[u[i]].insert(mp(c[i], v[i])); else g[u[i]].insert(mp(c[i], v[i])); if(c[i] == r[v[i]]) todo[v[i]].insert(mp(c[i], u[i])); else g[v[i]].insert(mp(c[i], u[i])); } for(int i = 0; i < n; i++){ key[i].insert(r[i]); if(!todo[i].empty()) f[i] = todo[i].begin()->S; } //printv(f, cerr); for(int i = 0; i < n; i++){ unionDSU2(i, f[i]); } vector<bool> vst(n); auto findcycle = [&](int s){ vector<int> path; while(!vst[s]){ vst[s] = true; path.eb(s); s = f[s]; } bool ok = false; for(int i : path){ if(i == s) ok = true; } if(ok) while(path.back() != s){ unionDSU(path.back(), s); path.pob; } }; for(int i = 0; i < n; i++){ if(!vst[i]) findcycle(i); } /*cerr << "cycle\n"; for(int i = 0; i < n; i++) cerr << findDSU(i) << " "; cerr << "\n";*/ auto solve = [&](int rt){ rt = findDSU(rt); while(!todo[rt].empty()){ pii i = *todo[rt].begin(); todo[rt].erase(todo[rt].begin()); int to = findDSU(i.S); if(!sameWCC(rt, to)){ setout(rt, to); unionDSU2(rt, to); break; } while(to != rt){ unionDSU(rt, to); to = outver(to); } } }; for(int i = 0; i < n; i++){ if(findDSU(i) != i) continue; if(outver(i) == i) solve(i); } vector<int> cnt(n); for(int i = 0; i < n; i++){ if(isroot(i)) cnt[findDSU(i)]++; } int mn = MAX; for(int i = 0; i < n; i++){ if(findDSU(i) == i && isroot(i)) mn = min(mn, cnt[i]); } /*cerr << "end\n"; for(int i = 0; i < n; i++) cerr << findDSU(i) << " "; cerr << "\n"; for(int i = 0; i < n; i++) cerr << findDSU(f[findDSU(i)]) << " "; cerr << "\n"; printv(cnt, cerr);*/ vector<int> ans(n); for(int i = 0; i < n; i++){ int p = findDSU(i); if(!isroot(i)) continue; if(cnt[p] == mn) ans[i] = 1; } return ans; }
#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...