제출 #595966

#제출 시각아이디문제언어결과실행 시간메모리
595966wiwiho열쇠 (IOI21_keys)C++17
0 / 100
1 ms212 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(a, 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){ //cerr << "unionDSU2 " << a << " " << b << "\n"; a = findDSU2(a); b = findDSU2(b); dsu2[b] = 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; } //cerr << "test\n"; //printv(f, cerr); vector<bool> vst(n); auto findcycle = [&](int s){ //cerr << "findcycle " << s << "\n"; vector<int> path; while(!vst[s]){ vst[s] = true; //cerr << "visit " << s << "\n"; path.eb(s); s = f[s]; } //cerr << "to " << s << "\n"; //printv(path, cerr); bool ok = false; for(int i : path){ unionDSU2(i, s); if(i == s) ok = true; } if(ok) while(path.back() != s){ unionDSU(path.back(), s); path.pob; } //cerr << "DSU2 "; //for(int i = 0; i < n; i++) cerr << findDSU2(i) << " "; //cerr << "\n"; }; for(int i = 0; i < n; i++){ if(!vst[i]) findcycle(i); } /*cerr << "DSU2 "; for(int i = 0; i < n; i++) cerr << findDSU2(i) << " "; cerr << "\n";*/ auto solve = [&](int rt){ //cerr << "solve " << rt << " : "; rt = findDSU(rt); //printv(todo[rt], cerr); for(pii i : todo[rt]){ int to = findDSU(i.S); //cerr << "go " << i << " " << to << "\n"; if(findDSU2(rt) != findDSU2(to)){ //cerr << "change " << rt << " " << to << " " << findDSU2(rt) << " " << findDSU2(to) << "\n"; f[rt] = to; unionDSU2(rt, to); break; } while(to != rt){ //cerr << "merge " << to << "\n"; unionDSU(to, rt); to = findDSU(f[to]); } } }; for(int i = 0; i < n; i++){ if(findDSU(i) != i) continue; if(findDSU(f[i]) == i) solve(i); } vector<int> cnt(n); for(int i = 0; i < n; i++){ cnt[findDSU(i)]++; } int mn = MAX; for(int i = 0; i < n; i++){ if(findDSU(i) == i && findDSU(f[i]) == i) mn = min(mn, cnt[i]); } //cerr << mn << "\n"; /*cerr << "DSU "; for(int i = 0; i < n; i++) cerr << findDSU(i) << " "; cerr << "\n"; cerr << "f "; printv(f, cerr); for(int i = 0; i < n; i++) cerr << findDSU(f[i]) << " "; cerr << "\n"; printv(cnt, cerr);*/ vector<int> ans(n); for(int i = 0; i < n; i++){ if(cnt[findDSU(i)] == 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...