結果

問題 No.399 動的な領主
ユーザー ei1333333ei1333333
提出日時 2016-07-16 14:13:57
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,945 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 179,056 KB
実行使用メモリ 23,840 KB
最終ジャッジ日時 2024-04-25 08:56:05
合計ジャッジ時間 7,231 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘int BinaryIndexedTree::add(int, int)’:
main.cpp:11:3: warning: no return statement in function returning non-void [-Wreturn-type]
   11 |   }
      |   ^
main.cpp: In function ‘int main()’:
main.cpp:168:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  168 |   scanf("%d", &N);
      |   ~~~~~^~~~~~~~~~
main.cpp:172:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  172 |     scanf("%d %d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:180:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  180 |   scanf("%d", &Q);
      |   ~~~~~^~~~~~~~~~
main.cpp:183:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  183 |     scanf("%d %d", &A, &B);
      |     ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

struct BinaryIndexedTree
{
  vector< int > data;
  BinaryIndexedTree(int sz): data(++sz, 0) {};
  int add(int k, int x)
  {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
  int query(int k)
  {
    if(k < 0) return(0);
    int ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return(ret);
  }
};

struct CentroidPathDecomposition
{
  struct Centroid
  {
    int ParIndex, ParDepth, Deep;
    vector< int > node;
    inline int size()
    {
      return(node.size());
    }
    inline int &operator[](int k)
    {
      return(node[k]);
    }
    inline pair< int, int > Up()
    {
      return(make_pair(ParIndex, ParDepth));
    }
  };
  
  vector< vector< int > > graph;
  vector< int > SubTreeSize, NextPath;
  vector< int > TreeIndex, TreeDepth;
  vector< Centroid > Centroids;
    
  void BuildSubTreeSize()
  {
    stack< pair< int, int > > s;
    s.push({0, -1});
    while(!s.empty()) {
      auto p = s.top(); s.pop();
      if(~SubTreeSize[p.first]) {
        NextPath[p.first] = -1;
        for(auto& to : graph[p.first]) {
          if(p.second == to) continue;
          SubTreeSize[p.first] += SubTreeSize[to];
          if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
            NextPath[p.first] = to;
          }
        }
      } else {
        s.push(p);
        SubTreeSize[p.first] = 1;
        for(auto& to : graph[p.first]) {
          if(p.second != to) s.push({to, p.first});
        }
      }
    }
  }
  void BuildPath()
  {
    stack< pair< int, int > > s;
    Centroids.push_back((Centroid){-1, -1, 0});
    s.push({0, -1});
    TreeIndex[0] = 0;
    while(!s.empty()) {
      auto p = s.top(); s.pop();
      TreeDepth[p.first] = Centroids[TreeIndex[p.first]].size();
      for(auto& to : graph[p.first]) {
        if(p.second != to) {
          if(to == NextPath[p.first]) { // Centroid-Path
            TreeIndex[to] = TreeIndex[p.first];
          } else {                  // Not Centroid-Path
            TreeIndex[to] = Centroids.size();
            Centroids.push_back((Centroid){TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1});
          }
          s.push({to, p.first});
        }
      }
      Centroids[TreeIndex[p.first]].node.push_back(p.first);
    }
  }
  void AddEdge(int x, int y)
  {
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  void Build()
  {
    BuildSubTreeSize();
    BuildPath();
  }
    
  inline int size()
  {
    return(Centroids.size());
  }
  inline pair< int, int > Information(int idx)
  {
    return(make_pair(TreeIndex[idx], TreeDepth[idx]));
  }
  inline Centroid &operator[](int k)
  {
    return(Centroids[k]);
  }
  inline int LCA(int a, int b) // これを流用する
  {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while(TreeIdxA != TreeIdxB) {
      if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
    return(Centroids[TreeIdxA][TreeDepthA]);
  }
  
  CentroidPathDecomposition(int SZ)
  {
    graph.resize(SZ);
    SubTreeSize.assign(SZ, -1);
    NextPath.resize(SZ);
    TreeIndex.resize(SZ);
    TreeDepth.resize(SZ);
  }

  inline void AddPath(int a, int b);
};

vector< BinaryIndexedTree > bits;
inline void CentroidPathDecomposition::AddPath(int a, int b)
{
  int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
  tie(TreeIdxA, TreeDepthA) = Information(a);
  tie(TreeIdxB, TreeDepthB) = Information(b);
  while(TreeIdxA != TreeIdxB) {
    if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
      bits[TreeIdxA].add(0, 1);
      bits[TreeIdxA].add(TreeDepthA + 1, -1);
      tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
    } else {
      bits[TreeIdxB].add(0, 1);
      bits[TreeIdxB].add(TreeDepthB + 1, -1);
      tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
    }
  }
  if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
  bits[TreeIdxA].add(TreeDepthA, 1);
  bits[TreeIdxB].add(TreeDepthB + 1, -1);
}
int main()
{
  int N, Q;
  scanf("%d", &N);
  CentroidPathDecomposition tree(N);
  for(int i = 0; i < N - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    tree.AddEdge(--u, --v);
  }
  tree.Build();
  for(int i = 0; i < tree.size(); i++) {
    bits.emplace_back(BinaryIndexedTree(tree[i].size() + 1));
  }
  
  scanf("%d", &Q);
  while(Q--) {
    int A, B;
    scanf("%d %d", &A, &B);
    tree.AddPath(--A, --B);
  }

  long long ret = 0;
  for(int i = 0; i < tree.size(); i++) {
    for(int j = 0; j < tree[i].size(); j++) {
      int sum = bits[i].query(j);
      ret += 1LL * sum * (sum + 1) / 2;
    }
  }
  cout << ret << endl;
}
0