第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(沈阳)正式赛-Problem B. Bitwise Exclusive-OR Sequence

题目:

思路分析:

就是简单的并查集上dfs搜索就行!看代码 

不要直接mms 97.3分 直接爆炸!

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */




constexpr int N = 1e5 + 10;

int n, m, f[N], sum, tot, val[N][32];
bool used[N][32], ok;
vector<pair<int, int>> e[N];

int find(int x) {
    return x == f[x] ? f[x] : f[x] = find(f[x]);
}

void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x != y) {
        f[x] = y;
    }
}

void dfs(int u, int p, int x, int y) {
    sum++;
    tot += y == 1;
    val[u][x] = y;
    used[u][x] = true;
    for (auto [v, w] : e[u]) {
        if (v == p) continue;
        int ny = y;
        if (w >> x & 1) ny ^= 1;
        if (!used[v][x]) {
            dfs(v, u, x, ny);
        } else {
            if (val[v][x] != ny) {
                ok = false;
                cout << -1 << '\n';
                exit(0);
            }
        }
    }
}

int main() {
    
    cin >> n >> m;

    for(int i=0;i<n+1;i++)
        f[i]=0;
//    mms(f,0);
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        merge(u, v);
        e[u].push_back({v, w});
        e[v].push_back({u, w});
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        find(i);
//         if (f[i] == i) {
            for (int j = 0; j < 30; j++) {
                if (used[i][j]) continue;
                int mi = n + 1;
                sum = tot = 0;
                ok = true;
                dfs(i, 0, j, 0);
                if (ok) {
                    mi = min({mi, tot, sum - tot});
                    ans += (1ll << j) * mi;
                } else {
                    cout << -1 << '\n';
                    return 0;
                }
            }
//         }
    }
    cout << ans << '\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121455421