結果

問題 No.399 動的な領主
ユーザー CleyLCleyL
提出日時 2024-02-02 14:41:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,925 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 122,456 KB
実行使用メモリ 46,152 KB
最終ジャッジ日時 2024-02-02 14:41:49
合計ジャッジ時間 6,268 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 200 ms
46,152 KB
testcase_15 AC 232 ms
46,152 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//根付き木にしてLCAしてimos
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
using namespace std;


template< typename T, typename F>
struct SegmentTree{
  private:
    int n;
    vector<T> node;

    F f;
    T initer;

  public:
    SegmentTree(int n_, F f, T initer) : f(f),initer(initer) {
      n = 1;
      while(n < n_)n*=2;
      node.resize(2*n-1, initer);
    }

    SegmentTree(int n_, F f,T initer, vector<T> x) : f(f),initer(initer) {
      n = 1;
      while(n < n_)n*=2;
      node.resize(2*n-1, initer);
      set(x);
    }

    void set(vector<T> x){
      for(int i = 0; (int)x.size() > i; i++){
        update(i,x[i]);
      }
    }

    void update(int x, T val){
      x += (n-1);

      node[x] = val;

      while(x > 0){
        x = (x-1)/2;
        node[x] = f(node[2*x+1],node[2*x+2]);
      }
    }

    void add(int x, T val){
      x += (n-1);
      
      node[x] += val;
      while(x > 0){
        x = (x-1)/2;
        node[x] = f(node[2*x+1],node[2*x+2]);
      }
    }

    T getf(int a,int b, int k=0, int l=0, int r=-1){
      
      if(r < 0){
        r = n-1;
      }
      if(r < a || b < l){
        return initer;
      }
      if(a <= l && r <= b){
        return node[k];
      }

      T vl = getf(a,b,2*k+1,l,(l+r)/2);
      T vr = getf(a,b,2*k+2,(l+r)/2+1,r);
      return f(vl,vr);
    }

};

template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(int N, const F &f, T initer){
  return SegmentTree<T,F>{N,f,initer};
}

template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(int N, const F &f, T initer, vector<T> x){
  return SegmentTree<T,F>{N,f,initer,x};
}

struct EulerTourForLCA {
  int n;
  vector<vector<int>> edge;
  vector<pair<int, int>> timeline;
  vector<pair<int,int>> stump;
  vector<int> parent;
  EulerTourForLCA(int n, vector<vector<int>> edge):
    n(n), edge(edge), timeline(2*n, {-1, 0}), stump(n, {-1,-1}), parent(n, 0), lock(n, false) {}

  void build(int top){
    timer = 0;
    lock.assign(n, false);
    lock[top] = true;
    dfs(top, 0);
  }
  const pair<int,int> operator[](int index){
    return stump[index];
  }
private:
  vector<bool> lock;
  int timer = 0;
  void dfs(int vertex, int depth){
    stump[vertex].first = timer;
    timeline[timer].first = vertex;
    timeline[timer].second = depth;
    timer++;
    for(int i = 0; (int)edge[vertex].size() > i; i++){
      if(!lock[edge[vertex][i]]){
        lock[edge[vertex][i]] = true;
        parent[edge[vertex][i]] = vertex;
        dfs(edge[vertex][i], depth+1);
      }
    }
    stump[vertex].second = timer;
    timeline[timer].first = parent[vertex];
    timeline[timer].second = depth-1;
    timer++;
  }
};


struct LowestCommonAncestor {
  int n;
  vector<vector<int>> edge;
  EulerTourForLCA et;
  function<pair<int,int>(pair<int,int>, pair<int,int>)> RMQ = [](pair<int,int> a, pair<int,int> b){
    if(a.second < b.second)return a;
    return b;
  };
  //function<const pair<int,int>> RMQ = [](pair<int,int> a, pair<int,int> b){
  //};
  SegmentTree<pair<int,int>, decltype(RMQ)> st;

  LowestCommonAncestor(int n, vector<vector<int>> edge):n(n), edge(edge), et(n, edge), st(2*n, RMQ, {n+1, n+1}){
    et.build(0);
    st.set(et.timeline);
  }

  int compute(int l, int r){
    auto ls = et[l].first;
    auto rs = et[r].first;
    if(ls > rs)swap(ls, rs);
    auto mn = st.getf(ls, rs);
    return mn.first;
  }
};
using LCA = LowestCommonAncestor;

int main(){
  int n;cin>>n;
  vector<vector<int>> A(n);
  for(int i = 0; n-1 > i; i++){
    int u,v;cin>>u>>v;
    u--;v--;
    A[u].push_back(v);
    A[v].push_back(u);
  }
  LCA lca(n, A);
  vector<pair<int,int>> B(n, {0, 0});
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    int a,b;cin>>a>>b;a--;b--;
    auto z = lca.compute(a,b);
    B[z].first++;
    B[a].second++;
    B[b].second++;
  }
  // get leaf
  queue<int> X;
  vector<int> vis(n, 0);
  X.push(0);
  vis[0] = -1;
  vector<int> leaf;
  while(X.size()){
    auto x = X.front(); X.pop();
    bool found = false;
    for(int i = 0; A[x].size() > i; i++){
      if(vis[A[x][i]])continue;
      vis[A[x][i]] = -1;
      X.push(A[x][i]);
      found = true;
    }
    if(!found)leaf.push_back(x);
  }

  queue<int> S;
  vector<long long> count(n, 0);
  for(int i = 0; leaf.size() > i; i++){
    S.push(leaf[i]);
    count[leaf[i]] = B[leaf[i]].second;
    vis[leaf[i]] = 2;
  }
  while(S.size()){
    auto x = S.front(); S.pop();
    if(vis[x] != 2){
      count[x] = count[x] + B[x].second - B[x].first;
    }
    vis[x] = 1;
    for(int i = 0; A[x].size() > i; i++){
      if(vis[A[x][i]] == 1)continue;
      if(vis[A[x][i]] == -1){
        S.push(A[x][i]);
      }
      vis[A[x][i]] = 0;
      count[A[x][i]] += count[x] - B[x].first;
    }
  }
  long long ans = 0;
  for(int i = 0; n > i; i++){
    ans += count[i]*(count[i]+1)/2;
  }
  cout << ans << endl;
}
0