QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#692892#6613. Bitwise Exclusive-OR SequenceDoubeecat#ML 371ms8932kbC++171.9kb2024-10-31 15:10:432024-10-31 15:10:55

Judging History

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

  • [2024-10-31 15:10:55]
  • 评测
  • 测评结果:ML
  • 用时:371ms
  • 内存:8932kb
  • [2024-10-31 15:10:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
#define ll long long
struct e {
    int u,v,w;
}e[N];
int f[N],siz[N],n,m;

void init() {
    for (int i = 1;i <= n;++i) f[i] = i,f[i+n] = i+n,siz[i] = 1,siz[i+n] = 0;
}
int find(int x) {
    //cout << x;
    return x == f[x] ? x : f[x] = find(f[x]);
}

void merge(int x,int y) {
    if (x == y) return ;
    siz[y] += siz[x];siz[x] = 0;f[x] = y;
}

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= m;++i) {
        cin >> e[i].u >> e[i].v >> e[i].w;
    }
    bool flag = 0;
    ll ans = 0;
    for (int i = 0;i < 30;++i) {
        init();
        for (int j = 1;j <= m;++j) {
            //for (int k = 1;k <= n;++k )printf("%d %d\n",siz[find(k)],siz[find(k+n)]);
            int nowval = (e[j].w >> i) & 1;
            int x = find(e[j].u),y = find(e[j].v);
            int xx = find(e[j].u+n),yy = find(e[j].v+n);
            if (nowval) {
                if (find(e[j].u) == find(e[j].v)) {
                    flag = 1;break;
                }
                merge(x,yy);merge(y,xx);
            } else {
                if (find(e[j].u) == find(e[j].u+n)) {
                    flag = 1;break;
                }
                merge(x,y);merge(xx,yy);
            }
        }
            //for (int k = 1;k <= n;++k )printf("%d %d\n",siz[find(k)],siz[find(k+n)]);
        for (int j = 1;j <= n;++j) {
            //printf("wq;%d %d\n",siz[find(j)],siz[find(j+n)]);
            if (find(j) != find(j+n)) {
                ans += (ll)min(siz[find(j)],siz[find(j+n)]) * (1LL << i);
                siz[find(j)] = siz[find(j+n)] = 0;
            } else {
                flag = 1;
                break;
            }
        }
        if (flag) break;
    }
    if (flag)     cout << -1;
    else cout << ans;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5620kb

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: 5612kb

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: 5752kb

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: 39ms
memory: 7736kb

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: 38ms
memory: 7756kb

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: 0
Accepted
time: 364ms
memory: 8592kb

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:

52538527353096

result:

ok 1 number(s): "52538527353096"

Test #7:

score: 0
Accepted
time: 362ms
memory: 8532kb

input:

100000 200000
15687 27598 177189307
20863 37114 1037884991
93538 8500 447584932
79876 73177 560212440
90266 81435 191658398
78954 69980 476724968
3024 95419 1016359891
28005 78423 806987047
22363 37592 995962252
80788 41407 484625454
32534 34520 497714826
66857 76961 49733398
2725 7116 786428563
393...

output:

52602853327156

result:

ok 1 number(s): "52602853327156"

Test #8:

score: 0
Accepted
time: 371ms
memory: 8732kb

input:

100000 200000
14517 31233 44615804
33486 41973 1052821004
49545 79689 319778876
8546 59914 211634618
54913 52423 157522220
7407 94892 619152362
68434 82081 302860551
79458 83213 51598667
36118 558 751409357
92878 62707 87357711
71932 57530 121862749
67177 75830 953279062
38483 37005 779602421
90440 ...

output:

52429947891248

result:

ok 1 number(s): "52429947891248"

Test #9:

score: 0
Accepted
time: 290ms
memory: 8932kb

input:

100000 200000
11656 60762 1
16805 66929 17
55148 55195 2
29841 9092 11
31848 48612 12
26261 82795 11
65162 78347 23
15597 88601 21
92692 7801 12
50087 67030 22
75497 23748 1
59304 62297 29
56011 68857 4
11540 9395 11
36408 13733 10
29267 965 15
45943 65984 29
6240 25729 29
64043 76902 7
78121 89050 ...

output:

1514667

result:

ok 1 number(s): "1514667"

Test #10:

score: 0
Accepted
time: 279ms
memory: 8344kb

input:

100000 200000
19078 1796 29
89160 3396 12
14155 57229 5
36362 16264 4
34378 35576 20
89020 76502 18
85772 59833 30
47358 3841 7
15219 48259 1
4778 42876 11
57340 52164 30
98346 7446 16
74577 91274 18
74128 7433 22
24777 57648 5
77806 76383 14
18741 73911 9
98620 83468 18
21676 45630 20
16077 32480 6...

output:

1515438

result:

ok 1 number(s): "1515438"

Test #11:

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

input:

100000 50000
1 2 1073741823
3 4 1073741823
5 6 1073741823
7 8 1073741823
9 10 1073741823
11 12 1073741823
13 14 1073741823
15 16 1073741823
17 18 1073741823
19 20 1073741823
21 22 1073741823
23 24 1073741823
25 26 1073741823
27 28 1073741823
29 30 1073741823
31 32 1073741823
33 34 1073741823
35 36 1...

output:

53687091150000

result:

ok 1 number(s): "53687091150000"

Test #12:

score: 0
Accepted
time: 59ms
memory: 7692kb

input:

100000 99999
2 1 1073741823
3 2 1073741823
4 3 1073741823
5 4 1073741823
6 5 1073741823
7 6 1073741823
8 7 1073741823
9 8 1073741823
10 9 1073741823
11 10 1073741823
12 11 1073741823
13 12 1073741823
14 13 1073741823
15 14 1073741823
16 15 1073741823
17 16 1073741823
18 17 1073741823
19 18 107374182...

output:

53687091150000

result:

ok 1 number(s): "53687091150000"

Test #13:

score: -100
Memory Limit Exceeded

input:

100000 100000
2 1 1073741823
3 2 1073741823
4 3 1073741823
5 4 1073741823
6 5 1073741823
7 6 1073741823
8 7 1073741823
9 8 1073741823
10 9 1073741823
11 10 1073741823
12 11 1073741823
13 12 1073741823
14 13 1073741823
15 14 1073741823
16 15 1073741823
17 16 1073741823
18 17 1073741823
19 18 10737418...

output:


result: