QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761009#6613. Bitwise Exclusive-OR Sequencegycheng123456#WA 19ms144428kbC++142.9kb2024-11-18 20:35:102024-11-18 20:35:10

Judging History

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

  • [2024-11-18 20:35:10]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:144428kb
  • [2024-11-18 20:35:10]
  • 提交

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][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;
        vector<int> u;
        while(q.size())
        {
            int t = q.front();
            u.push_back(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 1;
                    }
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        for(auto [x, y] : g1[j])
        {
            if(col[x] && col[y])
            {
                if(col[x] != col[y])
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
            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(int i = 0; i < u.size(); i++)
        {
            if(col[u[i]] == 1) cnt++;
            else cnt1++;
        }
        ans += min(cnt, cnt1) * qmi(2ll, j);
        return 2;
    };
    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())
            {
                int sum = bfs(i, j);
                if(sum == 1)
                {
                    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: 19ms
memory: 144196kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 16ms
memory: 144292kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Wrong Answer
time: 19ms
memory: 144428kb

input:

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

output:

47

result:

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