QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#703786#6613. Bitwise Exclusive-OR SequenceYurily#WA 50ms12412kbC++202.3kb2024-11-02 18:30:212024-11-02 18:30:22

Judging History

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

  • [2024-11-02 18:30:22]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:12412kb
  • [2024-11-02 18:30:21]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long LL;
const int N = 1e5;
const int M = 2e5;
const int L = 30;

    int n, m;

struct Edge{
    int to, nxt, w;
}e[M * 2 + 3];
    int etop, h[N + 3];

void adde(int u, int v, int w){
    etop++;
    e[etop].to = v;
    e[etop].nxt = h[u];
    e[etop].w = w;
    h[u] = etop;
}

    bool vis[N + 3];
    bool colli;
    int s[N + 3];           //presum xor of node i
    int c[L + 3];           //count of s[]' bin digit being 1
    int tot;                //count of connecting block
    LL ans;

void dfs(int u, int fr){
    for(int tmp = h[u], v, news; tmp != -1; tmp = e[tmp].nxt){
        v = e[tmp].to;
        if(v == fr)
            continue;
        
        news = s[u] ^ e[tmp].w;
        
        if(vis[v]){
            if(s[v] != news)
                colli = true;
            return ;
        }
        else {
            vis[v] = true;
            s[v] = news;
            tot++;

            for(int i = 0; i < L; i++)
                if((s[v] >> i) & 1)
                    c[i]++;

            dfs(v, u);
        }
    }
}

int main(){
    scanf("%d%d", &n, &m);

    etop = 0;
    memset(h, -1, sizeof h);
    for(int i = 1, u, v, w; i <= m; i++){
        scanf("%d%d%d", &u, &v, &w);
        adde(u, v, w);
        adde(v, u, w);
    }

    memset(vis, 0, sizeof vis);
    colli = false;
    ans = 0;
    for(int i = 1; i <= n && !colli; i++)
    if(!vis[i]){
        vis[i] = true;
        s[i] = 0;
        memset(c, 0, sizeof c);
        tot = 1;
        dfs(i, 0);

        // printf("s:");
        // for(int j = 1; j <= n; j++)
        //     printf("%4d", s[j]);
        // printf("\n");
        // printf("c:");
        // for(int j = 0; j < 5; j++)
        //     printf("%3d", c[j]);
        // printf("\n");
        // printf("tot=%d\n", tot);
        // printf("ans=%lld=>", ans);

        for(int j = 0, t = 1; j < L; j++, t <<= 1){
            ans += (LL)min(c[j], tot - c[j]) * t;
        }

        // printf("%lld\n\n", ans);
    }

    if(colli){
        printf("-1\n");
        return 0;
    }

    printf("%lld", ans);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4304kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 4376kb

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: 1ms
memory: 5932kb

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

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

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
Wrong Answer
time: 50ms
memory: 12412kb

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:

-1

result:

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