#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
vector<pii> G[20];
vector<bool> tak[20];
void dfs(int x, int p=-1) {
for(int i=0;i<sz(G[x]);i++) {
auto [y, c] = G[x][i];
if(y==p) continue;
tak[x][i] = true;
dfs(y, x);
}
}
pii ans[20];
vector<int> V[20];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int S = 0;
for(int i=0;i<n-1;i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
a--, b--;
G[a].pb({b, d});
G[b].pb({a, c});
tak[a].pb(0);
tak[b].pb(0);
S+=c+d;
}
for(int i=1;i<(1<<n);i++) {
V[__builtin_popcount(i)].pb(i);
}
for(int I=1;I<=n;I++) {
for(int i:V[I]) {
if(I>=3&&((i&ans[I-1].nd)!=ans[I-1].nd))
continue;
for(int j=0;j<n;j++) {
fill(all(tak[j]), false);
}
for(int j=0;j<n;j++) {
if(i&(1<<j)) {
dfs(j);
}
}
int c = 0;
for(int j=0;j<n;j++) {
for(int k=0;k<sz(G[j]);k++) {
if(tak[j][k])
c += G[j][k].nd;
}
}
int x = __builtin_popcount(i);
ans[x] = max(ans[x], {c, i});
}
}
//for(int i=1;i<=n;i++) {
//cout << bitset<20>(ans[i].nd) << "\n";
//}
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
cout << S-ans[x].st << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |