QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761207#6613. Bitwise Exclusive-OR Sequencegyc123TL 127ms260232kbC++142.7kb2024-11-18 21:28:262024-11-18 21:28:27

Judging History

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

  • [2024-11-18 21:28:27]
  • 评测
  • 测评结果:TL
  • 用时:127ms
  • 内存:260232kb
  • [2024-11-18 21:28:26]
  • 提交

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++;
            col[x] = 0;
        }
        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 << -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: 21ms
memory: 191120kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 30ms
memory: 191300kb

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: 23ms
memory: 191316kb

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: 109ms
memory: 259748kb

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: 127ms
memory: 260232kb

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: -100
Time Limit Exceeded

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:


result: