Pagini recente » Cod sursa (job #667749) | Cod sursa (job #3033366) | Cod sursa (job #1045246) | Cod sursa (job #1279774) | Cod sursa (job #1058660)
#include<fstream>
#include<bitset>
#include<vector>
#define dim 10004
using namespace std;
const char InFile[]="cezar.in";
const char OutFile[]="cezar.out";
ifstream fin(InFile);
ofstream fout(OutFile);
int grad[dim],cost[dim];
bitset<dim>mark;
int heap[2*dim];
int n,k,N,costminim;
vector<int>G[dim];
int M[dim][dim];
inline void swap(int a,int b){
int aux=a;
a=b;
b=aux;
}
void sift (int nod){
int rs=2*nod+1;
int ls=2*nod;
int son =nod;
if(ls<=N && cost[heap[son]]>cost[heap[ls]])
son=ls;
if(rs<=N && cost[heap[son]]>cost[heap[rs]])
son=rs;
if(son!=nod){
swap(heap[son],heap[nod]);
sift(son);
}
}
void urca(int k){
while(k>1 && cost[heap[k]]<cost[heap[k/2]]){
swap(heap[k/2],heap[k]);
k/=2;
}
}
int main (){
fin>>n>>k;
for(register int i=1 ; i<n ; ++i){
int x,y;
fin>>x>>y;
/*G[x].push_back(y);
G[y].push_back(x);*/
M[x][y]=M[y][x]=1;
++grad[x];
++grad[y];
}
for(register int i=1;i<=n;++i){
heap[i]=n+1;
cost[i]=1;
}
cost[n+1]=20000;
for(register int i=1;i<=n;++i){
if(grad[i]==1)
{
heap[++N]=i;
}
}
for(register int i=n;i>=k+2;--i) {
int nod=heap[1];
costminim+=cost[nod];
/*for(int j=0;j<G[nod].size();++j){
if( mark[nod]==0 ){
int tata=G[nod][j];
cost[tata]+=cost[nod];
heap[++N]=tata;
}
}*/
for(register int j=1;j<=n;++j){
if(M[nod][j] && M[j][nod]){
int tata=j;
cost[tata]+=cost[j];
heap[++N]=tata;
M[nod][j]=M[j][nod]=0;
urca(N);
}
}
--N;
urca(N);
sift(1);
}
fout<<costminim<<"\n";
return 0;
}