結果

問題 No.529 帰省ラッシュ
ユーザー beetbeet
提出日時 2017-06-10 01:48:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,489 ms / 4,500 ms
コード長 5,378 bytes
コンパイル時間 4,460 ms
コンパイル使用メモリ 194,636 KB
実行使用メモリ 237,352 KB
最終ジャッジ日時 2023-08-22 05:29:53
合計ジャッジ時間 28,185 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 12 ms
4,720 KB
testcase_05 AC 14 ms
4,952 KB
testcase_06 AC 14 ms
4,756 KB
testcase_07 AC 15 ms
4,832 KB
testcase_08 AC 1,125 ms
39,456 KB
testcase_09 AC 1,219 ms
46,200 KB
testcase_10 AC 2,264 ms
82,416 KB
testcase_11 AC 2,299 ms
83,376 KB
testcase_12 AC 748 ms
35,580 KB
testcase_13 AC 4,489 ms
237,352 KB
testcase_14 AC 484 ms
36,768 KB
testcase_15 AC 2,607 ms
85,936 KB
testcase_16 AC 2,943 ms
85,912 KB
testcase_17 AC 1,065 ms
66,580 KB
testcase_18 AC 980 ms
66,140 KB
testcase_19 AC 999 ms
66,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct BiconectedGraph{
  typedef pair<int,int> P;
  vector<vector<int> > G,C,T;
  vector<int> ord,low,belong;
  vector<P> B;
  int V;
  BiconectedGraph(){}
  BiconectedGraph(int n){
    G.clear();
    C.clear();
    T.clear();
    G.resize(n);
    C.resize(n);
    T.resize(n);
  }
  bool is_bridge(int u,int v){
    if(ord[u]>ord[v]) swap(u,v);
    return ord[u]<low[v];
  }
  void dfs(int u,int p,int &k){
    ord[u]=low[u]=k;
    ++k;
    for(int v:G[u]){
      if(v==p) continue;
      if(ord[v]>=0){
	low[u]=min(low[u],ord[v]);
      }else{
	dfs(v,u,k);
	low[u]=min(low[u],low[v]);
      }
      if(is_bridge(u,v)) B.push_back(P(u,v));
    }
  }
  void fill_component(int c,int u){
    C[c].push_back(u);
    belong[u]=c;
    for(int v:G[u]){
      if(belong[v]>=0||is_bridge(u,v)) continue;
      fill_component(c,v);
    }
  }
  void add_component(int u,int &k){
    if(belong[u]>=0) return;
    fill_component(k++,u);
  }
  
  void biconnectedgraph(int n){
    int k=0;
    ord.clear();
    ord.resize(n,-1);
    low.clear();
    low.resize(n);
    belong.clear();
    belong.resize(n,-1);
    for(int u=0;u<n;u++){
      if(ord[u]>=0) continue;
      dfs(u,-1,k);
    }
    k=0;
    for(int i=0;i<(int)B.size();i++){
      add_component(B[i].first,k);
      add_component(B[i].second,k);
    }
    add_component(0,k);
    V=k;
    for(int i=0;i<(int)B.size();i++){
      int u=belong[B[i].first],v=belong[B[i].second];
      T[u].push_back(v);
      T[v].push_back(u);
    }
  }
};
struct HLDecomposition {
  vector<vector<int>> g;

  // vid, head, heavy, parent は必須
  // depth, inv は使用する機能によっては不要
  vector<int> vid, head, heavy, parent, depth, inv;

  HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {}

  // 辺 (u, v) を追加する
  void add(int u, int v) {
    g[u].push_back(v);
    //g[v].push_back(u);
  }

  // 構築する
  void build() {
    dfs(0, -1);
    bfs();
  }

  int dfs(int curr, int prev) {
    parent[curr] = prev;
    int sub = 1, max_sub = 0;
    for (int next : g[curr]) if (next != prev) {
	depth[next] = depth[curr] + 1;
	int sub_next = dfs(next, curr);
	sub += sub_next;
	if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
      }
    return sub;
  }

