답안 #441522

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441522 2021-07-05T09:54:08 Z abc864197532 Alternating Current (BOI18_alternating) C++17
74 / 100
40 ms 5196 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 100000;

int main () {
    srand(time(NULL));
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector <pii> a(m);
    vector <int> seg[n];
    if (n > 1000) {
        int mx1 = 0, mx2 = 0;
        for (int i = 0; i < m; ++i) cin >> a[i].X >> a[i].Y, --a[i].X;
        vector <int> ans(m, 0);
        vector <int> p(m);
        iota(all(p), 0);
        sort(all(p), [&](int i, int j) {
            return a[i].X < a[j].X;
        });
        for (int i : p) {
            if (mx1 < a[i].X || mx2 < a[i].X) break;
            if (mx1 < mx2) {
                mx1 = max(mx1, a[i].Y);
            } else {
                mx2 = max(mx2, a[i].Y);
                ans[i] = 1;
            }
        }
        if (mx1 < n || mx2 < n) return cout << "impossible\n", 0;
        for (int i : ans) cout << i;
        cout << endl;
        return 0;
    }
    for (int i = 0, l, r; i < m; ++i) {
        cin >> a[i].X >> a[i].Y, --a[i].X;
        tie(l, r) = a[i];
        if (l < r) {
            for (int j = l; j < r; ++j) seg[j].pb(i);
        } else {
            for (int j = l; j < n; ++j) seg[j].pb(i);
            for (int j = 0; j < r; ++j) seg[j].pb(i);
        }
    }
    vector <vector <int>> adj(m);
    for (int i = 0; i < n; ++i) {
        if (seg[i].size() < 2) return cout << "impossible\n", 0;
        if (seg[i].size() == 2) adj[seg[i][0]].pb(seg[i][1]), adj[seg[i][1]].pb(seg[i][0]);
    }
    vector <int> ans(m, -1);
    bool odd_cycle = false;
    function<void(int)> dfs = [&](int v) {
        for (int u : adj[v]) {
            if (ans[u] == -1) {
                ans[u] = ans[v] ^ 1;
                dfs(u);
            } else if (ans[v] == ans[u]) {
                odd_cycle = true;
            }
        }
    };
    for (int i = 0; i < m; ++i) if (ans[i] == -1 && !adj[i].empty()) {
        ans[i] = 0;
        dfs(i);
    }
    if (odd_cycle) return cout << "impossible\n", 0;
    for (int i = 0; i < n; ++i) {
        int ok = 0;
        for (int j : seg[i]) {
            if (ans[j] == -1) continue;
            ok |= 1 << ans[j];
        }
        if (ok == 0) {
            ans[seg[i][0]] = 0;
            ans[seg[i][1]] = 1;
        } else if (ok != 3) {
            for (int j : seg[i]) {
                if (ans[j] != -1) continue;
                ans[j] = ok == 2 ? 0 : 1;
                break;
            }
        }
    }
    for (int i : ans) {
        cout << (i == -1 ? 0 : i);
    }
    cout << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 0 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 0 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 1 ms 284 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 0 ms 204 KB Output is correct
68 Correct 0 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 0 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 0 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 1 ms 284 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 0 ms 204 KB Output is correct
68 Correct 0 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 9 ms 4428 KB Output is correct
71 Correct 1 ms 460 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 1 ms 332 KB Output is correct
74 Correct 1 ms 332 KB Output is correct
75 Correct 0 ms 204 KB Output is correct
76 Correct 3 ms 1612 KB Output is correct
77 Correct 4 ms 1612 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 1 ms 332 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 332 KB Output is correct
86 Correct 1 ms 332 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 1 ms 332 KB Output is correct
89 Correct 3 ms 1612 KB Output is correct
90 Correct 5 ms 2764 KB Output is correct
91 Correct 1 ms 588 KB Output is correct
92 Correct 1 ms 332 KB Output is correct
93 Correct 1 ms 332 KB Output is correct
94 Correct 0 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 460 KB Output is correct
97 Correct 1 ms 460 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 5196 KB Output is correct
2 Correct 2 ms 2616 KB Output is correct
3 Correct 13 ms 3916 KB Output is correct
4 Correct 16 ms 4068 KB Output is correct
5 Correct 39 ms 4932 KB Output is correct
6 Correct 34 ms 4860 KB Output is correct
7 Correct 36 ms 4828 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 2 ms 2496 KB Output is correct
10 Correct 40 ms 5036 KB Output is correct
11 Correct 30 ms 4528 KB Output is correct
12 Correct 36 ms 4828 KB Output is correct
13 Correct 2 ms 2660 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 39 ms 5016 KB Output is correct
16 Correct 14 ms 3940 KB Output is correct
17 Correct 40 ms 4944 KB Output is correct
18 Correct 35 ms 2756 KB Output is correct
19 Correct 4 ms 2764 KB Output is correct
20 Correct 39 ms 3780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 0 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 0 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 1 ms 284 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 0 ms 204 KB Output is correct
68 Correct 0 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 9 ms 4428 KB Output is correct
71 Correct 1 ms 460 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 1 ms 332 KB Output is correct
74 Correct 1 ms 332 KB Output is correct
75 Correct 0 ms 204 KB Output is correct
76 Correct 3 ms 1612 KB Output is correct
77 Correct 4 ms 1612 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 1 ms 332 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 332 KB Output is correct
86 Correct 1 ms 332 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 1 ms 332 KB Output is correct
89 Correct 3 ms 1612 KB Output is correct
90 Correct 5 ms 2764 KB Output is correct
91 Correct 1 ms 588 KB Output is correct
92 Correct 1 ms 332 KB Output is correct
93 Correct 1 ms 332 KB Output is correct
94 Correct 0 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 460 KB Output is correct
97 Correct 1 ms 460 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 28 ms 5196 KB Output is correct
100 Correct 2 ms 2616 KB Output is correct
101 Correct 13 ms 3916 KB Output is correct
102 Correct 16 ms 4068 KB Output is correct
103 Correct 39 ms 4932 KB Output is correct
104 Correct 34 ms 4860 KB Output is correct
105 Correct 36 ms 4828 KB Output is correct
106 Correct 3 ms 2636 KB Output is correct
107 Correct 2 ms 2496 KB Output is correct
108 Correct 40 ms 5036 KB Output is correct
109 Correct 30 ms 4528 KB Output is correct
110 Correct 36 ms 4828 KB Output is correct
111 Correct 2 ms 2660 KB Output is correct
112 Correct 2 ms 2636 KB Output is correct
113 Correct 39 ms 5016 KB Output is correct
114 Correct 14 ms 3940 KB Output is correct
115 Correct 40 ms 4944 KB Output is correct
116 Correct 35 ms 2756 KB Output is correct
117 Correct 4 ms 2764 KB Output is correct
118 Correct 39 ms 3780 KB Output is correct
119 Incorrect 2 ms 2636 KB 'impossible' claimed, but there is a solution
120 Halted 0 ms 0 KB -