結果

問題 No.399 動的な領主
ユーザー rickythetarickytheta
提出日時 2016-07-17 13:56:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 2,930 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 171,032 KB
実行使用メモリ 35,200 KB
最終ジャッジ日時 2024-04-25 08:59:55
合計ジャッジ時間 5,347 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,520 KB
testcase_01 AC 7 ms
11,392 KB
testcase_02 AC 7 ms
11,392 KB
testcase_03 AC 7 ms
11,520 KB
testcase_04 AC 7 ms
11,520 KB
testcase_05 AC 22 ms
12,544 KB
testcase_06 AC 297 ms
23,168 KB
testcase_07 AC 293 ms
23,040 KB
testcase_08 AC 267 ms
23,424 KB
testcase_09 AC 261 ms
23,040 KB
testcase_10 AC 9 ms
11,520 KB
testcase_11 AC 18 ms
12,288 KB
testcase_12 AC 177 ms
20,608 KB
testcase_13 AC 169 ms
20,736 KB
testcase_14 AC 130 ms
35,200 KB
testcase_15 AC 128 ms
35,200 KB
testcase_16 AC 139 ms
28,928 KB
testcase_17 AC 283 ms
22,912 KB
testcase_18 AC 279 ms
23,040 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:70:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |   scanf("%d",&n);
      |   ~~~~~^~~~~~~~~
main.cpp:73:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |     scanf("%d%d",&a,&b);
      |     ~~~~~^~~~~~~~~~~~~~
main.cpp:106:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  106 |   scanf("%d",&m);
      |   ~~~~~^~~~~~~~~
main.cpp:109:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  109 |     scanf("%d%d",&a,&b);
      |     ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

// yukicoder 386
// abc014 D
struct node{
  int id,anc,dep;
  vi kanc;
};
bool operator==(node l,node r){
  return l.id==r.id&&l.anc==r.anc&&l.dep==r.dep;
}
bool operator!=(node l,node r){
  return !(l==r);
}

int n;
vi g[125252];
int u[125252];
node nodes[125252];
bool used[125252];
ll imos[125252];

ll dfs(int p){
  ll ret = 0;
  used[p] = true;
  REP(i,g[p].size()){
    int to = g[p][i];
    if(used[to])continue;
    ret += dfs(to);
    imos[p] += imos[to];
  }
  ret += imos[p]*(imos[p]+1)/2;
  return ret;
}

int main(){
  // lca
  // doubling
  scanf("%d",&n);
  REP(i,n-1){
    int a,b;
    scanf("%d%d",&a,&b);
    --a;--b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<int> q;
  q.push(0);
  used[0] = true;
  nodes[0].anc = -1;
  nodes[0].dep = 0;
  nodes[0].kanc.push_back(-1);
  while(!q.empty()){
    int i = q.front(); q.pop();
    REP(j,g[i].size()){
      int k = g[i][j];
      if(used[k])continue;
      used[k] = true;
      q.push(k);
      nodes[k].id = k;
      nodes[k].anc = i;
      nodes[k].dep = nodes[i].dep+1;
      nodes[k].kanc.push_back(i);
      int iter = 0;
      while(nodes[k].kanc[iter]!=-1){
        int id = nodes[k].kanc[iter];
        if(nodes[id].kanc.size() <= iter)nodes[k].kanc.push_back(-1);
        else nodes[k].kanc.push_back(nodes[id].kanc[iter]);
        ++iter;
      }
    }
  }
  // query
  int m;
  scanf("%d",&m);
  while(m--){
    int a,b;
    scanf("%d%d",&a,&b);
    --a;--b;
    node l=nodes[a],r=nodes[b];
    if(l.dep > r.dep)swap(l,r);
    int sub = r.dep - l.dep;
    int k = 0;
    while(sub){
      if(sub&1==1) r = nodes[r.kanc[k]];
      ++k;
      sub>>=1;
    }
    while(r.anc!=l.anc && r!=nodes[l.anc] && l!=nodes[r.anc]){
      k = 0;
      while(r.kanc[k]!=l.kanc[k] && r.kanc[k]!=-1)++k;
      --k;
      r = nodes[r.kanc[k]];
      l = nodes[l.kanc[k]];
    }
    if(r!=l){
      if(r.anc==-1)r=nodes[l.anc];
      else r=nodes[r.anc];
    }
    imos[a]++;
    imos[b]++;
    imos[r.id]--;
    if(r.anc!=-1)
      imos[r.anc]--;
  }
  fill(used,used+n,false);
  printf("%lld\n",dfs(0));
  return 0;
}
0