Submission #717219

# Submission time Handle Problem Language Result Execution time Memory
717219 2023-04-01T14:54:54 Z Obada_Saleh Beads and wires (APIO14_beads) C++14
0 / 100
4 ms 5076 KB
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<long long,int>>adj[200010];
long long dp1[200010],dp2[200010],dp3[200010],dp4[200010];
bool comp(pair<long long,int>x,pair<long long,int>y)
{
    return x.first+dp3[x.second]-dp4[x.second]>y.first+dp3[y.second]-dp4[y.second];
}
void dfs(int node=1,int p=0)
{
    for(int i=0;i<adj[node].size();i++)
    {
        int neighbour=adj[node][i].second;
        if(neighbour==p)
        {
            adj[node].erase(adj[node].begin()+i);
            break;
        }
    }
    for(int i=0;i<adj[node].size();i++)
    {
        int neighbour=adj[node][i].second;
        dfs(neighbour,node);
    }
    for(int i=0;i<adj[node].size();i++)
    {
        long long weight=adj[node][i].first;
        int neighbour=adj[node][i].second;
        dp4[neighbour]=dp3[neighbour];
        if(dp1[neighbour]!=0)
            dp4[neighbour]=max(dp4[neighbour],weight+dp1[neighbour]);
    }
    sort(adj[node].begin(),adj[node].end(),comp);
    if(adj[node].size()!=0)
    {
        long long weight=adj[node][0].first;
        int neighbour=adj[node][0].second;
        dp1[node]=weight+dp3[neighbour];
        for(int i=1;i<adj[node].size();i++)
        {
            weight=adj[node][i].first;
            neighbour=adj[node][i].second;
            dp1[node]+=dp4[neighbour];
        }
    }
    if(adj[node].size()>=2)
    {
        long long weight;
        int neighbour;
        for(int i=0;i<2;i++)
        {
            weight=adj[node][i].first;
            neighbour=adj[node][i].second;
            dp2[node]+=weight+dp3[neighbour];
        }
        for(int i=2;i<adj[node].size();i++)
        {
            weight=adj[node][i].first;
            neighbour=adj[node][i].second;
            dp2[node]+=dp4[neighbour];
        }
    }
    for(int i=0;i<adj[node].size();i++)
    {
        long long weight=adj[node][i].first;
        int neighbour=adj[node][i].second;
        dp3[node]+=dp4[neighbour];
    }
    dp3[node]=max(dp3[node],dp2[node]);
}

int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","r",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        long long w;
        cin>>w;
        adj[u].push_back({w,v});
        adj[v].push_back({w,u});
    }
    dfs();
    cout<<dp3[1];
    return 0;
}

Compilation message

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:12:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int i=1;i<adj[node].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
beads.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int i=2;i<adj[node].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
beads.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:66:19: warning: unused variable 'weight' [-Wunused-variable]
   66 |         long long weight=adj[node][i].first;
      |                   ^~~~~~
beads.cpp: In function 'int main()':
beads.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen("beads.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
beads.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     freopen("beads.out","r",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -