QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504972#9102. Zayin and ElementsIllusionaryDominance#TL 0ms3784kbC++201.9kb2024-08-04 17:57:482024-08-04 17:57:51

Judging History

你现在查看的是最新测评结果

  • [2024-08-04 17:57:51]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3784kb
  • [2024-08-04 17:57:48]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200 + 5;

int N, M, a[MAX_N], b[MAX_N], c[MAX_N];
vector <int> G[MAX_N];
int mat[MAX_N], A[MAX_N], B[MAX_N], C[MAX_N], ans;
int deg[MAX_N], idx[MAX_N];

inline bool cmp(int i, int j) {return deg[i] < deg[j];}

void dfs(int i, int res) {
    if (res > ans) return ;
    if (i > N) {
        ans = min(ans, res);
        return ;
    }
    vector <pair <int, int>> candidate(0);
    for (auto x : G[idx[i]]) {
        if (A[x] != a[x]) {
            candidate.emplace_back(0, x);
        }else if (B[x] != b[x]) {
            candidate.emplace_back(B[x] & 1, x);
        }else if (C[x] != c[x]) {
            candidate.emplace_back(1, x);
        }
    }
    sort(candidate.begin(), candidate.end());
    for (auto [cost, x] : candidate) {
        int ty;
        if (A[x] != a[x]) {
            A[x] ++; ty = 1;
        }else if (B[x] != b[x]) {
            B[x] ++; ty = 2;
        }else if (C[x] != c[x]) {
            C[x] ++; ty = 3;
        }
        dfs(i + 1, res + cost);
        if (ty == 1) {
            A[x] --;
        }else if (ty == 2) {
            B[x] --;
        }else {
            C[x] --;
        }
    }
}

void solve() {
    scanf("%d%d", &N, &M);
    int sum = 0;
    for (int i = 1; i <= M; i ++) {
        int n;
        scanf("%d%d%d%d", a + i, b + i, c + i, &n);
        A[i] = B[i] = C[i] = 0;
        for (int j = 1; j <= n; j ++) {
            int x;
            scanf("%d", &x);
            G[x].push_back(i);
            deg[x] ++;
        }
        sum += b[i] + c[i];
    }
    for (int i = 1; i <= N; i ++) idx[i] = i;
    sort(idx + 1, idx + N + 1, cmp);
    ans = 0x3f3f3f3f;
    dfs(1, 0);
    if (ans < 0x3f3f3f3f) printf("%d\n", sum - ans);
    else printf("-1\n");
}

int main() {
    int T;
    scanf("%d", &T);
    while (T --) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3784kb

input:

2
5
2
2 3 1 2 3 1
1 1 1 3 4 5 2
5
2
2 3 1 1 3
1 0 1 2 1 2

output:

5
-1

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

5
9
10
0 0 10 2 3 8
0 0 10 2 4 6
0 8 2 2 2 4
0 0 10 3 1 3 8
0 4 6 2 2 3
0 8 2 3 2 6 7
0 9 1 2 1 7
0 2 8 3 1 4 9
0 7 3 1 5
0 0 10 3 5 6 9
3
10
0 9 1 1 2
0 5 5 1 1
0 1 9 1 1
0 7 3 1 1
0 7 3 0
0 0 10 0
0 6 4 1 3
0 9 1 0
0 7 3 0
0 9 1 1 2
90
20
0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89
0 1 9 12 3 8 21 3...

output:


result: