Submission #596736

#TimeUsernameProblemLanguageResultExecution timeMemory
596736wiwihoKeys (IOI21_keys)C++17
9 / 100
3086 ms73628 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<vector<pii>> tg; // to, color vector<int> dsu, sz; vector<int> dsu2; vector<int> f; vector<set<int>> key; vector<int> r, U, V, C; void init(){ g.resize(n); tg.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]); key[a].swap(key[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); } key[a].insert(i); } 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); } } //check for(pii i : g[a]){ assert(key[a].find(i.F) == key[a].end()); } for(pii i : todo[a]){ assert(key[a].find(i.F) != key[a].end()); } 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<bool> checkans(int s){ vector<vector<int>> can(n); vector<bool> ok(n), vst(n); queue<int> q; q.push(s); vst[s] = true; int cnt = 0; vector<int> ans; while(!q.empty()){ int now = q.front(); cnt++; ans.eb(now); q.pop(); if(!ok[r[now]]){ ok[r[now]] = true; for(int i : can[r[now]]){ if(vst[i]) continue; vst[i] = true; q.push(i); } } for(pii i : tg[now]){ if(!ok[i.S]){ can[i.S].eb(i.F); continue; } if(vst[i.F]) continue; vst[i.F] = true; q.push(i.F); } } return vst; } vector<int> find_reachable(vector<int> _r, vector<int> _U, vector<int> _V, vector<int> _C){ r = _r; U = _U; V = _V; C = _C; n = r.size(); m = U.size(); init(); for(int i = 0; i < m; i++){ int u = U[i], v = V[i], c = C[i]; tg[u].eb(mp(v, c)); tg[v].eb(mp(u, c)); if(c == r[u]) todo[u].insert(mp(c, v)); else g[u].insert(mp(c, v)); if(c == r[v]) todo[v].insert(mp(c, u)); else g[v].insert(mp(c, u)); } 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) return; while(path.back() != s){ unionDSU(s, path.back()); 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(isroot(i)) solve(i); } for(int i = 0; i < n; i++){ if(!isroot(i)) continue; /*vector<bool> v = checkans(i); for(int j = 0; j < n; j++){ if(!v[j]) continue; if(sameSCC(i, j)) continue; vector<bool> t = checkans(j); assert(sameWCC(i, j)); }*/ int p = findDSU(i); for(int j = 0; j < n; j++){ if(findDSU(j) != p) continue; for(pii e : tg[j]){ if(key[p].find(e.S) == key[p].end()) continue; assert(sameSCC(j, e.F) || g[p].find(mp(e.S, e.F)) != g[p].end()); } } } 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; //int tmp = checkans(i); //assert(cnt[p] <= tmp); 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...