結果

問題 No.2325 Skill Tree
ユーザー shop_oneshop_one
提出日時 2023-05-28 14:27:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 424 ms / 3,000 ms
コード長 1,625 bytes
コンパイル時間 1,408 ms
コンパイル使用メモリ 123,820 KB
実行使用メモリ 19,168 KB
最終ジャッジ日時 2023-08-27 09:49:53
合計ジャッジ時間 16,753 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,504 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 206 ms
4,836 KB
testcase_08 AC 121 ms
8,860 KB
testcase_09 AC 228 ms
6,968 KB
testcase_10 AC 144 ms
12,748 KB
testcase_11 AC 215 ms
10,112 KB
testcase_12 AC 385 ms
17,372 KB
testcase_13 AC 389 ms
17,228 KB
testcase_14 AC 387 ms
17,212 KB
testcase_15 AC 393 ms
17,240 KB
testcase_16 AC 387 ms
17,180 KB
testcase_17 AC 387 ms
17,168 KB
testcase_18 AC 383 ms
17,172 KB
testcase_19 AC 385 ms
17,376 KB
testcase_20 AC 395 ms
17,124 KB
testcase_21 AC 389 ms
17,232 KB
testcase_22 AC 386 ms
17,172 KB
testcase_23 AC 385 ms
17,240 KB
testcase_24 AC 382 ms
17,172 KB
testcase_25 AC 384 ms
17,176 KB
testcase_26 AC 388 ms
17,164 KB
testcase_27 AC 418 ms
18,964 KB
testcase_28 AC 415 ms
19,168 KB
testcase_29 AC 408 ms
18,896 KB
testcase_30 AC 413 ms
18,744 KB
testcase_31 AC 409 ms
18,968 KB
testcase_32 AC 390 ms
18,740 KB
testcase_33 AC 392 ms
18,904 KB
testcase_34 AC 392 ms
18,884 KB
testcase_35 AC 411 ms
18,620 KB
testcase_36 AC 403 ms
18,380 KB
testcase_37 AC 424 ms
18,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = int;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
ll calc(int i, vector<ll> &dp, vector<ll> &a, vector<vector<ll>> &G, vector<ll> &arr) {
  if (dp[i] != -1) return dp[i];
  if (arr[i]) {
    return dp[i] = -2;
  }
  arr[i] = 1;
  ll tmp = a[i];
  for(auto nv: G[i]) {
    ll res = calc(nv, dp, a, G, arr);
    if (res == -2) {
      tmp = res;
      break;
    }
    tmp = max(res, tmp);
  }
  arr[i] = 0;
  return dp[i] = tmp;
}
signed main(){
  init_io();
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<vector<ll>> G(n, vector<ll>());
  vector<ll> found(n, false);
  vector<ll> dp(n, -1);
  vector<ll> arr(n, 0);
  a[0] = 0;
  dp[0] = 0;
  for (int i=1;i<n;i++) {
    ll x, y;
    cin >> y >> x;
    x--;
    G[i].push_back(x);
    a[i] = y;
  }
  vector<ll> copy;
  map<ll, ll> mp;
  copy.push_back(0);
  for (int i=1;i<n;i++) {
    int tmp = calc(i, dp, a, G, arr);
    if (tmp != -2) {
      copy.push_back(tmp);
    }
  }
  sort(copy.begin(), copy.end());
  for(int i=0;i<copy.size();i++) {
    mp[copy[i]] = i+1;
  }
  int q;
  cin >> q;
  for (int i=0;i<q;i++) {
    int x,y;
    cin >> x >> y;
    if (x == 1) {
      auto itr = mp.upper_bound(y);
      itr--;
      cout << itr->second << endl;
    }
    if (x == 2) {
      if (dp[y-1] == -2) dp[y-1] = -1;
      cout << dp[y-1] << endl;
    }
  }
}
0