結果

問題 No.529 帰省ラッシュ
ユーザー tonegawatonegawa
提出日時 2020-05-12 17:00:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,717 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 136,032 KB
実行使用メモリ 114,644 KB
最終ジャッジ日時 2023-10-11 16:36:14
合計ジャッジ時間 21,618 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
73,116 KB
testcase_01 AC 82 ms
73,112 KB
testcase_02 AC 83 ms
73,048 KB
testcase_03 AC 83 ms
73,116 KB
testcase_04 AC 101 ms
73,584 KB
testcase_05 AC 102 ms
73,372 KB
testcase_06 AC 102 ms
73,848 KB
testcase_07 AC 102 ms
73,540 KB
testcase_08 AC 1,081 ms
111,840 KB
testcase_09 AC 1,002 ms
108,920 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 666 ms
105,528 KB
testcase_13 AC 530 ms
113,488 KB
testcase_14 AC 1,002 ms
114,644 KB
testcase_15 AC 2,300 ms
103,764 KB
testcase_16 AC 2,337 ms
103,684 KB
testcase_17 AC 1,101 ms
111,216 KB
testcase_18 AC 1,103 ms
111,544 KB
testcase_19 AC 1,120 ms
109,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

//二重辺連結成分分解
struct Graph{
  int MAX_V;
  vector<set<ll>> G;
  vector<set<ll>> rG;
  vector<bool> used;
  vector<int> cmp;
  vector<int> vs;
  Graph(ll V){
    MAX_V = V;
    G = vector<set<ll>>(MAX_V+1);
    rG = vector<set<ll>>(MAX_V+1);
    used.resize(MAX_V+1, false);
    cmp.resize(MAX_V+1, 0);
  }
  void add_edge(int from, int to){
    G[from].insert(to);
    G[to].insert(from);
    rG[from].insert(to);
    rG[to].insert(from);
  }
  void dfs(int v){
    used[v] = true;
    for(auto to:G[v]) {
      ll a = v, b = to;
      G[b].erase(a);
      rG[a].erase(b);
      if(!used[to]) dfs(to);
    }
    vs.push_back(v);
  }
  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for(auto to:rG[v]) {
      if(!used[to]) rdfs(to, k);
    }
  }
  int scc(int V){
    for(int i=0;i<MAX_V;i++) used[i] = false;
    vs.clear();
    for(int i=0;i<MAX_V;i++) if(!used[i]) dfs(i);
    for(int i=0;i<MAX_V;i++) used[i] = false;
    int k = 0;
    for(int i=vs.size()-1;i>=0;i--) if(!used[vs[i]]) rdfs(vs[i], k++);
    return k;
  }
  //変換後の頂点->辺群
  vvl build(ll n){
    vvl ret = VV(MAX_V+1, 0, 0, ll);
    vector<set<ll>> T(MAX_V+1);
    scc(n);
    for(int i=0;i<=n;i++){
      for(auto to:G[i]){
        T[cmp[i]].insert(cmp[to]);
        T[cmp[to]].insert(cmp[i]);
      }
    }
    for(int i=0;i<=n;i++){
      for(auto to:T[i]){
        ret[i].push_back(to);
      }
    }
    return ret;
  }
};
template< typename G >
struct HeavyLightDecomposition {
  G &g;
  vector< int > sz, in, out, head, rev, par;

  HeavyLightDecomposition(G &g) :
      g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}

  void dfs_sz(int idx, int p) {
    par[idx] = p;
    sz[idx] = 1;
    if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
    for(auto &to : g[idx]) {
      if(to == p) continue;
      dfs_sz(to, idx);
      sz[idx] += sz[to];
      if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
    }
  }

  void dfs_hld(int idx, int par, int &times) {
    in[idx] = times++;
    rev[in[idx]] = idx;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      head[to] = (g[idx][0] == to ? head[idx] : to);
      dfs_hld(to, idx, times);
    }
    out[idx] = times;
  }

  void build() {
    dfs_sz(0, -1);
    int t = 0;
    dfs_hld(0, -1, t);
  }
  int la(int v, int k) {
    while(1) {
      int u = head[v];
      if(in[v] - k >= in[u]) return rev[in[v] - k];
      k -= in[v] - in[u] + 1;
      v = par[u];
    }
  }
  int lca(int u, int v) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) return u;
    }
  }
  template< typename T, typename Q, typename F >
  T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {
    T l = ti, r = ti;
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v), swap(l, r);
      if(head[u] == head[v]) break;
      l = f(q(in[head[v]], in[v] + 1), l);
    }
    return f(f(q(in[u] + edge, in[v] + 1), l), r);
//  return {f(q(in[u] + edge, in[v] + 1), l), r};
  }

  template< typename Q >
  void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }
};
//-------------------------------------------------------------------
struct segtree{
  ll M=1, INF = 1000000000000000000, INIT;
  vector<vll> dat;
  segtree(ll N, ll num){
    INIT = num;
    while(M<N) M*=2;
    dat.resize(M*2-1, vll{num, -1});
  }
  void update(vll x, ll k, ll l=0, ll r=-1){
    if(r==-1) r = M;
    k+=M-1;
    dat[k] = x;
    while(k>0) {
      k = (k-1)/2;
      dat[k] = max(dat[k*2+1], dat[k*2+2]);
    }
  }
  vll query(ll a, ll b=-1, ll k=0, ll l=0, ll r=-1){
    if(r==-1) r = M;
    if(b==-1) b = M;
    if(r<=a || b<=l) return vll{INIT, -INIT};
    if(a<=l && r<=b) return dat[k];
    vll A = query(a, b, k*2+1, l, (l+r)/2);
    vll B = query(a, b, k*2+2, (l+r)/2, r);
    return max(A, B);
  }
};
#include <queue>
int main(int argc, char const *argv[]) {
  ll a, b, c, n, m, q;std::cin >> n >> m >> q;
  Graph scc(n);
  re(i, m){
    scanf("%lld %lld", &a, &b);
    scc.add_edge(a-1, b-1);
  }
  vvl G = scc.build(n-1);
  HeavyLightDecomposition<vvl> hld(G);
  hld.build();
  segtree seg(300000, -1);
  vector<priority_queue<ll>> max_cost(400000);

  re(i, q){
    scanf("%lld %lld %lld", &a, &b, &c);
    if(a==1){
      b = scc.cmp[b-1];
      max_cost[b].push(c);
      ll MAX = max_cost[b].top();
      hld.add(b, b, [&](ll x, ll y){seg.update(vll{MAX, b}, x);});
    }else{
      b = scc.cmp[b-1];
      c = scc.cmp[c-1];
      vll q = hld.query(b, c, vll{-1, -1},
        [&](ll x, ll y){return seg.query(x, y);},
        [&](vll x, vll y){return max(x, y);}
      );
      printf("%lld\n", q[0]);
      if(q[0]!=-1) {
        if(!max_cost[q[1]].empty()) max_cost[q[1]].pop();
        ll MAX = (!max_cost[q[1]].empty()?max_cost[q[1]].top():-1);
        hld.add(q[1], q[1], [&](ll x, ll y){seg.update(vll{MAX, q[1]}, x);});
      }
    }
  }
  return 0;
}
0