  void bfs() {
    int k = 0;
    queue<int> q({ 0 });
    while (!q.empty()) {
      int h = q.front(); q.pop();
      for (int i = h; i != -1; i = heavy[i]) {
	vid[i] = k++;
	//cout<<h<<"-"<<parent[h]<<":"<<i<<":"<<vid[i]<<endl;
	inv[vid[i]] = i;
	head[i] = h;
	for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j);
      }
    }
    //cout<<k<<endl;
  }
  // 頂点属性の for_each
  void for_each(int u, int v, function<void(int, int)> f) {
    if (vid[u] > vid[v]) swap(u, v);
    f(max(vid[head[v]], vid[u]), vid[v]);
    if (head[u] != head[v]) for_each(u, parent[head[v]], f);
  }
};

struct RMQ{
  int n;
  vector<set<int> > dat;
  RMQ(){}
  RMQ(int n_){init(n_);}
  void init(int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1);
  }
  void update(int k,int a){
    //cout<<k<<" "<<a<<endl;
    k+=n-1;
    assert(!dat[k].count(a));
    dat[k].insert(a);
    while(k>0){
      k=(k-1)/2;
      assert(!dat[k].count(a));
      dat[k].insert(a);
    }
  }
  void remove(int k,int a){
    //cout<<k<<" "<<a<<endl;
    k+=n-1;
    assert(dat[k].count(a));
    dat[k].erase(a);
    while(k>0){
      k=(k-1)/2;
      assert(dat[k].count(a));
      dat[k].erase(a);
    }
  }
  int query(int a,int b,int k,int l,int r){
    if(r<=a||b<=l) return -1;
    if(a<=l&&r<=b){
      if(dat[k].empty()) return -1;
      return *--dat[k].end();
    }
    int vl=query(a,b,k*2+1,l,(l+r)/2);
    int vr=query(a,b,k*2+2,(l+r)/2,r);
    return max(vl,vr);
  }
  int query(int a,int b){
    //cout<<a<<" "<<b<<endl;
    return query(a,b,0,0,n);
  }
};

signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n,e,q;
  cin>>n>>e>>q;
  BiconectedGraph big(n);
  for(int i=0;i<e;i++){
    int s,t;
    cin>>s>>t;
    s--;t--;
    //cout<<s<<" "<<t<<endl;
    big.G[s].push_back(t);
    big.G[t].push_back(s);
  }
  big.biconnectedgraph(n);

  int E=0;
  
  int V=big.V;
  HLDecomposition hl(V+1000);
  for(int i=0;i<V;i++)
    for(int j:big.T[i]) hl.add(i,j),E++;
  E/=2;
  assert(V==E+1);
  
  //cout<<V<<" "<<E<<endl;
  
  /*//
  cout<<V<<endl;
  for(int i=0;i<V;i++)
    for(int j:big.T[i]) cout<<i<<"->"<<j<<endl;
  for(int i=0;i<V;i++){
    cout<<i<<":";
    for(int j:big.C[i]) cout<<j<<" ";
    cout<<endl;
  }
  //*/

  
  hl.build();
  RMQ rmq(V);
  map<int,int> m;
  int num=0;
  set<int> as;
  for(int i=0;i<q;i++){
    int d;
    cin>>d;
    if(d==1){
      int u,w;
      cin>>u>>w;
      u--;
      u=big.belong[u];
      u=hl.vid[u];
      //cout<<u<<":"<<w<<endl;
      m[w]=u;
      rmq.update(m[w],w);
      num++;
    }
    if(d==2){
      int s,t;
      cin>>s>>t;
      s--;t--;
      s=big.belong[s];
      t=big.belong[t];
      int ans=-1;
      //cout<<s<<"-"<<t<<endl;
      //cout<<" "<<hl.vid[s]<<" "<<hl.vid[t]<<endl;
      hl.for_each(s, t, [&](int l, int r) {
	  ans = max(ans,rmq.query(l, r + 1));
	  //cout<<ans<<endl;
	});
      cout<<ans<<endl;
      if(~ans) rmq.remove(m[ans],ans),num--;
    }
    assert(num==(int)rmq.dat[0].size());
  }
  return 0;
}
0