1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=505; const ll inf=0x3f3f3f3f; int p[N],t[N]; ll s[N],g[N]; int m,idx[N],xdi[N]; ll f[(1<<10)+5]; bool nxt[(1<<10)+5][10]; struct node { ll mx; int mask; }nodes[N]; vector<int> g0[N]; typedef pair<int,int> pii; priority_queue<pii,vector<pii>,greater<pii>> q; bool contains(int x,int y) { return ((x^y)&y)==0; } void dfs(int u,ll mx,int mask) { if(t[u]==1) mx=max(mx,s[u]); if(t[u]==2) mask=mask|(1<<idx[u]); nodes[u]={mx,mask}; for(auto v:g0[u]) dfs(v,mx,mask); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n;cin>>n; for(int i=2;i<=n;i++) { cin>>p[i]>>t[i]>>s[i]>>g[i]; g0[p[i]].push_back(i); if(t[i]==2) { xdi[m]=i; idx[i]=m++; } } dfs(1,0,0); f[0]=1;q.push({0,1}); while(!q.empty()) { int u=q.top().second;q.pop(); if(s[u]>f[0]) continue; f[0]=min(f[0]+g[u],inf); for(auto v:g0[u]) if(t[v]==1) q.push({s[v],v}); else if(t[v]==2) nxt[0][idx[v]]=1; } for(int i=0;i<(1<<m);i++) for(int j=0;j<m;j++) { if(!nxt[i][j]) continue; ll cur=min(inf,g[xdi[j]]*f[i]); q.push({0,1}); while(!q.empty()) { int u=q.top().second;q.pop(); if(nodes[u].mx>cur) continue; if(t[u]==1&&(nodes[u].mx>f[i]||!contains(i,nodes[u].mask))) cur=min(inf,cur+g[u]); for(auto v:g0[u]) { if(t[v]==1) { if(contains(i|(1<<j),nodes[v].mask)) q.push({s[v],v}); } else { if((i|1<<j)>>idx[v]&1) q.push({s[v],v}); else nxt[(i|1<<j)][idx[v]]=1; } } } f[i|(1<<j)]=max(f[i|(1<<j)],cur); } bool flag=true; for(int i=1;i<=n;i++) if(s[i]>f[(1<<m)-1]) flag=false; cout<<(flag?"Yes":"No")<<'\n'; return 0; }
|