結果

問題 No.763 Noelちゃんと木遊び
ユーザー mkawa2mkawa2
提出日時 2020-08-27 13:54:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,551 bytes
コンパイル時間 1,674 ms
コンパイル使用メモリ 166,304 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-04-25 06:31:17
合計ジャッジ時間 3,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
10,112 KB
testcase_01 AC 29 ms
7,552 KB
testcase_02 AC 75 ms
9,344 KB
testcase_03 AC 47 ms
8,064 KB
testcase_04 AC 34 ms
7,680 KB
testcase_05 AC 53 ms
7,936 KB
testcase_06 AC 92 ms
9,984 KB
testcase_07 AC 86 ms
9,856 KB
testcase_08 AC 49 ms
8,192 KB
testcase_09 AC 32 ms
7,424 KB
testcase_10 AC 15 ms
6,400 KB
testcase_11 AC 88 ms
10,112 KB
testcase_12 AC 80 ms
9,728 KB
testcase_13 AC 75 ms
9,600 KB
testcase_14 AC 67 ms
9,344 KB
testcase_15 AC 45 ms
8,064 KB
testcase_16 AC 11 ms
6,272 KB
testcase_17 AC 49 ms
8,064 KB
testcase_18 AC 90 ms
9,856 KB
testcase_19 AC 86 ms
9,600 KB
testcase_20 AC 85 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const ll inf = 1e9;

int tree[400005];
vi to[100005];

void add(int i,int a){
  int u=i+tree[0];
  tree[u]+=a;
  while(u>1){
    u/=2;
    tree[u]=min(tree[u*2],tree[u*2+1]);
  }
}

void update(int i,int a){
  int u=i+tree[0];
  tree[u]=a;
  while(u>1){
    u/=2;
    tree[u]=min(tree[u*2],tree[u*2+1]);
  }
}

int mini(){
  int u=1;
  while(u<tree[0]){
    if(tree[1]==tree[u*2]) u=u*2;
    else u=u*2+1;
  }
  return u-tree[0];
}

int main() {
  ll n;
  cin>>n;
  tree[0]=1;
  while(tree[0]<n) tree[0]*=2;
  for(int u=tree[0]+n;u<tree[0]*2;u++) tree[u]=inf;
  rep(i,n-1){
    int u,v;
    cin>>u>>v;
    u--;v--;
    to[u].push_back(v);
    to[v].push_back(u);
    tree[u+tree[0]]++;
    tree[v+tree[0]]++;
  }
  // rep(i,tree[0]) cout<<tree[i+tree[0]]<<" ";
  // cout<<endl;

  for(int u=tree[0]-1;u>=1;u--) tree[u]=min(tree[u*2],tree[u*2+1]);
  int ans=1;
  while(tree[1]==1){
    int d=0;
    int i=mini();
    for(int j:to[i]) if(tree[j+tree[0]]!=inf){
      d=j;
      break;
    }
    i=d;
    ans+=tree[i+tree[0]]-1;
    // cout<<i<<endl;
    update(i,inf);
    for(int j:to[i]){ 
      int u=j+tree[0];
      if(tree[u]==inf) continue;
      if(tree[u]==1) update(j,inf);
      else add(j,-1);
      }
    // rep(i,n) cout<<tree[i+tree[0]]<<" ";
    // cout<<endl;
  }
  cout<<ans<<endl;
  return 0;
}
0