QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#761041#6613. Bitwise Exclusive-OR Sequencegycheng123456#WA 27ms144360kbC++142.9kb2024-11-18 20:41:352024-11-18 20:41:37

Judging History

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

  • [2024-11-18 20:41:37]
  • 评测
  • 测评结果:WA
  • 用时:27ms
  • 内存:144360kb
  • [2024-11-18 20:41:35]
  • 提交

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;
        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 1;
                    }
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        for(auto [x, y] : g1[j])
        {
            u.insert(x);
            u.insert(y);
            if(col[x] && col[y])
            {
                if(col[x] != col[y])
                {
                    return 1;
                }
            }
            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++;
            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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 12ms
memory: 144360kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 27ms
memory: 144180kb

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: 16ms
memory: 144204kb

input:

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

output:

62

result:

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