結果
問題 | No.529 帰省ラッシュ |
ユーザー | beet |
提出日時 | 2019-12-17 21:08:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 378 ms / 4,500 ms |
コード長 | 11,884 bytes |
コンパイル時間 | 3,095 ms |
コンパイル使用メモリ | 242,596 KB |
実行使用メモリ | 69,232 KB |
最終ジャッジ日時 | 2024-07-04 15:12:30 |
合計ジャッジ時間 | 8,523 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 5 ms
5,376 KB |
testcase_05 | AC | 4 ms
5,376 KB |
testcase_06 | AC | 5 ms
5,376 KB |
testcase_07 | AC | 5 ms
5,376 KB |
testcase_08 | AC | 236 ms
30,864 KB |
testcase_09 | AC | 239 ms
31,460 KB |
testcase_10 | AC | 286 ms
41,512 KB |
testcase_11 | AC | 283 ms
41,892 KB |
testcase_12 | AC | 213 ms
32,132 KB |
testcase_13 | AC | 312 ms
69,232 KB |
testcase_14 | AC | 238 ms
35,704 KB |
testcase_15 | AC | 378 ms
47,368 KB |
testcase_16 | AC | 374 ms
47,508 KB |
testcase_17 | AC | 336 ms
61,144 KB |
testcase_18 | AC | 344 ms
61,668 KB |
testcase_19 | AC | 370 ms
57,576 KB |
ソースコード
#ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE class HLD { private: void dfs_sz(int v) { for(int &u:G[v]) if(u==par[v]) swap(u,G[v].back()); if(~par[v]) G[v].pop_back(); for(int &u:G[v]){ par[u]=v; dep[u]=dep[v]+1; dfs_sz(u); sub[v]+=sub[u]; if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]); } } void dfs_hld(int v,int c,int &pos) { vid[v]=pos++; inv[vid[v]]=v; type[v]=c; for(int u:G[v]){ if(u==par[v]) continue; head[u]=(u==G[v][0]?head[v]:u); dfs_hld(u,c,pos); } } public: vector< vector<int> > G; vector<int> vid, head, sub, par, dep, inv, type; HLD(int n): G(n),vid(n,-1),head(n),sub(n,1), par(n,-1),dep(n,0),inv(n),type(n){} void add_edge(int u,int v) { G[u].emplace_back(v); G[v].emplace_back(u); } void build(vector<int> rs={0}) { int c=0,pos=0; for(int r:rs){ dfs_sz(r); head[r]=r; dfs_hld(r,c++,pos); } } int lca(int u,int v){ while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]==head[v]) return u; v=par[head[v]]; } } int distance(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } // for_each(vertex) // [l, r) <- attention!! template<typename F> void for_each(int u, int v, const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); f(max(vid[head[v]],vid[u]),vid[v]+1); if(head[u]!=head[v]) v=par[head[v]]; else break; } } template<typename T,typename Q,typename F> T for_each(int u,int v,T ti,const Q &q,const F &f){ T l=ti,r=ti; while(1){ if(vid[u]>vid[v]){ swap(u,v); swap(l,r); } l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1)); if(head[u]!=head[v]) v=par[head[v]]; else break; } return f(l,r); } // for_each(edge) // [l, r) <- attention!! template<typename F> void for_each_edge(int u, int v,const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]!=head[v]){ f(vid[head[v]],vid[v]+1); v=par[head[v]]; }else{ if(u!=v) f(vid[u]+1,vid[v]+1); break; } } } }; //END CUT HERE #ifndef call_from_test #define call_from_test #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE struct LowLink{ int n,pos; vector<int> ord,low,par,blg,num; vector<vector<int> > G,C,T; vector<vector<pair<int, int> > > E; vector<int> ap; vector<pair<int, int> > bs,cand; LowLink(int n):n(n),pos(0),ord(n,-1),low(n), par(n,-1),blg(n,-1),num(n,1),G(n){} void add_edge(int u,int v){ G[u].emplace_back(v); G[v].emplace_back(u); } bool is_bridge(int u,int v){ if(ord[u]>ord[v]) swap(u,v); return ord[u]<low[v]; } void dfs(int v){ ord[v]=low[v]=pos++; for(int u:G[v]){ if(u==par[v]) continue; if(ord[u]<ord[v]) cand.emplace_back(min(u,v),max(u,v)); if(~ord[u]){ low[v]=min(low[v],ord[u]); continue; } par[u]=v; dfs(u); num[v]+=num[u]; low[v]=min(low[v],low[u]); if(is_bridge(u,v)) bs.emplace_back(u,v); if(low[u]>=ord[v]){ E.emplace_back(); while(1){ auto e=cand.back(); cand.pop_back(); E.back().emplace_back(e); if(make_pair(min(u,v),max(u,v))==e) break; } } } } void fill_component(int v){ C[blg[v]].emplace_back(v); for(int u:G[v]){ if(~blg[u]||is_bridge(u,v)) continue; blg[u]=blg[v]; fill_component(u); } } void add_component(int v,int &k){ if(~blg[v]) return; blg[v]=k++; C.emplace_back(); fill_component(v); } int build(){ for(int i=0;i<n;i++) if(ord[i]<0) dfs(i); vector<int> cnt(n,0); for(int i=0;i<n;i++){ int p=par[i]; if(p<0) continue; if(par[p]<0) cnt[p]++; else if(ord[p]<=low[i]) ap.emplace_back(p); } for(int i=0;i<n;i++) if(cnt[i]>1) ap.emplace_back(i); sort(ap.begin(),ap.end()); ap.erase(unique(ap.begin(),ap.end()),ap.end()); int k=0; for(int i=0;i<n;i++) add_component(i,k); T.assign(k,vector<int>()); for(auto e:bs){ int u=blg[e.first],v=blg[e.second]; T[u].emplace_back(v); T[v].emplace_back(u); } return k; } }; //END CUT HERE #ifndef call_from_test #define call_from_test #include "../tools/fastio.cpp" #include "../datastructure/unionfindtree.cpp" #include "../mod/mint.cpp" #include "../mod/enumeration.cpp" #undef call_from_test //INSERT ABOVE HERE signed ARC045_D(){ int n; cin>>n; vector<int> xs(2*n+1),ys(2*n+1); for(int i=0;i<2*n+1;i++) cin>>xs[i]>>ys[i]; vector<vector<int> > R(2*n+2),C(2*n+2); for(int i=0;i<2*n+1;i++) R[xs[i]].emplace_back(i); for(int i=0;i<2*n+1;i++) C[ys[i]].emplace_back(i); UnionFind uf(2*n+1); for(auto &v:R) for(auto u:v) uf.unite(v[0],u); for(auto &v:C) for(auto u:v) uf.unite(v[0],u); vector<int> vs; for(int i=0;i<2*n+1;i++){ if(uf.find(i)!=i) continue; if(uf.size(i)&1) vs.emplace_back(i); } assert(!vs.empty()); if(vs.size()>1u){ for(int i=0;i<2*n+1;i++) cout<<"NG\n"; cout<<flush; return 0; } LowLink G(2*n+1); auto add_edge= [&](auto &V)->void{ for(auto &v:V){ if(v.empty()) continue; if(!uf.same(vs[0],v[0])) continue; if(v.size()>0u) for(auto u:v) G.add_edge(v[0],u); if(v.size()>1u) for(auto u:v) G.add_edge(v[1],u); } }; add_edge(R); add_edge(C); G.build(); auto ap=G.ap; vector<int> ans(2*n+1,0); for(int i=0;i<2*n+1;i++) if(uf.same(vs[0],i)) ans[i]=1; for(int v:ap){ if(!uf.same(vs[0],v)) continue; for(int u:G.G[v]){ if(G.par[u]!=v) continue; if(~G.par[v]&&G.ord[v]>G.low[u]) continue; if(G.num[u]&1) ans[v]=0; } } for(int i=0;i<2*n+1;i++) cout<<(ans[i]?"OK\n":"NG\n"); cout<<flush; return 0; } /* verified on 2019/10/25 https://atcoder.jp/contests/arc045/tasks/arc045_d */ signed ARC062_F(){ int n,m,k; cin>>n>>m>>k; using P = pair<int, int>; map<P, int> idx; LowLink G(n); for(int i=0;i<m;i++){ int a,b; cin>>a>>b; a--;b--; G.add_edge(a,b); idx[P(a,b)]=idx[P(b,a)]=i; } G.build(); UnionFind uf(m); for(auto vs:G.E) for(auto p:vs) uf.unite(idx[p],idx[vs[0]]); vector<set<int>> cnt(m); for(auto vs:G.E){ for(auto p:vs){ cnt[uf.find(idx[p])].emplace(p.first); cnt[uf.find(idx[p])].emplace(p.second); } } using M = Mint<int>; using E = Enumeration<M>; E::init(1000); auto calc1= [&](int x)->M{ M res{0}; for(int i=0;i<x;i++) res+=M(k).pow(__gcd(i,x)); res*=E::Invs(x); return res; }; M ans{1}; for(int i=0;i<m;i++){ if(uf.find(i)!=i) continue; if(uf.size(i)< (int)cnt[i].size()) ans*=M(k).pow(uf.size(i)); if(uf.size(i)==(int)cnt[i].size()) ans*=calc1(uf.size(i)); if(uf.size(i)> (int)cnt[i].size()) ans*=E::H(k,uf.size(i)); } cout<<ans.v<<endl; return 0; } /* verified on 2019/10/25 https://atcoder.jp/contests/arc062/tasks/arc062_d */ signed main(){ //ARC045_D(); //ARC062_F(); return 0; } #endif #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE template <typename T> struct SegmentTree{ using F = function<T(T,T)>; int n; F f; T ti; vector<T> dat; SegmentTree(){} SegmentTree(F f,T ti):f(f),ti(ti){} void init(int n_){ n=1; while(n<n_) n<<=1; dat.assign(n<<1,ti); } void build(const vector<T> &v){ int n_=v.size(); init(n_); for(int i=0;i<n_;i++) dat[n+i]=v[i]; for(int i=n-1;i;i--) dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]); } void set_val(int k,T x){ dat[k+=n]=x; while(k>>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } T query(int a,int b){ if(a>=b) return ti; T vl=ti,vr=ti; for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) { if(l&1) vl=f(vl,dat[l++]); if(r&1) vr=f(dat[--r],vr); } return f(vl,vr); } template<typename C> int find(int st,C &check,T &acc,int k,int l,int r){ if(l+1==r){ acc=f(acc,dat[k]); return check(acc)?k-n:-1; } int m=(l+r)>>1; if(m<=st) return find(st,check,acc,(k<<1)|1,m,r); if(st<=l&&!check(f(acc,dat[k]))){ acc=f(acc,dat[k]); return -1; } int vl=find(st,check,acc,(k<<1)|0,l,m); if(~vl) return vl; return find(st,check,acc,(k<<1)|1,m,r); } template<typename C> int find(int st,C &check){ T acc=ti; return find(st,check,acc,1,0,n); } }; //END CUT HERE #ifndef call_from_test signed ARC038_C(){ int n; scanf("%d",&n); vector<int> cs(n),as(n); for(int i=1;i<n;i++) scanf("%d %d",&cs[i],&as[i]); const int INF = 1e9; auto f=[](int a,int b){return min(a,b);}; SegmentTree<int> seg(f,INF); seg.build(vector<int>(n,-1)); vector<int> dp(n); dp[0]=0; seg.set_val(0,0); for(int i=1;i<n;i++){ auto check=[&](int x){return x<i-cs[i];}; dp[i]=seg.find(0,check); seg.set_val(dp[i],max(seg.query(dp[i],dp[i]+1),i)); } int ans=0; for(int i=1;i<n;i++) if(as[i]&1) ans^=dp[i]; puts(ans?"First":"Second"); return 0; } /* verified on 2019/06/26 https://atcoder.jp/contests/arc038/tasks/arc038_c */ signed KUPC2013_D(){ int n; scanf("%d",&n); vector<int> as(n+2,0); for(int i=1;i<=n;i++) scanf("%d",&as[i]); const int INF = 1.1e9; auto f=[](int a,int b){return min(a,b);}; SegmentTree<int> seg(f,INF); seg.build(as); using P = pair<int, int>; set<P> sp; for(int k=0;k<2;k++){ for(int i=1;i<=n;i++){ auto check=[&](int x){return x<as[i];}; int l=seg.find(i,check); sp.emplace(k?n+2-l:l,as[i]); } reverse(as.begin(),as.end()); seg.build(as); } printf("%d\n",(int)sp.size()/2); return 0; } /* verified on 2019/06/26 https://atcoder.jp/contests/kupc2013/tasks/kupc2013_d */ signed ABC130_D(){ using ll = long long; ll n,k; cin>>n>>k; vector<ll> as(n); for(int i=0;i<n;i++) cin>>as[i]; auto f=[](ll a,ll b){return a+b;}; SegmentTree<ll> seg(f,0); seg.build(as); ll ans=0; auto check=[&](ll d){return d>=k;}; for(int i=0;i<n;i++){ int idx=seg.find(i,check); if(~idx) ans+=n-idx; } cout<<ans<<endl; return 0; } /* verified on 2019/06/26 https://atcoder.jp/contests/abc130/tasks/abc130_d */ signed main(){ //ARC038_C(); //KUPC2013_D(); //ABC130_D(); return 0; } #endif #undef call_from_test //INSERT ABOVE HERE signed YUKI_529(){ int n,e,q; scanf("%d %d %d",&n,&e,&q); LowLink big(n); for(int i=0;i<e;i++){ int u,v; scanf("%d %d",&u,&v); u--;v--; big.add_edge(u,v); } int E=0,V=big.build(); HLD hld(V); for(int i=0;i<V;i++) for(int j:big.T[i]) if(i<j) hld.add_edge(i,j),E++; hld.build(); SegmentTree<int> rmq([](int a,int b){return max(a,b);},-1); rmq.build(vector<int>(V,-1)); vector<priority_queue<int> > pq(V); map<int, int> m; int num=0; for(int i=0;i<q;i++){ int d; scanf("%d",&d); if(d==1){ int u,w; scanf("%d %d",&u,&w); u--; u=big.blg[u]; u=hld.vid[u]; m[w]=u; if(pq[u].empty()||pq[u].top()<w) rmq.set_val(u,w); pq[u].push(w); num++; } if(d==2){ int s,t; scanf("%d %d",&s,&t); s--;t--; s=big.blg[s]; t=big.blg[t]; auto f=[&](int l,int r){return rmq.query(l,r);}; int ans=hld.for_each(s,t,-1,f,rmq.f); printf("%d\n",ans); if(~ans){ int k=m[ans]; pq[k].pop(); rmq.set_val(k,(!pq[k].empty()?pq[k].top():-1)); num--; } } } return 0; } /* verified on 2019/12/17 https://yukicoder.me/problems/no/529 */ signed main(){ YUKI_529(); return 0; }; #endif