답안 #610930

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
610930 2022-07-28T19:06:50 Z _maks2000 Fireworks (APIO16_fireworks) C++17
0 / 100
0 ms 212 KB
#include<iostream>
#include<vector>
#include<set>

using namespace std;
typedef pair<int,int>pi;

vector<pi>dp ;
int timee=0,odp=0 ;

vector<int>czas ;

void DFS(int v, int ojciec, int suma, vector<vector<pi>>&V)
{
    //cout << v+1 << endl ;
    czas[v]=timee++ ;
    for(auto [u,du]:V[v])
    {
        if(u==ojciec) continue ;
        DFS(u,v,suma+du,V) ;
    }

    if(V[v].size()==1)
    {
        dp[czas[v]]={suma,suma} ;
        return ;
    }

    multiset<int>S ;
    for(auto [u,du]:V[v])
    {
        if(u==ojciec) continue ;
        auto [x,y]=dp[czas[u]] ;
        S.insert(x) ;
        S.insert(y);
    }
    //cout << v+1 << " " << S.size() << " " ;

    int kat=-S.size()/2,minn,last ;
    for(auto i:S)
    {
        if(kat==0)
        {
            minn=i ;
            break ;
        }
        kat++ ;
        last=i ;
    }
    dp[czas[v]]={last,minn} ;

    //cout << minn << endl ;

    for(auto [u,du]:V[v])
    {
        if(u==ojciec) continue ;
        auto [x,y]=dp[czas[u]] ;
        int xd=0 ;
        if(minn<x) xd=x-minn ;
        if(minn>y) xd=minn-y;
        odp+=xd;
    }
    return ;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0) ;
    std::cout.tie(0);

    /*
    dp[i]=
    {
        jesli lisc, to wartosc
        inaczej przejdz po dzieciach i stworz funkcje
        nastepnie oblicz optymalna wartosc
        przejdz po dzieciach jeszcze raz oblicz koszt
        dodaj koszt do wyniku i przejdz dalej
    }
    */
    int n,m; cin >> n >> m;
    dp.resize(n+m,{-1,-1}),czas.resize(n+m,0) ;
    vector<vector<pi>>V(n+m,vector<pi>()) ;
    for(int i=1;i<n+m;i++)
    {
        int b,c; cin >> b >> c;
        b-- ;
        V[i].push_back({b,c}) ;
        V[b].push_back({i,c}) ;
    }
    DFS(0,0,0,V) ;
    /*
    for(auto [x,y]:dp)
    {
        cout << x << " " << y << endl ;
    }
    */
    cout << odp ;
}

Compilation message

fireworks.cpp: In function 'void DFS(int, int, int, std::vector<std::vector<std::pair<int, int> > >&)':
fireworks.cpp:60:22: warning: 'minn' may be used uninitialized in this function [-Wmaybe-uninitialized]
   60 |         if(minn>y) xd=minn-y;
      |                    ~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -