답안 #419275

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
419275 2021-06-06T17:44:10 Z volkyoe Alternating Current (BOI18_alternating) C++17
100 / 100
126 ms 11388 KB
#include"bits/stdc++.h"
using namespace std;
int N, M, x, y, Cnt[200005], D[100005];

int Two[100005], T, Vt[100005][2], V[100005];
vector<int> AdjList[100005];

int NT[100005], AC[100005], last[2];
pair<int, int> P[100005];

class Cmp {
	public:
		bool operator() (int i, int j) const {
			return P[i] < P[j];
		}
};
bool bipartite(int u) {
	queue<int> Q;
	Q.push(u);
	while (!Q.empty()) {
		u = Q.front(), Q.pop();
		for (int v : AdjList[u]) {
			if (!NT[v]) continue;
			if (AC[v] == AC[u]) return false;
			if (AC[v]) continue;	
			Q.push(v), AC[v] = -AC[u];
		}
	}
	return true;
}

int main() {
	scanf("%d%d", &N, &M);
	for (int i = 1; i <= M; i++) {
		scanf("%d%d", &x, &y);
		if (y < x) y += N;
		++Cnt[--x], --Cnt[y];
		P[i] = {x, y};
		D[i-1] = i;
	}
	
	for (int i = 1; i < 2*N; i++)
		Cnt[i] += Cnt[i-1];
	for (int i = 0; i < N; i++) {
		Cnt[i] += Cnt[i+N];
		//printf("Cnt[%d]: %d\n", i, Cnt[i]);
		if (Cnt[i] < 2) {
			printf("impossible");
			return 0;
		}
		if (Cnt[i] == 2) Two[T++] = i;
	}
	
	if (T > 0) {
		for (int i = 1; i <= M; i++) {
			tie(x, y) = P[i];
			int lb = (lower_bound(Two, Two + T, x) - Two) % T;
			int ub = (lower_bound(Two, Two + T, y % N) - Two) % T;
			//printf("(%d, %d) [%d, %d]\n", x, y, lb, ub);
			for (int j = lb; j != ub; j = (j + 1) % T)
				Vt[j][V[j]++] = i;
		}
		//for (int i = 0; i < T; i++) printf("[%d] %d %d\n", V[i], Vt[i][0], Vt[i][1]);
		
		for (int i = 0; i < T; i++) {
			AdjList[Vt[i][0]].push_back(Vt[i][1]);
			AdjList[Vt[i][1]].push_back(Vt[i][0]);
			NT[Vt[i][0]] = NT[Vt[i][1]] = 1;
		}
		//for (int i = 1; i <= M; i++) printf("%d ", NT[i]); printf("\n");
	}
	
	sort(D, D + M, Cmp());
	last[0] = last[1] = P[D[0]].first;
	for (int i = 0; i < M; i++) {
		int p = D[i];
		tie(x, y) = P[p];
		if (AC[p]) {
			int l = (AC[p] + 1) >> 1;
			last[l] = max(last[l], y);
			continue;
		}
		int l = last[0] < last[1] ? 0 : 1;
		AC[p] = l - !l;
		if (NT[p] && !bipartite(p)) {
			printf("impossible");
			return 0;
		}
		last[l] = max(last[l], y);
		//for (int m = 1; m <= M; m++) AC[m] ? printf("%d", (AC[m]+1) >> 1) : printf("-"); printf("\n");
	}
	
	for (int i = 1; i <= M; i++)
		printf("%d", 1 - ((AC[i] + 1) >> 1));
}
/*
12 4
1 9
4 12
7 3
10 6
*/

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
alternating.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2648 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2648 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 4 ms 2636 KB Output is correct
47 Correct 4 ms 2648 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 2 ms 2652 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 4 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 2 ms 2648 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 4 ms 2636 KB Output is correct
60 Correct 3 ms 2648 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2648 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2648 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 4 ms 2636 KB Output is correct
47 Correct 4 ms 2648 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 2 ms 2652 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 4 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 2 ms 2648 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 4 ms 2636 KB Output is correct
60 Correct 3 ms 2648 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2652 KB Output is correct
70 Correct 3 ms 2636 KB Output is correct
71 Correct 3 ms 2636 KB Output is correct
72 Correct 2 ms 2636 KB Output is correct
73 Correct 2 ms 2648 KB Output is correct
74 Correct 3 ms 2636 KB Output is correct
75 Correct 3 ms 2648 KB Output is correct
76 Correct 3 ms 2636 KB Output is correct
77 Correct 2 ms 2636 KB Output is correct
78 Correct 3 ms 2688 KB Output is correct
79 Correct 3 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 ms 2636 KB Output is correct
82 Correct 2 ms 2652 KB Output is correct
83 Correct 2 ms 2636 KB Output is correct
84 Correct 2 ms 2764 KB Output is correct
85 Correct 3 ms 2636 KB Output is correct
86 Correct 2 ms 2636 KB Output is correct
87 Correct 2 ms 2636 KB Output is correct
88 Correct 2 ms 2636 KB Output is correct
89 Correct 2 ms 2636 KB Output is correct
90 Correct 2 ms 2636 KB Output is correct
91 Correct 2 ms 2636 KB Output is correct
92 Correct 2 ms 2636 KB Output is correct
93 Correct 2 ms 2648 KB Output is correct
94 Correct 2 ms 2636 KB Output is correct
95 Correct 2 ms 2636 KB Output is correct
96 Correct 3 ms 2636 KB Output is correct
97 Correct 2 ms 2636 KB Output is correct
98 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5956 KB Output is correct
2 Correct 6 ms 4932 KB Output is correct
3 Correct 13 ms 4540 KB Output is correct
4 Correct 21 ms 4776 KB Output is correct
5 Correct 108 ms 11060 KB Output is correct
6 Correct 25 ms 5676 KB Output is correct
7 Correct 120 ms 10936 KB Output is correct
8 Correct 9 ms 5228 KB Output is correct
9 Correct 6 ms 4940 KB Output is correct
10 Correct 126 ms 11332 KB Output is correct
11 Correct 75 ms 9928 KB Output is correct
12 Correct 81 ms 11388 KB Output is correct
13 Correct 3 ms 3788 KB Output is correct
14 Correct 3 ms 3788 KB Output is correct
15 Correct 49 ms 6164 KB Output is correct
16 Correct 14 ms 4588 KB Output is correct
17 Correct 49 ms 6244 KB Output is correct
18 Correct 50 ms 5176 KB Output is correct
19 Correct 4 ms 3556 KB Output is correct
20 Correct 67 ms 10084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2648 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2648 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2648 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 4 ms 2636 KB Output is correct
47 Correct 4 ms 2648 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 2 ms 2652 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 4 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 2 ms 2648 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 4 ms 2636 KB Output is correct
60 Correct 3 ms 2648 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2652 KB Output is correct
70 Correct 3 ms 2636 KB Output is correct
71 Correct 3 ms 2636 KB Output is correct
72 Correct 2 ms 2636 KB Output is correct
73 Correct 2 ms 2648 KB Output is correct
74 Correct 3 ms 2636 KB Output is correct
75 Correct 3 ms 2648 KB Output is correct
76 Correct 3 ms 2636 KB Output is correct
77 Correct 2 ms 2636 KB Output is correct
78 Correct 3 ms 2688 KB Output is correct
79 Correct 3 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 ms 2636 KB Output is correct
82 Correct 2 ms 2652 KB Output is correct
83 Correct 2 ms 2636 KB Output is correct
84 Correct 2 ms 2764 KB Output is correct
85 Correct 3 ms 2636 KB Output is correct
86 Correct 2 ms 2636 KB Output is correct
87 Correct 2 ms 2636 KB Output is correct
88 Correct 2 ms 2636 KB Output is correct
89 Correct 2 ms 2636 KB Output is correct
90 Correct 2 ms 2636 KB Output is correct
91 Correct 2 ms 2636 KB Output is correct
92 Correct 2 ms 2636 KB Output is correct
93 Correct 2 ms 2648 KB Output is correct
94 Correct 2 ms 2636 KB Output is correct
95 Correct 2 ms 2636 KB Output is correct
96 Correct 3 ms 2636 KB Output is correct
97 Correct 2 ms 2636 KB Output is correct
98 Correct 2 ms 2636 KB Output is correct
99 Correct 34 ms 5956 KB Output is correct
100 Correct 6 ms 4932 KB Output is correct
101 Correct 13 ms 4540 KB Output is correct
102 Correct 21 ms 4776 KB Output is correct
103 Correct 108 ms 11060 KB Output is correct
104 Correct 25 ms 5676 KB Output is correct
105 Correct 120 ms 10936 KB Output is correct
106 Correct 9 ms 5228 KB Output is correct
107 Correct 6 ms 4940 KB Output is correct
108 Correct 126 ms 11332 KB Output is correct
109 Correct 75 ms 9928 KB Output is correct
110 Correct 81 ms 11388 KB Output is correct
111 Correct 3 ms 3788 KB Output is correct
112 Correct 3 ms 3788 KB Output is correct
113 Correct 49 ms 6164 KB Output is correct
114 Correct 14 ms 4588 KB Output is correct
115 Correct 49 ms 6244 KB Output is correct
116 Correct 50 ms 5176 KB Output is correct
117 Correct 4 ms 3556 KB Output is correct
118 Correct 67 ms 10084 KB Output is correct
119 Correct 7 ms 5964 KB Output is correct
120 Correct 71 ms 9532 KB Output is correct
121 Correct 49 ms 8140 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 4 ms 3532 KB Output is correct
124 Correct 14 ms 4300 KB Output is correct
125 Correct 32 ms 6664 KB Output is correct
126 Correct 7 ms 5580 KB Output is correct
127 Correct 12 ms 5068 KB Output is correct
128 Correct 78 ms 10180 KB Output is correct
129 Correct 50 ms 8480 KB Output is correct
130 Correct 65 ms 9012 KB Output is correct
131 Correct 8 ms 5928 KB Output is correct
132 Correct 8 ms 6092 KB Output is correct
133 Correct 50 ms 6196 KB Output is correct
134 Correct 15 ms 4204 KB Output is correct
135 Correct 3 ms 3532 KB Output is correct
136 Correct 15 ms 4748 KB Output is correct
137 Correct 27 ms 5192 KB Output is correct
138 Correct 4 ms 3404 KB Output is correct