Submission #1053317

#TimeUsernameProblemLanguageResultExecution timeMemory
1053317fuad27Keys (IOI21_keys)C++17
20 / 100
2969 ms2097152 KiB
#include <bits/stdc++.h> using namespace std; vector<int> u, v, c; struct node { set<int> cols; vector<int> goodEdges; set<int> curnodes; map<int,vector<int>> badEdges; int cntEdges=0; int getGoodEdge() { while(goodEdges.size()) { if(curnodes.find(u[goodEdges.back()])==curnodes.end())break; if(curnodes.find(v[goodEdges.back()])==curnodes.end())break; goodEdges.pop_back(); } if(goodEdges.size())return goodEdges.back(); return -1; } bool isHere(int vv) { if(curnodes.find(vv)!=curnodes.end())return true; return false; } }; struct dsu { vector<int> e; int cc=0; dsu(int n) { e=vector<int>(n, -1); cc=n; } int get(int x) { if(e[x]<0)return x; return e[x]=get(e[x]); } bool unite(int x, int y) { x=get(x),y=get(y); if(x==y)return false; if(-e[x] < -e[y]){ swap(x, y); } cc--; e[x]+=e[y]; e[y] = x; return true; } }; vector<int> find_reachable(vector<int> r, vector<int> U, vector<int> V, vector<int> C) { int n = r.size(), m = U.size(); vector<node> nd(n); vector<int> pending(n); iota(pending.begin(), pending.end(), 0); vector<int> id(n), par(n, -1); iota(id.begin(), id.end(), 0); u=U; v=V; c=C; for(int i = 0;i<n;i++) { id[i] = i; nd[i].cols.insert(r[i]); nd[i].curnodes.insert(i); } for(int i = 0;i<m;i++) { if(r[u[i]] == c[i]) { nd[u[i]].goodEdges.push_back(i); } else nd[u[i]].badEdges[c[i]].push_back(i); if(r[v[i]] == c[i]) { nd[v[i]].goodEdges.push_back(i); } else nd[v[i]].badEdges[c[i]].push_back(i); nd[u[i]].cntEdges++; nd[v[i]].cntEdges++; } dsu d(n); vector<int> res(n, 1e9); while(pending.size()) { int at = id[pending.back()]; pending.pop_back(); int E = nd[at].getGoodEdge(); if(E == -1) { for(int j:nd[at].curnodes)res[j]=nd[at].curnodes.size(); continue; } int to =0; if(nd[at].isHere(v[E]))to = u[E]; else to = v[E]; par[at] = to; bool cycle=false; { int cur = id[par[at]]; while(1) { if(cur == at) { cycle=1; break; } cur = par[cur]; if(cur==-1)break; cur=id[cur]; } } if(cycle) { int cur = id[par[at]]; while(cur!=at) { for(int j:nd[at].curnodes){ nd[cur].curnodes.insert(j); id[j] = cur; } for(int j:nd[at].goodEdges)nd[cur].goodEdges.push_back(j); for(auto &j:nd[at].badEdges) { bool becomesGood=false; if(nd[cur].cols.find(j.first)!=nd[cur].cols.end())becomesGood=true; for(int k:j.second) { if(becomesGood)nd[cur].goodEdges.push_back(k); else nd[cur].badEdges[j.first].push_back(k); } } for(auto j:nd[at].cols) { if(nd[cur].badEdges.find(j)!=nd[cur].badEdges.end()) { for(int k:nd[cur].badEdges[j])nd[cur].goodEdges.push_back(k); nd[cur].badEdges[j].clear(); } } nd[cur].cntEdges+=nd[at].cntEdges; at=cur; cur = id[par[cur]]; } par[at] = -1; pending.push_back(to); } else continue; } vector<int> ans(n); int mn = (*min_element(res.begin(), res.end())); for(int i = 0;i<n;i++) { if(res[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...