QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788524 | #9580. 插排串联 | abovecloud# | WA | 1ms | 3860kb | C++23 | 1.8kb | 2024-11-27 17:18:40 | 2024-11-27 17:18:40 |
Judging History
answer
/*** WORK : 省赛\2024大连省赛\c.cpp ***/
/*** TIME : 2024/11/27 16:37 ***/
#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define pb push_back
#define se second
#define fi first
#define int long long
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int MOD = (int) 1e9 + 7;
const int INF = LLONG_MAX;
const int MAXN = 1e5 + 5;
vi e[MAXN];
void solve() {
int n;
cin >> n;
vi w(n+1);
vi root;
for(int i=1;i<=n;i++){
int fa;
cin >> fa >> w[i];
if(fa==0)root.pb(i);
e[fa].push_back(i);
e[i].push_back(fa);
}
// for(int x:e[1]){cout << x <<" ";cout << endl;}
vi lead(n+1,0),dp(n+1,0);
multiset<int> use;
int sum = 0;
for(int i=1;i<=n;i++){
if(e[i].size()==1){
lead[i] = 1;
sum += w[i];
dp[i] = w[i];
}else{
use.insert(w[i]);
}
}
use.insert(INF);
auto dfs = [&](auto&& dfs,int u,int fa)->void{
if(lead[u]==1)return;
for(int v:e[u]){
if(v==fa)continue;
dfs(dfs,v,u);
dp[u] += dp[v];
}
if(u == 0){
if(dp[u] > 2200){
cout << "NO" << endl;
exit(0);
}
return;
}
auto f = *use.lower_bound(dp[u]);
// cout << u << " " << dp[u] << " " << f << endl;
if(f == INF){
cout << "NO" << endl;
exit(0);
}
use.erase(f);
};
dfs(dfs,root[0],0);
cout << "YES" << endl;
}
int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(12);
int T = 1;
T=1;
while (T--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3572kb
input:
5 0 500 1 700 1 400 2 100 2 200
output:
YES
result:
ok single line: 'YES'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
5 0 500 1 700 1 400 2 100 2 300
output:
NO
result:
ok single line: 'NO'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
4 0 1000 1 800 1 500 2 300
output:
YES
result:
ok single line: 'YES'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3608kb
input:
3 0 1000000000 0 1000000000 0 147483647
output:
YES
result:
wrong answer 1st lines differ - expected: 'NO', found: 'YES'