結果

問題 No.399 動的な領主
ユーザー tempura_pptempura_pp
提出日時 2018-08-13 23:30:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 2,225 bytes
コンパイル時間 2,948 ms
コンパイル使用メモリ 104,928 KB
実行使用メモリ 26,708 KB
最終ジャッジ日時 2023-10-24 16:52:59
合計ジャッジ時間 5,832 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,140 KB
testcase_01 AC 2 ms
6,140 KB
testcase_02 AC 3 ms
6,152 KB
testcase_03 AC 3 ms
6,152 KB
testcase_04 AC 4 ms
6,272 KB
testcase_05 AC 18 ms
7,484 KB
testcase_06 AC 242 ms
22,220 KB
testcase_07 AC 241 ms
22,220 KB
testcase_08 AC 233 ms
22,220 KB
testcase_09 AC 222 ms
22,220 KB
testcase_10 AC 4 ms
6,268 KB
testcase_11 AC 16 ms
7,540 KB
testcase_12 AC 177 ms
23,276 KB
testcase_13 AC 175 ms
23,276 KB
testcase_14 AC 142 ms
26,708 KB
testcase_15 AC 155 ms
26,708 KB
testcase_16 AC 164 ms
24,336 KB
testcase_17 AC 220 ms
22,220 KB
testcase_18 AC 225 ms
22,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
struct LCA{
   int n,h;
   vector<vector<int>> v,par;
   vector<int> depth;
   LCA(int sz):n(sz),v(n),depth(n){
       h=1;
       while((1<<h)<n)h++;
       par.assign(h,vector<int>(n,-1));
   }

   void add_edge(int x,int y){
       v[x].push_back(y);
       v[y].push_back(x);
   }

   void dfs(int x,int p,int d){
       par[0][x]=p;
       depth[x]=d;
       for(auto to:v[x])if(to!=p)dfs(to,x,d+1);
   }

   void build(){
       dfs(0,-1,0);
       rep(i,h-1){
           rep(j,n){
               if(par[i][j]<0)par[i+1][j]=-1;
               else par[i+1][j]=par[i][par[i][j]];
           }
       }
   }

   int lca(int u,int v){
       if(depth[u]>depth[v])swap(u,v);
       rep(i,h)if((depth[v]-depth[u])>>i &1)v=par[i][v];
       if(u==v)return u;
       for(int i=h-1;i>=0;i--){
           if(par[i][u]!=par[i][v]){
               u=par[i][u];
               v=par[i][v];
           }
       }
       return par[0][u];
   }

   int dist(int u,int v){
       return depth[u]+depth[v]-2*depth[lca(u,v)];
   }
};
vector<int> v[101010];
ll a[101010];

void dfs(int x,int p){
    for(auto to:v[x]){
        if(to==p)continue;
        dfs(to,x);
    }
    if(p!=-1)a[p]+=a[x];
}
int main(){
    int n;
    cin>>n;
    LCA lca(n);
    rep(i,n-1){
        int x,y;
        cin>>x>>y;
        --x;--y;
        v[x].push_back(y);
        v[y].push_back(x);
        lca.add_edge(x, y);
    }
    lca.build();
    int q;
    cin>>q;
    rep(i,q){
        int x,y;
        cin>>x>>y;
        --x;--y;
        int z=lca.lca(x, y);
        int zz=lca.par[0][z];
        ++a[x];
        ++a[y];
        --a[zz];
        --a[z];
    }
    dfs(0,-1);
    ll ans=0;
    rep(i,n)ans+=a[i]*(a[i]+1)/2;
    cout<<ans<<endl;
   return 0;
}
0