結果
| 問題 | No.2325 Skill Tree |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-28 14:54:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 582 ms / 3,000 ms |
| コード長 | 1,155 bytes |
| コンパイル時間 | 2,989 ms |
| コンパイル使用メモリ | 167,632 KB |
| 最終ジャッジ日時 | 2025-02-13 12:13:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <cstring>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < int(n);i++)
const ll INF = 1LL << 60;
int main(){
int n;
cin >> n;
vector<ll> l(n);
vector<int> a(n);
vector<ll> d(n,INF);
vector<vector<int>> g(n);
d[0] = 0;
for (int i = 1; i < n;i++){
cin >> l[i] >> a[i];
g[a[i]-1].push_back(i);
}
queue<int> q;
q.push(0);
while(!q.empty()){
auto x = q.front();
//cout << x << endl;
q.pop();
for (auto nx:g[x]){
d[nx] = d[x] + max(l[nx]-d[x],0LL);
q.push(nx);
}
}
//cout << "ok" << endl;
auto d2 = d;
sort(d2.begin(),d2.end());
int t;
cin >> t;
while(t--){
int type,x;
cin >> type >> x;
if (type == 1){
cout << int(lower_bound(d2.begin(),d2.end(),x+1)-d2.begin()) << endl;
}
else{
if (d[x-1] == INF){
cout << -1 << endl;
}
else cout << d[x-1] << endl;
}
}
return 0;
}