제출 #446457

#제출 시각아이디문제언어결과실행 시간메모리
446457lohacho열쇠 (IOI21_keys)C++17
9 / 100
349 ms41360 KiB
#include <bits/stdc++.h> using namespace std; std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) { int n = (int)r.size(), m = (int)u.size(); vector<vector<int>> way(n), wayb(n); vector<vector<pair<int, int>>> wall(n); for(int i = 0; i < m; ++i){ if(r[u[i]] == c[i]){ way[u[i]].push_back(v[i]); wayb[v[i]].push_back(u[i]); } wall[u[i]].push_back({v[i], c[i]}); if(r[v[i]] == c[i]){ way[v[i]].push_back(u[i]); wayb[u[i]].push_back(v[i]); } wall[v[i]].push_back({u[i], c[i]}); } vector<int> chk(n), chk2(n); auto dfs = [&](auto&&self, int x)->int{ if(chk[x]) return x; chk[x] = 1; if(!(int)way[x].size()) return x; return self(self, way[x][0]); }; auto dfs2 = [&](auto&&self, int x)->void{ if(chk2[x]) return; chk[x] = chk2[x] = 1; for(auto&nxt:wayb[x]){ self(self, nxt); } }; vector<vector<int>> who(n); vector<int> col, same(n, -1), del; auto mdfs = [&](auto&&self, int x, int num)->void{ same[x] = num; col.push_back(r[x]); for(auto&nxt:wall[x]){ who[nxt.second].push_back(nxt.first); del.push_back(nxt.second); } for(auto&nxt:way[x]){ if(same[nxt] == -1){ self(self, nxt, num); } } }; for(int i = 0; i < n; ++i){ if(chk[i]){ continue; } int rt = dfs(dfs, i); dfs2(dfs2, rt); for(auto&nxt:wall[rt]){ who[nxt.second].push_back(nxt.first); del.push_back(nxt.second); } col.push_back(r[rt]); same[rt] = i; while((int)col.size()){ int now = col.back(); col.pop_back(); if(!(int)who[now].size()) continue; while((int)who[now].size()){ int nxt = who[now].back(); who[now].pop_back(); if(same[nxt] != -1) continue; mdfs(mdfs, nxt, i); } } while((int)del.size()){ who[del.back()].clear(); del.pop_back(); } } vector<int> cnt(n); for(int i = 0; i < n; ++i){ if(same[i] != -1){ ++cnt[same[i]]; } } int mn = (int)1e9; for(int i = 0; i < n; ++i){ if(cnt[i] > 0) mn = min(mn, cnt[i]); } vector<int> ans(n, 0); for(int i = 0; i < n; ++i){ if(same[i] != -1 && cnt[same[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...