QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#784846#3570. GuardsSGColin#100 ✓127ms3768kbC++201.6kb2024-11-26 16:07:532024-11-26 16:07:54

Judging History

This is the latest submission verdict.

  • [2024-11-26 16:07:54]
  • Judged
  • Verdict: 100
  • Time: 127ms
  • Memory: 3768kb
  • [2024-11-26 16:07:53]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;

#define pb push_back
#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

pii dfs(int n, int m, int w, int sp) { // {min(all), min(sp is selected)}
    vector<int> V;
    vector<pii> E;
    rep(i, 1, m) {
        int u, v;
        cin >> u >> v;
        V.eb(u); V.eb(v); E.eb(u, v);
    }
    sort(all(V));
    V.erase(unique(all(V)), V.end());
    auto getpos = [&](int w) {
        return lower_bound(all(V), w) - V.begin();
    };
    vector<tii> child;
    rep(i, 1, w) {
        int u, v, nn, mm, ww;
        cin >> u >> v >> nn >> mm >> ww;
        auto [fc, gc] = dfs(nn, mm, ww, v);
        child.eb(getpos(u), fc, gc);
    }

    if (sp != -1) sp = getpos(sp);
    int f = INT_MAX, g = INT_MAX;
    auto ok = [&](int S) {
        for (auto [u, v] : E) {
            u = getpos(u); v = getpos(v);
            if (!((S >> u) & 1) && !((S >> v) & 1)) return false;
        }
        return true;
    };
    rep(S, 0, (1 << V.size()) - 1) if (ok(S)) {
        int cst = __builtin_popcount(S);
        for (auto [u, fc, gc] : child) {
            cst += (((S >> u) & 1) ? fc : gc);
        }
        f = min(f, cst);
        if (sp != -1 && ((S >> sp) & 1)) g = min(g, cst);
    }
    return {f, g};
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, w;
    while (cin >> n >> m >> w) printf("%d\n", dfs(n, m, w, -1).first);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 100
Accepted
time: 127ms
memory: 3768kb

input:

5 8 2
1 2
2 4
3 4
1 3
1 5
2 5
3 5
4 5
1 6
3 3 0
6 7
7 8
8 6
5 10
3 2 2
10 11
10 12
11 13
2 1 0
13 9
11 14
3 2 0
14 15
14 16
5 8 2
1 2
2 4
3 4
1 3
1 5
2 5
3 5
4 5
2 7
3 3 0
7 9
7 8
8 9
3 11
3 2 0
11 12
11 13
5 4 2
2 1
3 2
4 1
5 4
4 8
5 4 0
7 6
8 6
9 6
10 9
5 15
5 4 0
12 11
13 11
14 12
15 12
5 4 2
2 1...

output:

8
6
7
7
7
8
7
9
10
10
11
8
10
10
8
11
12
12
13
15
12
11
12
12
13
13
15
15
17
13
12
15
16
17
16
19
18
20
8
8
9
9
9
10
11
12
12
10
10
12
13
14
15
15
16
16
14
13
15
15
15
19
19
20
21
15
15
18
16
22
22
23
24
25
9
12
10
12
10
13
12
14
15
10
11
14
15
17
17
19
19
20
15
16
19
20
20
21
23
25
24
20
20
21
18
2...

result:

ok 476 lines