結果

問題 No.529 帰省ラッシュ
ユーザー tko919tko919
提出日時 2020-12-07 19:28:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 620 ms / 4,500 ms
コード長 4,477 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 196,264 KB
実行使用メモリ 39,728 KB
最終ジャッジ日時 2023-10-17 16:37:25
合計ジャッジ時間 12,732 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 9 ms
4,348 KB
testcase_07 AC 9 ms
4,348 KB
testcase_08 AC 556 ms
18,264 KB
testcase_09 AC 528 ms
19,056 KB
testcase_10 AC 535 ms
24,424 KB
testcase_11 AC 553 ms
24,828 KB
testcase_12 AC 457 ms
17,884 KB
testcase_13 AC 442 ms
39,728 KB
testcase_14 AC 560 ms
19,328 KB
testcase_15 AC 614 ms
27,076 KB
testcase_16 AC 620 ms
27,076 KB
testcase_17 AC 540 ms
29,696 KB
testcase_18 AC 546 ms
29,960 KB
testcase_19 AC 551 ms
28,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

struct LowLink{
   const int n; vector<vector<int>> g;
   vector<int> used,ord,low,cmp;
   LowLink(const int& _n):n(_n),g(n),
      used(n,0),ord(n,0),low(n,0),cmp(n,-1){
   }
   void add_edge(int u,int v){
      g[u].emplace_back(v); g[v].emplace_back(u);
   }
   void dfs(int v,int p,int& k){
      used[v]=1; low[v]=ord[v]=k++;
      int cnt=0,sub=0;
      for(auto& to:g[v]){
         if(to==p and (++sub)<=1)continue;
         if(!used[to]){
            cnt++; dfs(to,v,k);
            chmin(low[v],low[to]);
         }
         else chmin(low[v],ord[to]);
      }
   }
   void dfs2(int v,int p,int& k){
      if(p!=-1 and ord[p]>=low[v])cmp[v]=cmp[p];
      else cmp[v]=k++;
      for(auto& to:g[v])if(cmp[to]==-1)dfs2(to,v,k);
   }
   int run(){
      int k=0; rep(i,0,n)if(!used[i])dfs(i,-1,k);
      k=0; rep(i,0,n)if(cmp[i]==-1)dfs2(i,-1,k);
      return k;
   }
};

struct HLD{
   using P=pair<int,int>;
   vector<vector<int>> g; vector<int> sz,in,out,hs,par;
   void dfs(int v,int p){
      par[v]=p; sz[v]=1;
      if(!g[v].empty() and g[v][0]==p)swap(g[v][0],g[v].back());
      for(auto& to:g[v])if(to!=p){
         dfs(to,v); sz[v]+=sz[to];
         if(sz[g[v][0]]<sz[to])swap(g[v][0],to);
      }
   }
   void dfs2(int v,int p,int& k){
      in[v]=k++;
      for(auto& to:g[v])if(to!=p){
         hs[to]=(g[v][0]==to?hs[v]:to);
         dfs2(to,v,k);
      }
      out[v]=k;
   }
   HLD(int _n):g(_n),sz(_n),in(_n),out(_n),hs(_n),par(_n){}
   void add_edge(int u,int v){
      g[u].emplace_back(v); g[v].emplace_back(u);
   }
   void run(){dfs(0,-1); int k=0; dfs2(0,-1,k);}
   int lca(int u,int v){
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])return u;
      }
   }
   vector<P> get(int u,int v,bool es=0){
      vector<P> res;
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])break;
         res.emplace_back(in[hs[v]],in[v]+1);
      }
      res.emplace_back(in[u]+es,in[v]+1);
      return res;
   }
};

template<typename M,typename N,M (*f)(M,M),M (*g)(M,N)>struct SegmentTree{
   int sz; vector<M> data; const M m1;
   SegmentTree(int n,const M &m1):m1(m1){
      sz=1; while(sz<n)sz<<=1; data.assign(2*sz,m1);
   }
   void run(vector<M> v){
      rep(i,0,v.size())data[i+sz]=v[i];
      for(int k=sz-1;k>0;k--)data[k]=f(data[2*k],data[2*k+1]);
   }
   void update(int k,const N &x){
      k+=sz; data[k]=g(data[k],x);
      while(k>>=1)data[k]=f(data[2*k],data[2*k+1]);
   }
   M query(int a,int b){
      M L=m1,R=m1;
      for(a+=sz,b+=sz;a<b;a>>=1,b>>=1){
         if(a&1)L=f(L,data[a++]);
         if(b&1)R=f(data[--b],R);
      } return f(L,R);
   }
   M operator[](const int &k)const{return data[k+sz];}
};

typedef pair<int,int> P;
P f(P a,P b){return max(a,b);}
P g(P a,P b){return b;}

int main(){
   int n,m,q; cin>>n>>m>>q;
   LowLink low(n);
   vector<P> es;
   rep(i,0,m){
      int x,y; cin>>x>>y;
      x--; y--;
      es.push_back({x,y});
      low.add_edge(x,y);
   }
   int d=low.run();
   HLD hld(d);
   for(auto& p:es){
      int u=low.cmp[p.first];
      int v=low.cmp[p.second];
      if(u!=v){
         hld.add_edge(u,v);
      }
   }
   hld.run();
   SegmentTree<P,P,f,g> seg(n,{-1,inf});
   vector<set<int>> qs(d);
   while(q--){
      int t; cin>>t;
      if(t==1){
         int u,w; cin>>u>>w;
         u--; u=low.cmp[u];
         qs[u].insert(w);
         seg.update(hld.in[u],{*qs[u].rbegin(),u});
      }
      else{
         int u,v; cin>>u>>v;
         u--; v--;
         u=low.cmp[u],v=low.cmp[v];
         if(u>v)swap(u,v);
         auto ps=hld.get(u,v);
         P mx={-1,-1};
         for(auto& p:ps){
            chmax(mx,seg.query(p.first,p.second));
         }
         if(mx.first==-1)puts("-1");
         else{
            int id=mx.second;
            qs[id].erase(mx.first);
            if(qs[id].empty())seg.update(hld.in[id],{-1,id});
            else seg.update(hld.in[id],{*qs[id].rbegin(),id});
            cout<<mx.first<<endl;
         }
      }
   }
   return 0;
}
0