結果

問題 No.2325 Skill Tree
ユーザー shop_one
提出日時 2023-05-28 14:27:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 411 ms / 3,000 ms
コード長 1,625 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 119,396 KB
最終ジャッジ日時 2025-02-13 11:29:42
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0