QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761196#6613. Bitwise Exclusive-OR Sequencegyc123WA 30ms191120kbC++142.7kb2024-11-18 21:25:152024-11-18 21:25:15

Judging History

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

  • [2024-11-18 21:25:15]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:191120kb
  • [2024-11-18 21:25:15]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long

using namespace std;

using PII = pair<int,int>;

const int N = 30, M = 2e5 +10;
vector<int> g[N + 10][M];

int qmi(int a, int b)
{
    int res = 1;
    while(b)
    {
        if(b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}


signed main()
{
    cin.tie(0), ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<int> u(m + 1), v(m + 1), w(m + 1);
    vector<vector<PII>> g1(35);
   
    for(int i = 1; i <= m; i++)
    {
        cin >> u[i] >> v[i] >> w[i];
        for(int j = 0; j <= 30; j++)
        {
            if((w[i] >> j) & 1)
            {
                g[j][u[i]].push_back(v[i]);
                g[j][v[i]].push_back(u[i]);
            }
            else
            {
                g1[j].push_back({v[i], u[i]});
            }
        }
    }


    vector<bool> vis(n + 1, false);
    vector<int> col(n + 1, 0);
    int ans = 0;
    auto bfs = [&](int x, int j){
        queue<int> q;
        vis[x] = true;
        q.push(x);
        col[x] = 1;
        set<int> u;
        while(q.size())
        {
            int t = q.front();
            u.insert(t);
            q.pop();
            for(auto v : g[j][t])
            {
                if(!col[v])
                {
                    col[v] = (col[t] * 2) % 3;
                    vis[v] = true;
                    q.push(v);
                }
                else
                {
                    if(col[v] == col[t]) return false;
                }
            }
        }
        for(auto [x, y] : g1[j])
        {
            u.insert(x);
            u.insert(y);
            if(col[x] && col[y])
            {
                if(col[x] != col[y]) return false;
            }
            else if(!col[x] && !col[y]) ;
            else
            {
                if(!col[x]) col[x] = col[y];
                else col[y] = col[x];
            }
        }
        int cnt = 0, cnt1 = 0;
        for(auto x : u)
        {
            if(col[x] == 1) cnt++;
            if(col[x] == 2) cnt1++;
        }
        ans += min(cnt, cnt1) * qmi(2ll, j);
        return true;
    };
    for(int j = 0; j <= 30; j++)
    {
        for(int i = 1; i <= n; i++) vis[i] = false, col[i] = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!vis[i] && g[j][i].size())
            {
                bool sum = bfs(i, j);
                if(!sum)
                {
                    cout << j << endl;
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 22ms
memory: 191120kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Wrong Answer
time: 30ms
memory: 191004kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

0
-1

result:

wrong answer 1st numbers differ - expected: '-1', found: '0'