QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#70792#5160. Kebab PizzaGreenscreen23TL 75ms11812kbC++173.1kb2023-01-08 00:38:112023-01-08 00:38:13

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-08 00:38:13]
  • 评测
  • 测评结果:TL
  • 用时:75ms
  • 内存:11812kb
  • [2023-01-08 00:38:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
#define rep(i, a, b) for(ll i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    ll n, k; cin >> n >> k;
    set<pair<ll, ll>> edges;
    rep(i, 0, n) {
        ll a, b; cin >> a >> b; a--; b--;

        if (a > b) swap(a, b);
        edges.emplace(a, b);
    }

    vector<ll> deg(k, 0);
    for(auto& [u, v] : edges) {
        if (u == v) continue;
        deg[u]++;
        deg[v]++;
    }

    vector<bool> required(k, false);
    vector<vector<ll>> adj(k);
    ll nodeWithEdge = -1;
    for(auto& [u, v]: edges) {
        if (u == v) {
            required[u] = true;
            continue;
        }

        if (deg[u] == 1 && !required[u]) {
            required[v] = true;
            deg[v]--;
            deg[u]--;
            continue;
        }

        if (deg[v] == 1 && !required[v]) {
            required[u] = true;
            deg[u]--;
            deg[v]--;
            continue;
        }

        adj[u].push_back(v);
        adj[v].push_back(u);
        nodeWithEdge = u;
    }
    if (nodeWithEdge == -1) {
        cout << "possible" << endl;
        return 0;
    }

    bool tryCircle = true;
    rep(i, 0, k) {
        if (sz(adj[i]) >= 3) {
            cout << "impossible" << endl;
            return 0;
        }
        if (required[i] && deg[i] == 0) {
            tryCircle = false;
        }
    }

    vector<bool> used(k, false);
    bool hasCircle = true;
    if (tryCircle) {
        // check connected
        queue<ll> todo;
        todo.push(nodeWithEdge);
        used[nodeWithEdge] = true;
        while(!todo.empty()) {
            ll v = todo.front();
            todo.pop();

            for(auto u : adj[v]) {
                if (used[u]) continue;

                todo.push(u);
                used[u] = true;
            }
        }

        rep(i, 0, k) {
            if (deg[i] == 1) hasCircle = false;
            if (deg[i] == 2 && !used[i]) hasCircle = false;
        }

        if (hasCircle) {
            cout << "possible" << endl;
            return 0;
        }
    }

    ll countDeg2 = 0;
    vector<ll> deg1Nodes;
    rep(i, 0, k) {
        if (deg[i] == 1) {
            deg1Nodes.push_back(i);
        }

        if (deg[i] == 2) countDeg2++;
    }

    if (countDeg2 == 0) {
        cout << "possible" << endl;
        return 0;
    }

    used.assign(k, false);
    for(auto deg1Node : deg1Nodes) {
        if (used[deg1Node]) continue;
        ll currNode = deg1Node;
        ll prevNode = -1;
        do {
            for(auto next : adj[currNode]) {
                if (next == prevNode) continue;
                prevNode = currNode;
                currNode = next;
                countDeg2--;
            }
        } while(sz(adj[currNode]) == 2);
        countDeg2++;
        if (countDeg2 == 0) {
            cout << "possible" << endl;
            return 0;
        }
        used[currNode] = true;
    }

    cout << "impossible" << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3500kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3300kb

input:

5 5
1 3
1 5
2 3
2 5
3 4

output:

possible

result:

ok single line: 'possible'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #4:

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

input:

8 4
1 1
1 2
2 1
2 2
3 3
3 4
4 3
4 4

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3504kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3352kb

input:

5 4
1 1
1 4
2 2
2 4
3 4

output:

possible

result:

ok single line: 'possible'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3500kb

input:

6 4
1 1
1 4
2 2
2 4
3 3
3 4

output:

impossible

result:

ok single line: 'impossible'

Test #8:

score: 0
Accepted
time: 2ms
memory: 3312kb

input:

4 5
1 2
3 4
4 5
5 3

output:

impossible

result:

ok single line: 'impossible'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3304kb

input:

4 4
1 1
2 3
3 4
4 2

output:

impossible

result:

ok single line: 'impossible'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3504kb

input:

3 4
1 2
2 3
3 1

output:

possible

result:

ok single line: 'possible'

Test #11:

score: 0
Accepted
time: 2ms
memory: 3500kb

input:

4 3
1 2
2 3
3 1
1 2

output:

possible

result:

ok single line: 'possible'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3372kb

input:

5 4
1 2
2 3
3 1
1 4
4 4

output:

impossible

result:

ok single line: 'impossible'

Test #13:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

4 3
1 2
2 3
3 1
1 1

output:

possible

result:

ok single line: 'possible'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3400kb

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3304kb

input:

4 6
1 2
2 3
4 5
5 6

output:

possible

result:

ok single line: 'possible'

Test #16:

score: 0
Accepted
time: 2ms
memory: 3496kb

input:

4 8
1 2
3 4
5 6
7 8

output:

possible

result:

ok single line: 'possible'

Test #17:

score: 0
Accepted
time: 2ms
memory: 3456kb

input:

3 3
1 2
2 3
1 2

output:

possible

result:

ok single line: 'possible'

Test #18:

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

input:

13 11
11 7
8 6
4 8
1 2
6 7
6 2
2 9
3 8
11 8
6 11
8 2
9 1
9 11

output:

impossible

result:

ok single line: 'impossible'

Test #19:

score: 0
Accepted
time: 2ms
memory: 3648kb

input:

1635 131
40 13
22 72
98 70
81 118
124 122
90 12
24 5
86 61
45 75
91 80
14 1
28 74
84 27
120 83
75 117
44 130
127 38
58 64
22 17
12 48
107 87
37 131
41 15
11 48
5 46
127 50
123 75
43 124
93 5
17 83
26 130
66 122
55 105
36 119
116 49
98 89
73 86
119 99
16 24
39 90
33 72
94 22
19 13
101 9
116 8
33 95
8...

output:

impossible

result:

ok single line: 'impossible'

Test #20:

score: 0
Accepted
time: 2ms
memory: 3664kb

input:

1803 1766
967 1468
1305 1669
617 36
1564 714
53 1045
1536 80
467 373
375 1173
1750 210
1433 81
667 798
1345 825
274 85
1107 1425
1576 560
30 405
1324 129
612 332
506 1708
87 1587
337 891
503 786
1140 304
1439 966
841 234
1270 696
1557 1607
763 1422
196 461
1485 557
1509 44
511 1050
623 1587
687 1758...

output:

impossible

result:

ok single line: 'impossible'

Test #21:

score: 0
Accepted
time: 22ms
memory: 8540kb

input:

39505 78410
3175 40148
52846 78310
27128 59546
19323 44017
2534 68741
70302 1985
11003 6218
54155 11195
34079 21458
44804 11586
56941 46235
76524 20594
62563 17285
45626 5887
42683 63518
60306 151
25930 34002
19196 26270
3757 34394
60466 29619
32474 33236
59760 26098
11678 74638
51383 12205
16010 45...

output:

impossible

result:

ok single line: 'impossible'

Test #22:

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

input:

8364 43972
17102 14140
29056 23018
21282 40801
16760 27492
17370 13743
20137 31183
37298 39871
39916 31212
2472 25458
10766 38742
27020 14591
40426 32321
8831 30814
19561 2976
30423 21646
21760 2454
19103 43674
38828 43679
42838 37929
24460 3928
24227 35224
11041 27999
43445 11000
29465 16355
2591 2...

output:

impossible

result:

ok single line: 'impossible'

Test #23:

score: 0
Accepted
time: 55ms
memory: 10388kb

input:

62138 58240
56929 31261
38519 49289
3011 27909
9851 14304
38929 33991
48684 39971
18479 4049
22461 52271
20936 40010
7797 20153
44481 10183
14006 35620
30917 829
40099 53767
8041 16634
52764 57279
30667 3334
15542 23939
19932 25368
53529 51386
9452 45573
55315 41467
39172 3995
43732 4915
21116 28286...

output:

impossible

result:

ok single line: 'impossible'

Test #24:

score: 0
Accepted
time: 75ms
memory: 11812kb

input:

96147 6603
5405 3128
1267 2523
352 4758
3190 3000
1108 3762
2150 3562
3704 1748
5120 5883
1240 1219
731 6400
2477 6176
3583 474
562 3046
2864 730
1048 3532
4002 1102
3424 1197
3034 478
3828 1494
5543 5660
6478 4998
1650 2397
549 2073
3667 1440
266 4790
2118 4951
3993 3619
3383 6127
3748 6174
416 757...

output:

impossible

result:

ok single line: 'impossible'

Test #25:

score: 0
Accepted
time: 35ms
memory: 10120kb

input:

59974 62084
32353 28539
59280 13133
49252 34406
61223 21059
42029 57591
29291 9619
43338 58447
43639 4549
52967 11881
57304 46567
51189 42869
38788 9223
40539 8837
48395 28893
8002 40786
8531 48490
59796 34964
1120 1788
3282 26976
9492 14840
26847 20287
5078 50996
26323 42319
46119 339
38541 32471
2...

output:

impossible

result:

ok single line: 'impossible'

Test #26:

score: 0
Accepted
time: 48ms
memory: 10016kb

input:

66303 31218
11606 28363
6297 26839
14830 4154
7429 23837
8049 26652
19828 30249
25628 29398
25551 16332
9419 1805
22735 10642
26312 15045
22346 29624
28230 24039
5367 3575
12948 20105
22260 4125
20861 4699
19171 10016
14977 6636
29361 30501
2793 25021
17148 23728
623 20838
595 6709
26966 23795
15754...

output:

impossible

result:

ok single line: 'impossible'

Test #27:

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

input:

6 5
3 5
1 4
1 5
1 5
3 5
1 4

output:

possible

result:

ok single line: 'possible'

Test #28:

score: 0
Accepted
time: 2ms
memory: 3424kb

input:

57 33
29 3
3 3
15 3
3 15
10 9
9 10
9 3
3 29
9 10
9 9
15 3
3 29
10 9
3 3
3 3
9 9
9 10
3 3
9 16
9 9
3 3
10 9
15 3
3 3
9 9
9 16
9 3
3 3
9 3
3 3
3 3
3 29
3 9
3 15
3 3
9 9
3 3
3 3
3 3
3 3
3 29
3 3
16 9
16 9
3 3
29 3
9 9
3 15
3 3
3 3
3 3
9 16
29 3
3 15
9 10
29 3
3 15

output:

possible

result:

ok single line: 'possible'

Test #29:

score: -100
Time Limit Exceeded

input:

749 5820
4064 4001
100 3402
2357 1581
5035 706
3879 3357
123 4811
903 4490
1526 3240
2789 2743
5665 4251
4431 5665
2709 1238
521 5759
4251 44
2186 3391
3568 1309
3737 3145
268 2671
1404 2625
776 2307
2302 5521
4568 4568
85 4427
75 3592
5417 829
5723 3057
4276 2016
4602 1713
3910 819
3832 4519
2330 8...

output:


result: