Cod sursa(job #1611602)

Utilizator DobosDobos Paul Dobos Data 24 februarie 2016 11:52:25
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;
const int NMAX = 1e4 + 5;
short D[NMAX];

struct cmp{
    bool operator()(const short &a,const short &b){
        return D[a] < D[b];
    }
};


priority_queue < short ,vector < short > ,cmp > heap;
vector < short > G[NMAX];

inline void(short k){
    short nod;
    while(k != n && !heap.empty()){
        nod = heap.top();
        heap.pop();
        if(D[nod] != 0)
            D[nod] ++;
        for(int i = 0; i < G[nod].size(); i++){
            if(D[G[nod][i]] != - 1){
                D[G[nod][i]] += 2*D[nod];
                if(D[nod] == 0)            }
        }
    }
}

int main()
{
    freopen ("cezar.in" , "r", stdin);
    freopen ("cezar.out" , "w", stdout);
    short n,x,y,k;
    scanf("%h %h", &n, &k);
    for(int i =  1; i < n ; i++){
        scanf("%h %h", &x &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i = 1; i <= n; i++){
        if(G[i].size() == 1){
            heap.push(i);
            k++;
        }
    }
    Solve(k,n);
    return 0;
}