2 条题解

  • 0
    @ 2025-10-8 17:00:32
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 1e5+10;
    vector<int> G[N];
    LL t[N], c[N], tt[N], ans;
    bool vis[N];
     
    void dfs(int x) {
        vis[x] = 1;
        for(int y: G[x])if(!vis[y]){
            dfs(y);
            c[x] = min(c[x] , c[y]);
            tt[x] += tt[y];
        }
        if(tt[x] < t[x]) {
            ans += (t[x]-tt[x]) * c[x];
            tt[x] = t[x];
        }
    }
    int main() {
        int n,rt;scanf("%d",&n);
        for(int i = 1,x; i <= n; i++) {
            scanf("%d%lld%lld",&x,&t[i],&c[i]);
            if(x != -1) G[x].push_back(i);
            else rt = i;
        }
        ans=0; memset(vis,0,sizeof(vis));
        dfs(rt);
        printf("%lld\n",ans);
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:25
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const int N = 1e5+10;
      vector<int> G[N];
      LL t[N], c[N], tt[N], ans;
      bool vis[N];
       
      void dfs(int x) {
          vis[x] = 1;
          for(int y: G[x])if(!vis[y]){
              dfs(y);
              c[x] = min(c[x] , c[y]);
              tt[x] += tt[y];
          }
          if(tt[x] < t[x]) {
              ans += (t[x]-tt[x]) * c[x];
              tt[x] = t[x];
          }
      }
      int main() {
          int n,rt;scanf("%d",&n);
          for(int i = 1,x; i <= n; i++) {
              scanf("%d%lld%lld",&x,&t[i],&c[i]);
              if(x != -1) G[x].push_back(i);
              else rt = i;
          }
          ans=0; memset(vis,0,sizeof(vis));
          dfs(rt);
          printf("%lld\n",ans);
          return 0;
      }
      • 1

      【递归+贪心】树上装饰[USACO11MAR] Tree Decoration G

      信息

      ID
      2281
      时间
      1000ms
      内存
      128MiB
      难度
      5
      标签
      递交数
      36
      已通过
      16
      上传者