結果

問題 No.399 動的な領主
ユーザー n_vipn_vip
提出日時 2016-07-15 22:57:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 3,209 bytes
コンパイル時間 1,692 ms
コンパイル使用メモリ 110,124 KB
実行使用メモリ 29,988 KB
最終ジャッジ日時 2023-08-07 14:58:20
合計ジャッジ時間 4,211 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 12 ms
4,856 KB
testcase_06 AC 175 ms
21,896 KB
testcase_07 AC 178 ms
21,888 KB
testcase_08 AC 167 ms
22,064 KB
testcase_09 AC 168 ms
22,060 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 10 ms
5,092 KB
testcase_12 AC 122 ms
22,216 KB
testcase_13 AC 113 ms
22,176 KB
testcase_14 AC 90 ms
29,964 KB
testcase_15 AC 100 ms
29,988 KB
testcase_16 AC 106 ms
25,768 KB
testcase_17 AC 187 ms
21,944 KB
testcase_18 AC 188 ms
21,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;

//doubling
typedef vv<int> Graph;
class Lca{
public:
  vector<int> dep,cld;
  vv<int> par;
  const int LOGN=20;
  void dfs(const Graph &g,int root){
    int n=g.size();
    vector<int> vst(n);
    stack<int> st; st.push(root);
    while(!st.empty()){
      int v=st.top(); st.pop();
      if(vst[v]==1){
	vst[v]=2;
	rep(i,g[v].size())if(g[v][i]!=par[v][0])
	  cld[v]+=cld[g[v][i]]+1;
      }else if(vst[v]==0){
	st.push(v);
	vst[v]=1;
	rep(i,g[v].size())if(!vst[g[v][i]]){
	  dep[g[v][i]]=dep[v]+1;
	  par[g[v][i]][0]=v;
	  st.push(g[v][i]);
	}
      }
    }
  }
  
  Lca(const Graph &g){
    int n=g.size();
    par.resize(n,vector<int>(LOGN,-1));
    dep.resize(n); cld.resize(n); dfs(g,0);
    rep(i,LOGN-1)rep(v,n)
      par[v][i+1]=par[v][i]<0?-1:par[par[v][i]][i];
  }
  
  int anc(int v,int n){
    rep(i,LOGN)if(n>>i&1)
      if((v=par[v][i])<0)break;
    return v;
  }
  
  int LCA(int v,int w){
    if(dep[v]>dep[w])
      swap(v,w);
    w=anc(w,dep[w]-dep[v]);
    rrep(i,LOGN)
      if(par[v][i]!=par[w][i]){
	v=par[v][i];
	w=par[w][i];
      }
    return v==w?v:par[v][0];
  }
  int dist(int v,int w){return dep[v]+dep[w]-2*dep[LCA(v,w)];}
};

ll ret=0;

ll dfs(vv<int> &g,int v,int p,vector<ll> &val){
  ll re=val[v];
  for(int w:g[v])
    if(w!=p) re+=dfs(g,w,v,val);
  ret+=re*(re+1)/2;
  //cout<<v<<":"<<re<<endl;
  return re;
}

int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int n;
  cin>>n;
  vv<int> g(n);
  int x,y;
  rep(i,n-1){
    cin>>x>>y; --x; --y;
    g[x].pb(y);
    g[y].pb(x);
  }
  Lca h(g);
  int q;
  cin>>q;
  vector<ll> v(n);
  while(q--){
    cin>>x>>y; --x; --y;
    int p=h.LCA(x,y);
    --v[p];
    if(p){
      --v[h.par[p][0]];
    }
    ++v[x];
    ++v[y];
  }
  dfs(g,0,-1,v);
  cout<<ret<<endl;
  return 0;
}
0