#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 |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
4 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
9 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
4 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
9 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
4 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
9 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |