답안 #369983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
369983 2021-02-22T20:36:52 Z luciocf Joker (BOI20_joker) C++14
100 / 100
293 ms 14004 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

typedef pair<int, int> pii;

const int maxn = 2e5+10;

struct DSU
{
	int pai[maxn], peso[maxn], edge[maxn];
	bool bp;

	struct RollBack
	{
		int u, v, pu;
		bool b;
	};

	stack<RollBack> stk;

	void init(int n)
	{
		bp = 1;
		for (int i = 0; i < maxn; i++)
			pai[i] = i, peso[i] = 1;
	}

	int Find(int x, int &c)
	{
		if (pai[x] == x) return x;

		c ^= edge[x];

		return Find(pai[x], c);
	}

	bool Join(int x, int y)
	{
		int cx = 0, cy = 0;
		x = Find(x, cx), y = Find(y, cy);

		if (peso[x] < peso[y])
		{
			swap(x, y);
			swap(cx, cy);
		}

		stk.push({x, y, peso[x], bp});

		if (x == y) return bp = (bp && (cx != cy));

		pai[y] = x, peso[x] += peso[y];
		edge[y] = cx ^ cy ^ 1;

		return 1;
	}

	void rollback(int x)
	{
		while (stk.size() > x)
		{
			auto E = stk.top(); stk.pop();

			pai[E.u] = E.u, pai[E.v] = E.v;
			peso[E.u] = E.pu, edge[E.v] = 0;
			bp = E.b;
		}
	}
} dsu;

int n, m;

int opt[maxn];
pii edge[maxn];

void solve(int l, int r)
{
	if (l > r) return;

	int mid = (l+r)>>1, s0 = dsu.stk.size();

	for (int i = l; i <= mid; i++)
		dsu.Join(edge[i].ff, edge[i].ss);

	int s1 = dsu.stk.size();

	if (!dsu.bp) opt[mid] = opt[r+1];
	else
	{
		for (int i = opt[r+1]; i >= max(opt[(l == 0 ? 0 : l-1)], mid+1); i--)
		{
			if (!dsu.Join(edge[i].ff, edge[i].ss))
			{
				opt[mid] = i;
				break;
			}
		}
	}

	dsu.rollback(s1);
	solve(mid+1, r);

	dsu.rollback(s0);
	for (int i = opt[r+1]; i > opt[mid]; i--)
		dsu.Join(edge[i].ff, edge[i].ss);

	solve(l, mid-1);

	dsu.rollback(s0);
}

int main(void)
{
	int q;
	scanf("%d %d %d", &n, &m, &q);

	edge[0] = {0, maxn-1};

	for (int i = 1; i <= m; i++)
		scanf("%d %d", &edge[i].ff, &edge[i].ss);
	
	edge[m+1] = {0, maxn-1};

	dsu.init(n);

	opt[0] = 1, opt[m+1] = m+1;
	solve(0, m);

	while (q--)
	{
		int l, r;
		scanf("%d %d", &l, &r);

		if (opt[l-1] > r) printf("YES\n");
		else printf("NO\n");
	}
}

Compilation message

Joker.cpp: In member function 'void DSU::rollback(int)':
Joker.cpp:64:21: warning: comparison of integer expressions of different signedness: 'std::stack<DSU::RollBack>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |   while (stk.size() > x)
      |          ~~~~~~~~~~~^~~
