#include <bits/stdc++.h>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
//#define ll long long
template<class T>void chmax(T &a, T b){if (a < b)a = b;}
template<class T>void chmin(T &a, T b){if (b < a)a = b;}
#define int long long
using namespace std;
const int MAXN = (int)2e5 + 5;
const int INF = 1e18;
int A[MAXN], B[MAXN], C[MAXN], D[MAXN];
vector <pii> g[MAXN];
int n;
int q;
int tin[MAXN], tout[MAXN];
int tiktak = 0;
void dfs(int v, int par) {
tin[v] = ++tiktak;
for (auto [to, cost] : g[v]) {
if (to != par) {
dfs(to, v);
}
}
tout[v] = tiktak;
}
bool father(int a, int b) {
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int used[MAXN][2], used_id = 0;
void add(int x) {
for (int i = 1; i < n; i++) {
int a = A[i];
int b = B[i];
if (father(a, b)) {
if (father(b, x)) {
used[i][0] = used_id;
} else {
used[i][1] = used_id;
}
}
else {
if (father(a, x)) {
used[i][1] = used_id;
}
else {
used[i][0] = used_id;
}
}
}
}
int calc_ans() {
int sum = 0;
for (int i = 1; i < n; i++) {
if (used[i][0] != used_id) {
sum += C[i];
}
if (used[i][1] != used_id) {
sum += D[i];
}
}
return sum;
}
int ans[20];
void precalc() {
fill(ans, ans + 20, INF);
for (int mask = 1; mask < (1 << 16); mask++) {
used_id++;
for (int i = 0; i < 16; i++) {
if (mask & (1 << i)) {
add(i + 1);
}
}
int bits = __builtin_popcount(mask);
chmin(ans[bits], calc_ans());
}
}
int e[MAXN];
void brute_force() {
precalc();
for (int i = 1; i <= q; i++) {
cout << ans[e[i]] << endl;
}
exit(0);
}
signed main() {
fastInp;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
g[A[i]].pb({B[i], C[i]});
g[B[i]].pb({A[i], D[i]});
}
dfs(1, -1);
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> e[i];
}
if (n <= 16) {
brute_force();
}
else {
cout << "kek" << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Correct |
29 ms |
5004 KB |
Output is correct |
3 |
Correct |
35 ms |
4940 KB |
Output is correct |
4 |
Correct |
28 ms |
5060 KB |
Output is correct |
5 |
Correct |
29 ms |
4940 KB |
Output is correct |
6 |
Correct |
29 ms |
4940 KB |
Output is correct |
7 |
Correct |
29 ms |
4940 KB |
Output is correct |
8 |
Correct |
29 ms |
4940 KB |
Output is correct |
9 |
Correct |
31 ms |
4940 KB |
Output is correct |
10 |
Correct |
26 ms |
4940 KB |
Output is correct |
11 |
Correct |
31 ms |
5044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
212 ms |
24504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
212 ms |
24496 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Correct |
29 ms |
5004 KB |
Output is correct |
3 |
Correct |
35 ms |
4940 KB |
Output is correct |
4 |
Correct |
28 ms |
5060 KB |
Output is correct |
5 |
Correct |
29 ms |
4940 KB |
Output is correct |
6 |
Correct |
29 ms |
4940 KB |
Output is correct |
7 |
Correct |
29 ms |
4940 KB |
Output is correct |
8 |
Correct |
29 ms |
4940 KB |
Output is correct |
9 |
Correct |
31 ms |
4940 KB |
Output is correct |
10 |
Correct |
26 ms |
4940 KB |
Output is correct |
11 |
Correct |
31 ms |
5044 KB |
Output is correct |
12 |
Correct |
10 ms |
5064 KB |
Output is correct |
13 |
Incorrect |
4 ms |
5196 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
212 ms |
24504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4940 KB |
Output is correct |
2 |
Correct |
29 ms |
5004 KB |
Output is correct |
3 |
Correct |
35 ms |
4940 KB |
Output is correct |
4 |
Correct |
28 ms |
5060 KB |
Output is correct |
5 |
Correct |
29 ms |
4940 KB |
Output is correct |
6 |
Correct |
29 ms |
4940 KB |
Output is correct |
7 |
Correct |
29 ms |
4940 KB |
Output is correct |
8 |
Correct |
29 ms |
4940 KB |
Output is correct |
9 |
Correct |
31 ms |
4940 KB |
Output is correct |
10 |
Correct |
26 ms |
4940 KB |
Output is correct |
11 |
Correct |
31 ms |
5044 KB |
Output is correct |
12 |
Correct |
10 ms |
4940 KB |
Output is correct |
13 |
Incorrect |
212 ms |
24504 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |