Cod sursa(job #1322218)

Utilizator pop_bogdanBogdan Pop pop_bogdan Data 19 ianuarie 2015 21:13:14
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int N, K, x, y, v, father;
int D[10001], Sol;

vector <int> G[10001];
vector <int> :: iterator it;

struct compareCosts {
    bool operator()(const int& x, const int& y) const {
        return D[x] > D[y];
    }
};


priority_queue < short, vector<short> , compareCosts > P;

void Input();


int main()
{
    freopen("cezar.in","r",stdin);
    freopen("cezar.out","w",stdout);

    Input();

    for ( int i = 1; i <= N; ++i )
    {
        D[i] = 1;
        if ( G[i].size() == 1 )
            P.push(i);
    }

    for ( int i = 1; i <= (N-1)-K ; ++i )
    {
        v = P.top();
        father = G[v][0];
        D[father] += D[v];
        Sol += D[v];
        G[v].clear();

        it = std::find(G[father].begin(),G[father].end(),v);
        if ( it != G[father].end() )
        G[father].erase(it);

        if ( G[father].size() == 1)
            P.push(father);

        P.pop();

    }

    printf("%i",Sol);
}

void Input()
{
    scanf("%i%i",&N,&K);
    for ( int i = 1; i <= N-1; ++i )
    {
        scanf("%i%i",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
}