# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
910886 | Isaac_Q1 | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<int> temp;
int res = INT_MAX;
int h[100][2];
int check()
{
int ht[100]={0};
int cnt = 0;
int n = temp.size();
for(int i=0; i<n; i++)
{
ht[h[i][0]] ++;
ht[h[i][1]] ++;
cnt += 2;
if(ht[h[i][0]] == 2) cnt -= 2;
else if(ht[h[i][0]] > 2) return 0;
if(ht[h[i][1]] == 2) cnt -= 2;
else if(ht[h[i][1]] > 2) return 0;
}
if(cnt == 2) return 1;
return 0;
}
void sub(int a[], int ind, int target, int n)
{
if(ind >= n) return;
if(target < 0) return;
if(target == 0)
{
if(check()){
int k = temp.size();
res = min(res, k);
}
return;
}
for(int i = ind; i<n; i++)
{
temp.push_back(i);
target -= a[i];
sub(a,i+1,target,n);
target += a[i];
temp.pop_back();
}
return;
}
int main()
{
int n,k;
cin >> n >> k;
int l[n-1];
for(int i=0; i<n-1; i++) cin >> h[i][0] >> h[i][1];
for(int i=0; i<n-1; i++)
{
cin >> l[i];
}
int u;
int u1,u2;
for(int i=0; i<n-2; i++)
{
for(int j=i+1; j<n-1; j++)
{
if(l[i] > l[j])
{
u = l[i];
l[i] = l[j];
l[j] = u;
u1 = h[i][0];
h[i][0] = h[j][0];
h[j][0] = u1;
u2 = h[i][1];
h[i][1] = h[j][1];
h[j][1] = u2;
}
}
}
sub(l,0,k,n-1);
if(res == INT_MAX) cout << "-1";
else cout << res;
return 0;
}