QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#662171#6613. Bitwise Exclusive-OR SequencemwhTL 992ms27040kbC++171.9kb2024-10-20 21:41:272024-10-20 21:41:28

Judging History

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

  • [2024-10-20 21:41:28]
  • 评测
  • 测评结果:TL
  • 用时:992ms
  • 内存:27040kb
  • [2024-10-20 21:41:27]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define int long long

const int mod = 998244353;

bool dfs(int u, vector<int> &color, vector<vector<int>> &g, vector<vector<int>> &g1, int &x, int &y) {
    for (auto v: g[u]) {
        if (color[v] == 0) {
            color[v] = color[u];
            if (color[v] == 1) x++;
            else y++;
            if (!dfs(v, color, g, g1, x, y)) return false;
        } else if (color[u] != color[v]) return false;
    }
    for (auto v: g1[u]) {
        if (color[v] == 0) {
            if (color[u] == 1) y++, color[v] = -1;
            else x++, color[v] = 1;
            if (!dfs(v, color, g, g1, x, y)) return false;
        } else if (color[u] == color[v]) return false;
    }
    return true;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<array<int, 3>> re(m);
    for (int i = 0; i < m; i++) {
        cin >> re[i][0] >> re[i][1] >> re[i][2];
        re[i][0]--, re[i][1]--;
    }
    int res = 0;
    for (int i = 0; i < 30; i++) {
        vector<vector<int>> g(n), g1(n);
        vector<int> color(n);
        vector<pair<int, int>> ans;
        for (int j = 0; j < m; j++)
            if (re[j][2] & (1 << i))
                g1[re[j][0]].push_back(re[j][1]), g1[re[j][1]].push_back(re[j][0]);
            else
                g[re[j][0]].push_back(re[j][1]), g[re[j][1]].push_back(re[j][0]);
        for (int j = 0; j < n; j++) {
            if (color[j]) continue;
            int x = 1, y = 0;
            color[j] = 1;
            if (!dfs(j, color, g, g1, x, y)) {
                cout << "-1\n";
                return;
            }
            ans.emplace_back(x, y);
        }
        for (auto [x, y]: ans) res += min(x, y) * (1 << i);
    }
    cout << res << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;
    while (t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

5 5
3 4 12
3 1 20
2 5 16
1 4 24
4 5 19

output:

58

result:

ok 1 number(s): "58"

Test #4:

score: 0
Accepted
time: 40ms
memory: 9336kb

input:

500 124750
1 2 31473
1 3 11597
1 4 6686
1 5 1214
1 6 14442
1 7 1042
1 8 19057
1 9 22648
1 10 24461
1 11 25714
1 12 3976
1 13 31954
1 14 7384
1 15 13988
1 16 28651
1 17 31599
1 18 8786
1 19 27068
1 20 9645
1 21 28281
1 22 11681
1 23 28897
1 24 31916
1 25 10462
1 26 23973
1 27 4615
1 28 5124
1 29 2026...

output:

8041745

result:

ok 1 number(s): "8041745"

Test #5:

score: 0
Accepted
time: 46ms
memory: 9364kb

input:

500 124750
1 2 3902
1 3 9006
1 4 2914
1 5 8753
1 6 2395
1 7 21406
1 8 14552
1 9 25834
1 10 28282
1 11 9684
1 12 11347
1 13 20545
1 14 16324
1 15 16951
1 16 11594
1 17 5035
1 18 17726
1 19 831
1 20 23194
1 21 7693
1 22 6147
1 23 315
1 24 32755
1 25 17078
1 26 11348
1 27 9587
1 28 21015
1 29 881
1 30 ...

output:

7803950

result:

ok 1 number(s): "7803950"

Test #6:

score: 0
Accepted
time: 992ms
memory: 27040kb

input:

100000 200000
82262 26109 1005476194
43106 86715 475153289
59086 60577 507254441
71498 80384 186530379
99676 3003 289537598
30772 72897 345346447
12686 87447 896623879
12520 27709 26442442
82734 20830 967590473
13380 76164 927495776
25723 55377 89078582
7173 86993 669894679
37790 94846 331905713
365...

output:

52538527353096

result:

ok 1 number(s): "52538527353096"

Test #7:

score: -100
Time Limit Exceeded

input:

100000 200000
15687 27598 177189307
20863 37114 1037884991
93538 8500 447584932
79876 73177 560212440
90266 81435 191658398
78954 69980 476724968
3024 95419 1016359891
28005 78423 806987047
22363 37592 995962252
80788 41407 484625454
32534 34520 497714826
66857 76961 49733398
2725 7116 786428563
393...

output:

52602853327156

result: