Submission #815095

#TimeUsernameProblemLanguageResultExecution timeMemory
815095blackyukiKeys (IOI21_keys)C++17
100 / 100
972 ms170880 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<bool> vb; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++) #define all(a) a.begin(),a.end() #define fi first #define se second #define pb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} const ll inf=1001001001001001001; #include "keys.h" struct UF{ ll n; vi par,sz; UF(ll n_):n(n_),par(n,-1),sz(n,1){} void merge(ll a,ll b){ a=root(a);b=root(b); if(a==b)return; if(sz[a]>sz[b])swap(a,b); par[a]=b; sz[b]+=sz[a]; } ll root(ll i){ if(par[i]==-1)return i; par[i]=root(par[i]); return par[i]; } bool same(ll a,ll b){ return root(a)==root(b); } }; std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) { ll n=r.size(),m=u.size(); vvp g(n); rep(i,m){ g[u[i]].pb(v[i],c[i]); g[v[i]].pb(u[i],c[i]); } vi par(n,-1); rep(i,n)for(P x:g[i])if(x.se==r[i])par[i]=x.fi; rep(i,n)if(par[i]==-1){ vector<int> res(n); rep(j,n)if(par[j]==-1)res[j]=1; return res; } UF uf(n); rep(i,n)uf.merge(i,par[i]); vi rt(n); vvi gr(n); vector<set<ll>> key(n); vector<map<ll,vi>> edge(n); vvi okedge(n); rep(i,n){ rt[i]=i; gr[i].pb(i); key[i].insert(r[i]); for(auto x:g[i]){ if(x.se==r[i])okedge[i].pb(x.fi); else edge[i][x.se].pb(x.fi); } } auto merge=[&](ll a,ll b){ a=rt[a];b=rt[b]; if(gr[a].size()>gr[b].size())swap(a,b); for(ll x:gr[a]){ gr[b].pb(x); rt[x]=b; } for(ll x:okedge[a]){ okedge[b].pb(x); } for(ll x:key[a]){ key[b].insert(x); if(edge[b].count(x)){ for(ll y:edge[b][x])okedge[b].pb(y); edge[b][x]=vi(0); } } for(auto x:edge[a]){ if(key[b].find(x.fi)!=key[b].end()){ for(ll y:x.se)okedge[b].pb(y); } else{ for(ll y:x.se)edge[b][x.fi].pb(y); } } }; queue<ll> que; vi id(n,-1); rep(i,n)if(id[i]==-1){ ll cur=i; vi al; while(id[cur]==-1){ al.pb(cur);id[cur]=i;cur=par[cur]; } if(id[cur]!=i)continue; reverse(all(al)); while(al.back()!=cur)al.pop_back(); rep(j,al.size()-1)merge(al[j],al[j+1]); que.push(rt[al[0]]); } vi confirm; while(!que.empty()){ ll i=que.front();que.pop(); bool done=false; while(okedge[i].size()){ ll j=okedge[i].back();okedge[i].pop_back(); if(rt[j]==rt[i])continue; if(!uf.same(i,j)){ uf.merge(i,j); par[i]=j; done=true;break; } vi al; ll cur=rt[j]; while(cur!=i){ al.pb(cur);cur=rt[par[cur]]; } al.pb(i); rep(t,al.size()-1)merge(al[t],al[t+1]); i=rt[i]; } if(!done)confirm.pb(rt[i]); } ll mi=inf; for(ll i:confirm)chmin(mi,(ll)(gr[i].size())); vector<int> res(n); for(ll i:confirm)if(mi==gr[i].size()){ for(ll j:gr[i])res[j]=1; } return res; }

Compilation message (stderr)

keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:139:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |  for(ll i:confirm)if(mi==gr[i].size()){
      |                      ~~^~~~~~~~~~~~~~
#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...