/*****************/
/* [o] Subtask 1 */
/* [ ] Subtask 2 */
/* [ ] Subtask 3 */
/* [ ] Subtask 4 */
/* [ ] Subtask 5 */
/* [ ] Subtask 6 */
/*****************/
#include <bits/stdc++.h>
using namespace std;
#define Foxyy cin.tie(0); cout.sync_with_stdio(0);
#define ll long long
const ll LINF = 1e18;
struct Solver {
int n;
vector<map<int, int>>& adj;
int q;
void solve() {
vector<ll> ans(n+1, LINF);
ll sum = 0;
for (int i = 0; i < n; i++) {
for (auto [j, w] : adj[i]) {
sum += w;
}
}
for (int mask = 1; mask < (1 << n); mask++) {
vector<map<int, bool>> vst(n);
function<void(int, int)> dfs = [&](int u, int p) {
for (auto [v, w] : adj[u]) if (v != p and not vst[u][v]) {
vst[u][v] = true;
dfs(v, u);
}
};
int cnt = __builtin_popcount(mask);
for (int i = 0; i < n; i++) if ((mask >> i) & 1) {
dfs(i, -1);
}
ll cur = 0;
for (int i = 0; i < n; i++) {
for (auto [j, w] : adj[i]) {
if (not vst[j][i]) {
cur += adj[i][j];
}
}
}
// cerr << cnt << ' ' << cur << '\n';
ans[cnt] = min(ans[cnt], cur);
}
while (q--) {
int e;
cin >> e;
cout << ans[e] << '\n';
}
}
};
signed main() {
Foxyy
int T = 1;
// cin >> T;
while (T--) {
int n;
cin >> n;
vector<map<int, int>> adj(n);
for (int i = 0; i < n-1; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
a--, b--;
adj[a][b] = c;
adj[b][a] = d;
}
int q;
cin >> q;
Solver solver{n, adj, q};
solver.solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
160 ms |
304 KB |
Output is correct |
3 |
Correct |
155 ms |
320 KB |
Output is correct |
4 |
Correct |
168 ms |
212 KB |
Output is correct |
5 |
Correct |
154 ms |
316 KB |
Output is correct |
6 |
Correct |
154 ms |
308 KB |
Output is correct |
7 |
Correct |
162 ms |
212 KB |
Output is correct |
8 |
Correct |
157 ms |
308 KB |
Output is correct |
9 |
Correct |
187 ms |
304 KB |
Output is correct |
10 |
Correct |
156 ms |
308 KB |
Output is correct |
11 |
Correct |
221 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
208 ms |
30052 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
210 ms |
36436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
160 ms |
304 KB |
Output is correct |
3 |
Correct |
155 ms |
320 KB |
Output is correct |
4 |
Correct |
168 ms |
212 KB |
Output is correct |
5 |
Correct |
154 ms |
316 KB |
Output is correct |
6 |
Correct |
154 ms |
308 KB |
Output is correct |
7 |
Correct |
162 ms |
212 KB |
Output is correct |
8 |
Correct |
157 ms |
308 KB |
Output is correct |
9 |
Correct |
187 ms |
304 KB |
Output is correct |
10 |
Correct |
156 ms |
308 KB |
Output is correct |
11 |
Correct |
221 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2055 ms |
1080 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
208 ms |
30052 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
160 ms |
304 KB |
Output is correct |
3 |
Correct |
155 ms |
320 KB |
Output is correct |
4 |
Correct |
168 ms |
212 KB |
Output is correct |
5 |
Correct |
154 ms |
316 KB |
Output is correct |
6 |
Correct |
154 ms |
308 KB |
Output is correct |
7 |
Correct |
162 ms |
212 KB |
Output is correct |
8 |
Correct |
157 ms |
308 KB |
Output is correct |
9 |
Correct |
187 ms |
304 KB |
Output is correct |
10 |
Correct |
156 ms |
308 KB |
Output is correct |
11 |
Correct |
221 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Incorrect |
208 ms |
30052 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |