QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#328295#1844. CactusHunsterAC ✓223ms70000kbC++143.3kb2024-02-15 18:56:432024-02-15 18:56:43

Judging History

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

  • [2024-02-15 18:56:43]
  • 评测
  • 测评结果:AC
  • 用时:223ms
  • 内存:70000kb
  • [2024-02-15 18:56:43]
  • 提交

answer

#include <bits/stdc++.h>

constexpr int N = 300005;

int n, m, cnt;
struct Edge { int x, y; };
Edge edge[N * 2];
bool del[N * 2], vis[N * 2];
std::vector<int> graph1[N];
int deg1[N];

int go(int u, int e) {
    if (u == edge[e].x) return edge[e].y;
    if (u == edge[e].y) return edge[e].x;
    assert(false);
}

std::vector<int> ans;
void work(int u) {
    ans.push_back(u);
    deg1[u] = 0;
    std::vector<int> out;
    for (int e : graph1[u]) {
        if (del[e]) continue;
        del[e] = 1;
        int v = go(u, e);
        deg1[v]--;
        out.push_back(v);
    }
    for (int v : out) if (deg1[v] & 1) work(v);
}

int deg2[N * 2];
std::vector<int> graph2[N * 2];
int dfn[N], low[N], tot, dfs_cnt;
std::stack<int> sta;
void tarjan(int u) {
    sta.push(u);
    low[u] = dfn[u] = ++dfs_cnt;
    for (int e : graph1[u]) {
        if (del[e]) continue;
        if (vis[e]) continue;
        vis[e] = 1;
        int v = go(u, e);
        if (!dfn[v]) {
            tarjan(v);
            low[u] = std::min(low[u], low[v]);
            if (low[v] >= dfn[u]) {
                tot++;
                int x;
                do {
                    x = sta.top();
                    sta.pop();
                    deg2[x]++;
                    deg2[tot]++;
                    graph2[x].push_back(tot);
                    graph2[tot].push_back(x);
                }
                while (x != v);
                deg2[u]++;
                deg2[tot]++;
                graph2[u].push_back(tot);
                graph2[tot].push_back(u);
            }
        }
        else low[u] = std::min(low[u], dfn[v]);
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        edge[++cnt] = {x, y};
        graph1[x].push_back(cnt);
        graph1[y].push_back(cnt);
        deg1[x]++;
        deg1[y]++;
    }
    for (int i = 1; i <= n; i++) if (deg1[i] & 1) work(i);
    for (int i = 1; i <= n; i++) assert(deg1[i] >= 0 && ~deg1[i] & 1);
    ans.push_back(-1);
    tot = n;
    for (int i = 1; i <= n; i++) {
        if (deg1[i]) {
            if (!dfn[i]) tarjan(i);
        }
        else ans.push_back(i);
    }
    std::queue<int> que;
    for (int i = 1; i <= tot; i++) if (deg2[i] == 1) que.push(i);
    while (que.size()) {
        int u = que.front();
        que.pop();
        if (u > n) {
            int p = -1;
            for (int i = 0; i < graph2[u].size(); i++)
                if (deg2[graph2[u][i]] > 1) 
                    p = i;
            std::vector<int> cur;
            if (p >= 0) {
                for (int i = p; i < graph2[u].size(); i++) cur.push_back(graph2[u][i]);
                for (int i = 0; i < p; i++) cur.push_back(graph2[u][i]);
            }
            else cur = graph2[u];
            for (int i = 1; i < cur.size(); i++) ans.push_back(cur[i] + (i & 1) * n);
            ans.push_back(cur[1]);
            ans.push_back(cur.back() + (cur.size() & 1) * n);
            if (p == -1) ans.push_back(cur[0]);
        }
        for (int v : graph2[u]) 
            if (--deg2[v] == 1)
                que.push(v);
    }
    printf("%d %lu\n", 0, ans.size());
    for (int x : ans) {
        if (x < 0) puts("2");
        else printf("1 %d\n", x);
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 32540kb

input:

3 3
1 2
1 3
2 3

output:

0 6
2
1 5
1 1
1 2
1 4
1 3

result:

ok You are right!

Test #2:

score: 0
Accepted
time: 0ms
memory: 30484kb

input:

7 7
1 2
1 3
2 3
2 4
2 5
3 6
3 7

output:

0 13
1 4
1 2
1 1
1 6
1 3
2
1 1
1 2
1 3
1 4
1 5
1 6
1 7

result:

ok You are right!

Test #3:

score: 0
Accepted
time: 135ms
memory: 49052kb

input:

300000 368742
1 143504
1 234282
2 91276
2 296320
3 274816
4 212293
4 258214
5 253489
5 295826
6 96521
6 252745
6 267103
6 269879
7 5293
7 295586
8 44304
8 57067
8 233291
9 190526
10 18682
11 7440
12 24695
12 172561
12 243692
12 280316
13 80152
13 268749
14 146394
14 207280
15 151280
15 226848
16 458...

output:

0 521873
1 3
1 274816
1 273658
1 217572
1 248509
1 231418
1 73094
1 117739
1 11855
1 48428
1 55953
1 121689
1 234392
1 181317
1 238712
1 77369
1 105062
1 6838
1 130659
1 8
1 44304
1 9
1 10
1 18682
1 63155
1 148043
1 194189
1 26612
1 1155
1 31020
1 108936
1 71065
1 77412
1 124995
1 126920
1 53663
1 4...

result:

ok You are right!

Test #4:

score: 0
Accepted
time: 151ms
memory: 55780kb

input:

221155 322762
1 16446
1 214165
2 22590
2 67599
2 77971
2 173665
3 93601
3 218260
4 78445
4 126476
5 85674
5 129671
6 105765
6 161364
7 16742
7 19554
7 28037
7 51308
7 57193
7 151371
7 173612
7 218897
8 50830
8 178401
9 36068
9 198155
10 31227
10 57914
11 104822
11 196836
12 57848
12 129309
13 17006
...

output:

0 424372
2
1 221420
1 193
1 265
1 221348
1 221429
1 175
1 274
1 221330
1 221770
1 31
1 615
1 221186
1 222357
1 898
1 1202
1 222053
1 222672
1 499
1 1517
1 221654
1 223187
1 1288
1 2032
1 222443
1 223818
1 542
1 2663
1 221697
1 224020
1 570
1 2865
1 221725
1 224162
1 1618
1 3007
1 222773
1 224249
1 2...

result:

ok You are right!

Test #5:

score: 0
Accepted
time: 216ms
memory: 66664kb

input:

299999 449997
1 135132
1 274953
2 18542
2 217508
3 47624
3 205183
4 121104
4 270541
5 117846
5 249889
6 272286
6 286376
7 63903
7 89777
8 153806
8 271509
9 49792
9 220343
10 4543
10 293635
11 99727
11 261001
12 29177
12 108823
13 119446
13 183797
14 210754
14 247874
15 53747
15 122774
15 179117
15 2...

output:

0 599998
2
1 301409
1 1121
1 1410
1 301120
1 301763
1 42
1 1764
1 300041
1 301891
1 432
1 1892
1 300431
1 302787
1 277
1 2788
1 300276
1 303226
1 1568
1 3227
1 301567
1 303307
1 265
1 3308
1 300264
1 303398
1 92
1 3399
1 300091
1 303416
1 1898
1 3417
1 301897
1 303730
1 1840
1 3731
1 301839
1 303769...

result:

ok You are right!

Test #6:

score: 0
Accepted
time: 174ms
memory: 52788kb

input:

300000 399999
1 215446
1 231704
2 115181
2 170051
3 1993
3 176144
4 18113
5 34133
5 53137
6 163395
6 256615
7 39049
7 128932
8 193977
8 205442
9 2810
9 260471
10 169593
10 278417
11 56849
11 139915
12 78729
12 164991
13 55416
13 62164
14 195504
14 254962
15 41739
15 143315
16 61905
16 244260
16 2772...

output:

0 511052
1 4
1 16
1 18
1 22
1 202949
1 285958
1 29
1 30
1 26134
1 271021
1 32
1 123549
1 181630
1 103490
1 257757
1 26829
1 52124
1 64135
1 65458
1 71442
1 80024
1 233192
1 109574
1 81157
1 35
1 139478
1 250704
1 64740
1 52176
1 166094
1 224937
1 263467
1 100032
1 77446
1 226783
1 128106
1 200630
1 ...

result:

ok You are right!

Test #7:

score: 0
Accepted
time: 223ms
memory: 70000kb

input:

299999 449997
1 207233
1 249849
2 26483
2 120133
3 48410
3 154146
3 264694
3 276131
4 33119
4 235166
5 104107
5 256329
6 121201
6 153902
7 217994
7 249799
8 98561
8 149095
8 161763
8 244331
9 113135
9 263126
10 78235
10 93112
11 58805
11 104743
11 135998
11 140583
11 199748
11 227329
12 34054
12 124...

output:

0 599998
2
1 300345
1 325
1 346
1 300324
1 302473
1 1304
1 2474
1 301303
1 303115
1 2780
1 3116
1 302779
1 303264
1 2303
1 3265
1 302302
1 303566
1 437
1 3567
1 300436
1 303704
1 2704
1 3705
1 302703
1 304095
1 3438
1 4096
1 303437
1 304339
1 2750
1 4340
1 302749
1 304729
1 1852
1 4730
1 301851
1 30...

result:

ok You are right!

Test #8:

score: 0
Accepted
time: 195ms
memory: 63304kb

input:

280291 392867
1 18749
1 136817
2 63519
2 159662
2 172896
2 251761
3 1878
3 142748
4 203284
4 228584
5 15844
5 50208
6 136817
6 245137
7 64955
7 165499
8 136817
8 220201
9 136817
9 191205
10 136817
10 192877
11 139542
11 162473
12 57326
12 190877
13 33859
13 153427
14 110791
14 136817
15 16389
15 864...

output:

0 505446
2
1 280850
1 413
1 559
1 280704
1 281475
1 562
1 1184
1 280853
1 281498
1 956
1 1207
1 281247
1 281544
1 762
1 1253
1 281053
1 281631
1 712
1 1340
1 281003
1 281641
1 1035
1 1350
1 281326
1 282169
1 3
1 1878
1 280294
1 282411
1 1370
1 2120
1 281661
1 282715
1 901
1 2424
1 281192
1 282810
1 ...

result:

ok You are right!

Test #9:

score: 0
Accepted
time: 222ms
memory: 69196kb

input:

292995 410094
1 71999
1 169963
2 98256
2 132856
3 132856
3 165225
4 82003
4 132856
5 132364
5 160935
6 35335
6 147436
7 132856
7 255985
8 132856
8 200164
9 4463
9 156552
10 2320
10 99646
11 132856
11 185279
12 110686
12 285870
13 73136
13 275400
14 121746
14 132856
15 153856
15 278178
16 83552
16 21...

output:

0 527196
2
1 293851
1 426
1 856
1 293421
1 294442
1 223
1 1447
1 293218
1 294874
1 1462
1 1879
1 294457
1 295381
1 1476
1 2386
1 294471
1 295402
1 604
1 2407
1 293599
1 295472
1 1488
1 2477
1 294483
1 295610
1 1374
1 2615
1 294369
1 295653
1 752
1 2658
1 293747
1 296267
1 2399
1 3272
1 295394
1 2965...

result:

ok You are right!

Test #10:

score: 0
Accepted
time: 129ms
memory: 48876kb

input:

300000 403707
1 129294
1 278573
2 11106
2 129294
3 184237
3 221196
4 55817
4 100929
4 136405
4 238314
5 31450
5 186997
6 4795
7 129294
7 163574
8 61455
8 129294
9 21984
9 129294
10 231237
11 129294
11 228549
12 129294
12 224390
13 19505
13 26574
14 55849
14 98378
15 23106
16 4025
16 100577
17 67090
...

output:

0 495571
1 6
1 10
1 231237
1 158616
1 69702
1 15
1 23106
1 19
1 20
1 68436
1 110694
1 14262
1 110432
1 247212
1 49549
1 244837
1 11639
1 141945
1 171293
1 25
1 26
1 37
1 90299
1 43
1 44
1 220797
1 7582
1 153915
1 210713
1 53091
1 36603
1 89991
1 60626
1 71253
1 140486
1 24893
1 7891
1 138920
1 22312...

result:

ok You are right!

Test #11:

score: 0
Accepted
time: 3ms
memory: 30476kb

input:

1 0

output:

0 2
2
1 1

result:

ok You are right!

Test #12:

score: 0
Accepted
time: 107ms
memory: 45852kb

input:

255596 313062
1 10655
1 15889
1 35851
1 220010
1 243820
2 236476
3 75462
3 240876
3 241881
4 164691
5 112681
6 31313
6 40322
6 89447
6 199617
7 78108
7 249785
8 180866
8 225929
9 26444
10 209074
11 108986
12 147579
12 165165
13 53313
13 182814
13 230088
14 124147
15 73182
15 98755
15 212191
16 98391...

output:

0 415963
1 1
1 10655
1 15889
1 220010
1 243820
1 216583
1 68743
1 101829
1 86887
1 2
1 3
1 75462
1 240876
1 241881
1 1722
1 4
1 5
1 9
1 10
1 209074
1 177824
1 11
1 108986
1 13
1 182814
1 14
1 124147
1 5520
1 9028
1 62318
1 72214
1 187645
1 116944
1 15
1 18
1 147217
1 195359
1 49875
1 37460
1 74697
1...

result:

ok You are right!

Test #13:

score: 0
Accepted
time: 161ms
memory: 59320kb

input:

244001 330353
1 33864
1 90721
1 120604
1 122898
1 195228
1 211057
2 9529
2 118870
3 35564
3 229469
4 81097
4 236945
5 47476
5 223470
6 46769
6 48597
7 49304
7 146411
7 154996
7 220507
8 14839
8 162760
9 85423
9 114698
10 4239
10 32108
10 39949
10 85700
11 160326
11 205183
12 25068
12 156392
13 13985...

output:

0 416708
2
1 244974
1 441
1 973
1 244442
1 245260
1 534
1 1259
1 244535
1 245276
1 510
1 1275
1 244511
1 245436
1 359
1 1435
1 244360
1 246178
1 1415
1 2177
1 245416
1 246613
1 856
1 2612
1 244857
1 246775
1 666
1 2774
1 244667
1 246845
1 81
1 2844
1 244082
1 246882
1 642
1 2881
1 244643
1 246923
1 ...

result:

ok You are right!