QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71791#2979. 染色He_Ren100 ✓23ms6868kbC++142.7kb2023-01-12 01:14:512023-01-12 01:15:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 01:15:23]
  • 评测
  • 测评结果:100
  • 用时:23ms
  • 内存:6868kb
  • [2023-01-12 01:14:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 5;

vector<int> g[MAXN];

int deg[MAXN];
bool alive[MAXN];

int clr[MAXN];
bool invalid = 0;
vector<int> vec;
void dfs(int u) {
    if (deg[u] > 3)
        invalid = 1;

    if (deg[u] == 3) {
        vec.emplace_back(u);

        if (vec.size() > 2)
            invalid = 1;
    }

    if (invalid)
        return;

    for (int v : g[u]) {
        if (clr[v] == -1) {
            clr[v] = clr[u] ^ 1;
            dfs(v);
        } else if (clr[v] != (clr[u] ^ 1)) {
            invalid = 1;
        }

        if (invalid)
            return;
    }
}

void solve(void) {
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; ++i)
        g[i].clear();

    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    queue<int> q;

    for (int i = 1; i <= n; ++i) {
        deg[i] = (int)g[i].size();
        alive[i] = 1;

        if (deg[i] <= 1)
            alive[i] = 0, q.emplace(i);
    }

    while (q.size()) {
        int u = q.front();
        q.pop();

        for (int v : g[u])
            if (alive[v])
                if (--deg[v] <= 1)
                    alive[v] = 0, q.emplace(v);
    }

    for (int u = 1; u <= n; ++u)
        if (alive[u]) {
            vector<int> nxt;

            for (int v : g[u])
                if (alive[v])
                    nxt.emplace_back(v);

            g[u].swap(nxt);
        }

    memset(clr, -1, (n + 1) << 2);
    invalid = 0;

    for (int rt = 1; rt <= n; ++rt)
        if (alive[rt] && clr[rt] == -1) {
            vec.clear();
            clr[rt] = 0;
            dfs(rt);

            if (invalid)
                break;

            if (!vec.size())
                continue;

            int u1 = vec[0], u2 = vec[1];
            int cnt = 0;

            for (int beg : g[u1]) {
                int cur = beg, lst = u1, len = 0;

                while (cur != u2) {
                    ++len;

                    if (g[cur][0] == lst)
                        swap(g[cur][0], g[cur][1]);

                    lst = cur;
                    cur = g[cur][0];
                }

                if (len == 1)
                    ++cnt;
            }

            if (cnt < 2) {
                invalid = 1;
                break;
            }
        }

    cout << (invalid ? "NO\n" : "YES\n");
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;

    while (T--)
        solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 3ms
memory: 5860kb

input:

10
2 1
1 2
3 1
1 2
3 1
1 3
3 3
2 1
1 3
2 3
3 2
1 2
2 3
3 2
1 2
1 3
3 3
3 2
1 3
2 1
1 0
2 0
3 0

output:

YES
YES
YES
NO
YES
YES
NO
YES
YES
YES

result:

ok 10 lines

Test #2:

score: 10
Accepted
time: 4ms
memory: 6208kb

input:

10
3 3
1 2
2 3
3 1
5 5
1 2
2 3
3 4
4 5
5 1
6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
6 6
1 2
2 3
3 4
4 5
5 6
6 1
4 4
1 2
2 3
3 4
4 1
6 8
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
6 7
1 3
1 4
1 5
2 3
2 4
2 5
4 6
6 5
1 2
1 3
1 4
1 5
1 6
6 6
1 2
2 3
3 1
1 4
2 5
3 6
5 6
1 2
1 3
1 4
2 5
3 5
4 5

output:

NO
NO
NO
YES
YES
NO
YES
YES
NO
YES

result:

ok 10 lines

Test #3:

score: 10
Accepted
time: 0ms
memory: 5860kb

input:

10
1000 1002
66 314
528 900
376 795
10 210
308 333
373 606
6 335
203 860
354 428
437 978
29 245
163 286
17 367
178 482
11 23
257 376
85 216
234 818
125 269
725 882
166 184
519 545
242 273
382 449
22 77
30 230
101 712
198 381
117 805
403 683
33 96
273 694
436 502
678 994
419 794
385 476
37 70
598 710...

output:

NO
NO
NO
YES
NO
YES
YES
YES
NO
YES

result:

ok 10 lines

Test #4:

score: 10
Accepted
time: 1ms
memory: 5876kb

input:

10
1000 1000
302 555
32 123
36 541
432 567
316 618
231 709
8 37
562 779
126 182
53 180
171 471
638 865
230 932
15 143
552 792
100 916
253 521
468 765
498 649
207 942
497 835
808 912
38 357
115 763
833 846
183 494
230 260
11 12
316 597
168 987
241 546
81 611
353 617
149 271
123 381
147 712
79 261
187...

output:

YES
YES
YES
NO
NO
YES
NO
NO
NO
YES

result:

ok 10 lines

Test #5:

score: 10
Accepted
time: 4ms
memory: 6288kb

input:

10
1000 1001
17 20
172 393
202 298
732 771
483 534
359 824
202 360
36 116
260 725
98 843
23 279
252 804
650 981
75 165
271 704
17 167
376 727
412 722
207 287
20 620
330 987
627 862
294 522
26 689
598 685
84 143
75 145
182 553
700 896
421 570
45 380
17 314
99 244
342 554
9 63
160 257
87 865
5 859
213...

output:

YES
YES
NO
NO
YES
YES
NO
NO
NO
YES

result:

ok 10 lines

Test #6:

score: 10
Accepted
time: 11ms
memory: 6868kb

input:

10
10000 10001
578 4967
2496 4305
5467 6762
285 1505
4083 4961
1021 1127
3547 6947
5207 6593
633 4454
1840 4156
2342 9282
2037 2729
1147 5075
1062 1614
3170 9021
2629 8481
3202 9340
2572 7713
2500 3006
78 994
3934 6287
3370 4736
1287 8184
1191 5456
1022 7705
3882 9076
2380 2846
2187 9356
2149 4841
6...

output:

YES
YES
YES
NO
NO
YES
NO
NO
NO
YES

result:

ok 10 lines

Test #7:

score: 10
Accepted
time: 21ms
memory: 6380kb

input:

10
10000 10000
1950 2793
581 9361
3214 4579
699 1271
9683 9787
644 2240
602 6117
1056 1583
5554 8586
400 2150
48 6952
335 9762
2491 4388
314 4611
3459 4391
70 6236
3273 4055
49 276
2557 3290
2309 4508
506 1792
1038 2464
1807 7113
2931 3657
2293 8994
1672 8112
845 1337
15 94
218 7705
5180 5969
4478 9...

output:

YES
YES
NO
NO
YES
YES
YES
NO
NO
NO

result:

ok 10 lines

Test #8:

score: 10
Accepted
time: 16ms
memory: 6276kb

input:

10
10000 9999
1117 2550
1285 1370
2873 3775
1651 2808
3461 8732
1297 9784
50 882
1984 3141
2933 4545
2700 8337
7574 9324
1646 6026
2274 7430
4348 7444
71 7455
8091 9534
764 841
1593 6353
1878 2046
4078 7791
3883 6027
302 7384
172 2504
6140 8052
1339 2290
3000 8455
7527 9988
3299 4368
6147 9702
2886 ...

output:

YES
YES
NO
NO
NO
YES
YES
NO
YES
NO

result:

ok 10 lines

Test #9:

score: 10
Accepted
time: 14ms
memory: 6640kb

input:

10
10000 10001
1458 4127
3728 4055
5932 6570
2612 3100
3462 5511
3196 9068
649 2575
5744 8570
3611 9272
2277 4443
599 7319
3938 6803
2049 3343
277 628
747 847
354 2645
1088 3474
712 756
1242 3289
2169 8555
1524 5865
3142 5688
341 348
308 1185
30 245
5802 6858
3248 7822
4365 6345
1340 4442
3241 6893
...

output:

NO
YES
NO
NO
NO
YES
NO
YES
YES
YES

result:

ok 10 lines

Test #10:

score: 10
Accepted
time: 23ms
memory: 6376kb

input:

10
10000 10001
2770 3818
315 6705
82 482
714 790
303 830
703 2085
2485 8025
539 2675
195 1862
3 22
384 883
669 6997
81 2653
1323 1711
303 1391
1247 8917
5344 6251
6746 7003
2404 6202
1422 4408
296 682
2206 2306
5424 7821
999 1143
3875 7755
1860 2358
2777 5395
1142 5823
4468 8320
8633 9014
1255 3699
...

output:

NO
NO
YES
YES
YES
NO
YES
NO
NO
YES

result:

ok 10 lines