結果

問題 No.2325 Skill Tree
ユーザー シアンシアン
提出日時 2023-05-28 14:41:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,806 ms / 3,000 ms
コード長 1,483 bytes
コンパイル時間 4,641 ms
コンパイル使用メモリ 277,040 KB
実行使用メモリ 13,820 KB
最終ジャッジ日時 2023-08-27 10:23:17
合計ジャッジ時間 34,513 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 264 ms
4,736 KB
testcase_08 AC 171 ms
7,408 KB
testcase_09 AC 293 ms
6,220 KB
testcase_10 AC 216 ms
9,920 KB
testcase_11 AC 297 ms
8,132 KB
testcase_12 AC 536 ms
12,840 KB
testcase_13 AC 540 ms
12,832 KB
testcase_14 AC 542 ms
12,800 KB
testcase_15 AC 543 ms
12,848 KB
testcase_16 AC 536 ms
12,796 KB
testcase_17 AC 519 ms
12,888 KB
testcase_18 AC 526 ms
12,832 KB
testcase_19 AC 523 ms
12,844 KB
testcase_20 AC 522 ms
12,796 KB
testcase_21 AC 526 ms
12,800 KB
testcase_22 AC 543 ms
12,892 KB
testcase_23 AC 546 ms
12,796 KB
testcase_24 AC 542 ms
12,788 KB
testcase_25 AC 539 ms
12,788 KB
testcase_26 AC 543 ms
12,752 KB
testcase_27 AC 1,780 ms
13,676 KB
testcase_28 AC 1,728 ms
13,820 KB
testcase_29 AC 1,611 ms
13,544 KB
testcase_30 AC 1,540 ms
13,640 KB
testcase_31 AC 1,612 ms
13,680 KB
testcase_32 AC 559 ms
13,116 KB
testcase_33 AC 564 ms
13,568 KB
testcase_34 AC 561 ms
13,764 KB
testcase_35 AC 2,077 ms
13,316 KB
testcase_36 AC 1,814 ms
12,748 KB
testcase_37 AC 2,806 ms
13,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;
const ll MOD = 998244353;

// DFS
vector<bool> seen;
vector<int> level;
map<int,int> mp;
void dfs(const vector<vector<pair<int,int>>>& G, int V, int ma) {
  seen.at(V) = true;
  level.at(V) = ma;
  mp[ma]++;

  for (auto next : G.at(V)) {
    if (seen.at(next.first)) continue;
    dfs(G, next.first, max(ma,next.second));
  }
}

// 二分探索
int binarySearch(vector<pair<int,int>> Sorted,int K) {
  int ok = -1;
  int ng = Sorted.size();
  while(abs(ok-ng)>1) {
    int mid = (ok+ng)/2;
    bool flg = Sorted.at(mid).first<=K;
    if(flg) ok=mid;
    else ng=mid;
  }
  return ok;
}


int main() {
  int n;cin>>n;
  seen.resize(n);
  level.resize(n);
  for(int i=0;i<n;i++) level.at(i)=-2;
  level.at(0)=0;
  vector t(n, vector<pair<int,int>>());
  for(int i=1;i<n;i++) {
    int l,a;cin>>l>>a;
    a--;l--;
    t.at(a).push_back(make_pair(i,l));
  }

  dfs(t,0,0);

  vector<pair<int,int>> kind;
  for(auto ele : mp) {
    kind.push_back(ele);
  }
  sort(kind.begin(),kind.end());
  for(int i=1;i<kind.size();i++) {
    kind.at(i).second += kind.at(i-1).second;
  }

  int Q;cin>>Q;
  while(Q--) {
    int q,x;cin>>q>>x;x--;
    if(q==1) {
      int idx = binarySearch(kind, x);
      cout << kind.at(idx).second << endl;
    } else {
      cout << level.at(x)+1 << endl;
    }
  }

  exit(0);
}
0