Joker.cpp: In function 'int main()':
Joker.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  124 |   scanf("%d %d", &edge[i].ff, &edge[i].ss);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:136:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  136 |   scanf("%d %d", &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 1 ms 1900 KB Output is correct
5 Correct 1 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 2 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1920 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 1 ms 1900 KB Output is correct
5 Correct 1 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 2 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1920 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1900 KB Output is correct
29 Correct 3 ms 2028 KB Output is correct
30 Correct 3 ms 2048 KB Output is correct
31 Correct 3 ms 2028 KB Output is correct
32 Correct 3 ms 2028 KB Output is correct
33 Correct 3 ms 2028 KB Output is correct
34 Correct 4 ms 2028 KB Output is correct
35 Correct 4 ms 2028 KB Output is correct
36 Correct 4 ms 2028 KB Output is correct
37 Correct 4 ms 2028 KB Output is correct
38 Correct 4 ms 2088 KB Output is correct
39 Correct 3 ms 2028 KB Output is correct
40 Correct 3 ms 2028 KB Output is correct
41 Correct 3 ms 2028 KB Output is correct
42 Correct 3 ms 2028 KB Output is correct
43 Correct 3 ms 2028 KB Output is correct
44 Correct 3 ms 2028 KB Output is correct
45 Correct 3 ms 2028 KB Output is correct
46 Correct 3 ms 2028 KB Output is correct
47 Correct 3 ms 2028 KB Output is correct
48 Correct 3 ms 2048 KB Output is correct
49 Correct 3 ms 2028 KB Output is correct
50 Correct 3 ms 2028 KB Output is correct
51 Correct 3 ms 2028 KB Output is correct
52 Correct 3 ms 2028 KB Output is correct
53 Correct 3 ms 2028 KB Output is correct
54 Correct 3 ms 2028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 182 ms 7264 KB Output is correct
4 Correct 237 ms 10988 KB Output is correct
5 Correct 152 ms 7644 KB Output is correct
6 Correct 189 ms 7264 KB Output is correct
7 Correct 185 ms 7348 KB Output is correct
8 Correct 223 ms 7284 KB Output is correct
9 Correct 215 ms 7388 KB Output is correct
10 Correct 263 ms 7900 KB Output is correct
11 Correct 201 ms 7260 KB Output is correct
12 Correct 276 ms 7660 KB Output is correct
13 Correct 202 ms 6892 KB Output is correct
14 Correct 206 ms 7232 KB Output is correct
15 Correct 255 ms 7392 KB Output is correct
16 Correct 283 ms 7796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 1 ms 1900 KB Output is correct
5 Correct 1 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 2 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1920 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1900 KB Output is correct
29 Correct 182 ms 7264 KB Output is correct
30 Correct 237 ms 10988 KB Output is correct
31 Correct 152 ms 7644 KB Output is correct
32 Correct 189 ms 7264 KB Output is correct
33 Correct 185 ms 7348 KB Output is correct
34 Correct 223 ms 7284 KB Output is correct
35 Correct 215 ms 7388 KB Output is correct
36 Correct 263 ms 7900 KB Output is correct
37 Correct 201 ms 7260 KB Output is correct
38 Correct 276 ms 7660 KB Output is correct
39 Correct 202 ms 6892 KB Output is correct
40 Correct 206 ms 7232 KB Output is correct
41 Correct 255 ms 7392 KB Output is correct
42 Correct 283 ms 7796 KB Output is correct
43 Correct 182 ms 11620 KB Output is correct
44 Correct 254 ms 13420 KB Output is correct
45 Correct 248 ms 13380 KB Output is correct
46 Correct 179 ms 11616 KB Output is correct
47 Correct 188 ms 11616 KB Output is correct
48 Correct 243 ms 11228 KB Output is correct
49 Correct 272 ms 11896 KB Output is correct
50 Correct 214 ms 11484 KB Output is correct
51 Correct 239 ms 11868 KB Output is correct
52 Correct 232 ms 11996 KB Output is correct
53 Correct 209 ms 11228 KB Output is correct
54 Correct 233 ms 11484 KB Output is correct
55 Correct 267 ms 11996 KB Output is correct
56 Correct 267 ms 11996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 1 ms 1900 KB Output is correct
5 Correct 1 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 2 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1920 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1900 KB Output is correct
29 Correct 3 ms 2028 KB Output is correct
30 Correct 3 ms 2048 KB Output is correct
31 Correct 3 ms 2028 KB Output is correct
32 Correct 3 ms 2028 KB Output is correct
33 Correct 3 ms 2028 KB Output is correct
34 Correct 4 ms 2028 KB Output is correct
35 Correct 4 ms 2028 KB Output is correct
36 Correct 4 ms 2028 KB Output is correct
37 Correct 4 ms 2028 KB Output is correct
38 Correct 4 ms 2088 KB Output is correct
39 Correct 3 ms 2028 KB Output is correct
40 Correct 3 ms 2028 KB Output is correct
41 Correct 3 ms 2028 KB Output is correct
42 Correct 3 ms 2028 KB Output is correct
43 Correct 3 ms 2028 KB Output is correct
44 Correct 3 ms 2028 KB Output is correct
45 Correct 3 ms 2028 KB Output is correct
46 Correct 3 ms 2028 KB Output is correct
47 Correct 3 ms 2028 KB Output is correct
48 Correct 3 ms 2048 KB Output is correct
49 Correct 3 ms 2028 KB Output is correct
50 Correct 3 ms 2028 KB Output is correct
51 Correct 3 ms 2028 KB Output is correct
52 Correct 3 ms 2028 KB Output is correct
53 Correct 3 ms 2028 KB Output is correct
54 Correct 3 ms 2028 KB Output is correct
55 Correct 130 ms 9580 KB Output is correct
56 Correct 198 ms 13548 KB Output is correct
57 Correct 108 ms 10164 KB Output is correct
58 Correct 132 ms 9580 KB Output is correct
59 Correct 182 ms 9580 KB Output is correct
60 Correct 241 ms 9964 KB Output is correct
61 Correct 168 ms 9580 KB Output is correct
62 Correct 174 ms 10024 KB Output is correct
63 Correct 152 ms 9196 KB Output is correct
64 Correct 196 ms 9708 KB Output is correct
65 Correct 230 ms 10092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1900 KB Output is correct
2 Correct 1 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 1 ms 1900 KB Output is correct
5 Correct 1 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 2 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1920 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1900 KB Output is correct
29 Correct 3 ms 2028 KB Output is correct
30 Correct 3 ms 2048 KB Output is correct
31 Correct 3 ms 2028 KB Output is correct
32 Correct 3 ms 2028 KB Output is correct
33 Correct 3 ms 2028 KB Output is correct
34 Correct 4 ms 2028 KB Output is correct
35 Correct 4 ms 2028 KB Output is correct
36 Correct 4 ms 2028 KB Output is correct
37 Correct 4 ms 2028 KB Output is correct
38 Correct 4 ms 2088 KB Output is correct
39 Correct 3 ms 2028 KB Output is correct
40 Correct 3 ms 2028 KB Output is correct
41 Correct 3 ms 2028 KB Output is correct
42 Correct 3 ms 2028 KB Output is correct
43 Correct 3 ms 2028 KB Output is correct
44 Correct 3 ms 2028 KB Output is correct
45 Correct 3 ms 2028 KB Output is correct
46 Correct 3 ms 2028 KB Output is correct
47 Correct 3 ms 2028 KB Output is correct
48 Correct 3 ms 2048 KB Output is correct
49 Correct 3 ms 2028 KB Output is correct
50 Correct 3 ms 2028 KB Output is correct
51 Correct 3 ms 2028 KB Output is correct
52 Correct 3 ms 2028 KB Output is correct
53 Correct 3 ms 2028 KB Output is correct
54 Correct 3 ms 2028 KB Output is correct
55 Correct 182 ms 7264 KB Output is correct
56 Correct 237 ms 10988 KB Output is correct
57 Correct 152 ms 7644 KB Output is correct
58 Correct 189 ms 7264 KB Output is correct
59 Correct 185 ms 7348 KB Output is correct
60 Correct 223 ms 7284 KB Output is correct
61 Correct 215 ms 7388 KB Output is correct
62 Correct 263 ms 7900 KB Output is correct
63 Correct 201 ms 7260 KB Output is correct
64 Correct 276 ms 7660 KB Output is correct
65 Correct 202 ms 6892 KB Output is correct
66 Correct 206 ms 7232 KB Output is correct
67 Correct 255 ms 7392 KB Output is correct
68 Correct 283 ms 7796 KB Output is correct
69 Correct 182 ms 11620 KB Output is correct
70 Correct 254 ms 13420 KB Output is correct
71 Correct 248 ms 13380 KB Output is correct
72 Correct 179 ms 11616 KB Output is correct
73 Correct 188 ms 11616 KB Output is correct
74 Correct 243 ms 11228 KB Output is correct
75 Correct 272 ms 11896 KB Output is correct
76 Correct 214 ms 11484 KB Output is correct
77 Correct 239 ms 11868 KB Output is correct
78 Correct 232 ms 11996 KB Output is correct
79 Correct 209 ms 11228 KB Output is correct
80 Correct 233 ms 11484 KB Output is correct
81 Correct 267 ms 11996 KB Output is correct
82 Correct 267 ms 11996 KB Output is correct
83 Correct 130 ms 9580 KB Output is correct
84 Correct 198 ms 13548 KB Output is correct
85 Correct 108 ms 10164 KB Output is correct
86 Correct 132 ms 9580 KB Output is correct
87 Correct 182 ms 9580 KB Output is correct
88 Correct 241 ms 9964 KB Output is correct
89 Correct 168 ms 9580 KB Output is correct
90 Correct 174 ms 10024 KB Output is correct
91 Correct 152 ms 9196 KB Output is correct
92 Correct 196 ms 9708 KB Output is correct
93 Correct 230 ms 10092 KB Output is correct
94 Correct 189 ms 12384 KB Output is correct
95 Correct 227 ms 14004 KB Output is correct
96 Correct 170 ms 12764 KB Output is correct
97 Correct 189 ms 12384 KB Output is correct
98 Correct 198 ms 12256 KB Output is correct
99 Correct 245 ms 12252 KB Output is correct
100 Correct 255 ms 12892 KB Output is correct
101 Correct 209 ms 12124 KB Output is correct
102 Correct 240 ms 12508 KB Output is correct
103 Correct 250 ms 12892 KB Output is correct
104 Correct 221 ms 12124 KB Output is correct
105 Correct 254 ms 12508 KB Output is correct
106 Correct 293 ms 12764 KB Output is correct
107 Correct 140 ms 13124 KB Output is correct
108 Correct 186 ms 12000 KB Output is correct
109 Correct 184 ms 12128 KB Output is correct
110 Correct 186 ms 12128 KB Output is correct
111 Correct 182 ms 12000 KB Output is correct
112 Correct 197 ms 12160 KB Output is correct
113 Correct 184 ms 12128 KB Output is correct
114 Correct 186 ms 12024 KB Output is correct
115 Correct 183 ms 12128 KB Output is correct
116 Correct 184 ms 12196 KB Output is correct