Description Description "seal the launch of the ceremony. Please release the Nescafe soul Pearl !" With the Holy Lord applepi starting, the holy sword shield law rainbow and the magic wand guard Freda place the Nescafe soul beads on the seal table. The seal stand is a tree structure, and the place where the soul beads are placed is the root node (numbered 0 ). There are n other nodes (numbered 1 ~ N) The seal stone is placed on it. The seal stone numbered I needs to obtain the EI energy from the soul beads. Energy can only be transmitted along the side of the tree from the soul beads to the seal stone. Each side has a maximum of Wi, and the soul beads have infinite energy. As the preparation before the seal starts, please find out how many sealing stones can meet the energy requirements at most?
Note: energy can be transmitted to the next node without meeting its needs. Each side can only transmit energy once. The input format is inputformat. The first line is an integer N, indicating the number of nodes other than the root node.
Next n rows, line I + 1 contains three integers: fi, EI, and WI, it indicates the energy requirements of the sealing stone on the parent node of node I, the edge Connecting Node I and Fi, and the maximum amount of energy that can be transferred. Question:
I don't feel any more love... I thought for a long time, including DP, network stream, and so on. I finally got a backpack of N ^ 4, then I gave up my treatment and worshipped my questions...
It turned out to be greedy...
Speaker:
Each time you select the node with the smallest energy requirement, scan its edge capacity on the path of the root node to see if it can meet the requirements, if it can meet the requirements, the capacity of the edge on the path to the root node will be equal to its requirements.
If you cannot grasp the correctness of greed, you can also write tree motion rules (Multi-tree, backpack transfer), but obviously the programming complexity has increased to a level.
Correctness needs to be proved...
Code:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 1000026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51 int x=0,f=1;char ch=getchar();52 53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}54 55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}56 57 return x*f;58 59 }60 int n,ans,fa[maxn],id[maxn],ned[maxn],mx[maxn];61 inline bool cmp(int x,int y){return ned[x]<ned[y];}62 63 int main()64 65 {66 67 freopen("input.txt","r",stdin);68 69 freopen("output.txt","w",stdout);70 71 n=read();72 for1(i,n)73 {74 fa[i]=read();ned[i]=read();mx[i]=read();id[i]=i;75 }76 sort(id+1,id+n+1,cmp);77 for1(i,n)78 {79 bool flag=0;80 for(int j=id[i];j;j=fa[j])81 if(mx[j]<ned[id[i]])flag=1;82 if(flag)continue;83 for(int j=id[i];j;j=fa[j])mx[j]-=ned[id[i]];84 ans++;85 }86 printf("%d\n",ans);87 88 return 0;89 90 }
View code
"Poetize10" Energy acquisition