Pagini recente » Cod sursa (job #1003459) | Cod sursa (job #1105417) | Cod sursa (job #1401969) | Cod sursa (job #2266780) | Cod sursa (job #767554)
Cod sursa(job #767554)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define nmax 10005
ifstream f("salvare.in");
ofstream g("salvare.out");
vector<int> gf[nmax], cpy[nmax];
int n, k, viz[nmax], d[nmax];
int rez[nmax];
bool special[nmax];
int st[nmax], _sz;
queue<pair<int, int> > q;
int t[nmax];
int sol[nmax];
typedef vector<int>::iterator it_vec;
void citeste(){
f >> n;
f >> k;
for(int i=1; i<n; i++){
int x, y;
f >> x >> y;
gf[x].push_back(y);
gf[y].push_back(x);
}
}
int check(int val){
int cnt = 0;
rez[0] = 0;
for(int i=1; i<=n; i++) cpy[i] = gf[i], viz[i]=0, d[i]=0, t[i] = 0;
for(;q.size(); q.pop());
for(int i=1; i<=n; i++){
if (cpy[i].size()==1){
q.push(make_pair(i,val));
}
}
for(; q.size(); ){
int nod = (q.front()).first;
int x = (q.front()).second;
d[nod] = x;
q.pop();
int vc = cpy[nod][0];
cpy[nod].clear();
for(it_vec j=cpy[vc].begin(); j!=cpy[vc].end(); j++){
if (*j == nod){
cpy[vc].erase(j);
break;
}
}
if (viz[vc] == 0 && x-1 == 0){
viz[vc] = 1;
rez[++rez[0]] = vc;
}
if ( t[vc]<=n/2 )++t[vc], q.push(make_pair(vc,x-1));
}
for(int i=1; i<=n; i++){
if (d[i] < 0)
if (d[i] < (-1)*(val)){
return 0;
}
}
if (cnt > k) return 0;
if (rez[0]< k)
for(int i=1; i<=n; i++){
if (viz[i] == 0) rez[++rez[0]] = i;
if (rez[0] == k) break;
}
return 1;
}
void rezolva(){
int st=0;
int dr = n+1;
while(dr-st>1){
int mij =(st+dr) / 2;
if (check(mij)){
sol[0] = 0;
for(int i=1; i<=rez[0]; i++) sol[++sol[0]] = rez[i];
dr = mij;
}else st = mij;
}
g << dr << "\n";
sort(sol+1, sol+sol[0]+1 );
for(int i=1; i<=sol[0]; i++) g << sol[i] << " ";
}
int main(){
citeste();
rezolva();
}