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>
#define eb emplace_back
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
struct edge{
int a, b, c, d;
ll get(int v){
if(v == b) return c;
else return d;
}
int ano(int v){
return a ^ b ^ v;
}
};
ll tans = 0;
vector<vector<edge>> g;
void dfs(int now, int p, int msk){
for(edge i : g[now]){
int v = i.ano(now);
if(v == p) continue;
if(!(1 << v & msk)) tans += i.get(v);
dfs(v, now, msk);
}
}
int cnt = 0, leaf = 0;
void dfsvst(int now, int p, int msk){
cnt++;
int deg = 0;
for(edge i : g[now]){
int v = i.ano(now);
if(!(1 << v & msk)) continue;
deg++;
if(v == p) continue;
dfsvst(v, now, msk);
}
if(deg <= 1) leaf++;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
assert(n <= 16);
g.resize(n);
for(int i = 0; i < n - 1; i++){
int u, v, c, d;
cin >> u >> v >> c >> d;
u--; v--;
g[u].eb(edge({u, v, c, d}));
g[v].eb(edge({u, v, c, d}));
}
vector<ll> ans(n + 1, 1LL << 60);
for(int i = 1; i < (1 << n); i++){
cnt = 0;
leaf = 0;
tans = 0;
int v = __lg(i & -i);
dfsvst(v, v, i);
if(cnt != __builtin_popcount(i)) continue;
dfs(v, v, i);
//cerr << bitset<4>(i) << " " << v << " " << tans << " " << leaf << "\n";
ans[leaf] = min(ans[leaf], tans);
}
for(int i = 2; i <= n; i++){
ans[i] = min(ans[i], ans[i - 1]);
}
int q;
cin >> q;
while(q--){
int e;
cin >> e;
cout << ans[e] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |