Submission #299541

# Submission time Handle Problem Language Result Execution time Memory
299541 2020-09-15T07:12:50 Z E869120 Joker (BOI20_joker) C++14
39 / 100
2000 ms 28104 KB
#include <bits/stdc++.h>
using namespace std;

class UnionFind {
	public:
	vector<int> par;
	
	void init(int sz) {
		par.resize(sz, -1);
	}
	int root(int pos) {
		if (par[pos] == -1) return pos;
		par[pos] = root(par[pos]);
		return par[pos];
	}
	void unite(int u, int v) {
		u = root(u);
		v = root(v);
		if (u == v) return;
		par[u] = v;
	}
	bool same(int u, int v) {
		if (root(u) == root(v)) return true;
		return false;
	}
};

// Input
int N;
int M, A[1 << 18], B[1 << 18];
int Q, L[1 << 18], R[1 << 18];

// Graph
vector<int> X[1 << 18];
int col[1 << 18];
bool flag;

// Subtask3
vector<pair<int, int>> Y[1 << 18];
int lim[1 << 18];
bool used[1 << 18];

void henhari(int idx) {
	for (int i = 1; i <= N; i++) Y[i].clear();
	for (int i = 1; i <= idx; i++) {
		if (used[i] == false) continue;
		Y[A[i]].push_back(make_pair(B[i], 1000000000));
		Y[B[i]].push_back(make_pair(A[i], 1000000000));
	}
	for (int i = idx + 1; i <= M; i++) {
		if (used[i] == false) continue;
		Y[A[i]].push_back(make_pair(B[i], i));
		Y[B[i]].push_back(make_pair(A[i], i));
	}
}

void dfs2(int pos, int dep) {
	col[pos] = dep;
	for (pair<int, int> i : Y[pos]) {
		if (col[i.first] >= 0) continue;
		dfs2(i.first, dep ^ 1);
	}
}

void coloring() {
	for (int i = 1; i <= N; i++) col[i] = -1;
	for (int i = 1; i <= N; i++) {
		if (col[i] >= 0) continue;
		dfs2(i, 0);
	}
}

int dfs3(int pos, int to, int pre) {
	if (pos == to) return (1 << 30);
	int ret = 0;
	for (pair<int, int> i : Y[pos]) {
		if (i.first == pre) continue;
		int z = dfs3(i.first, to, pos);
		ret = max(ret, min(z, i.second));
	}
	return ret;
}

int getedge(int a, int b) {
	return dfs3(a, b, -1);
}

void solve_subtask3() {
	int maxL = 0;
	for (int i = 1; i <= Q; i++) maxL = max(maxL, L[i]);
	
	// Step #1. Maeshori
	UnionFind UF;
	UF.init(N + 2);
	for (int i = M; i >= 1; i--) {
		if (UF.same(A[i], B[i]) == true) continue;
		UF.unite(A[i], B[i]);
		used[i] = true;
	}
	
	// Step #2. Maeshori 2
	henhari(0);
	coloring();
	
	// Step #3. Get Answer
	for (int i = 1; i <= maxL; i++) {
		for (int j = i; j <= M; j++) {
			if (used[j] == false && col[A[j]] == col[B[j]]) lim[i] = j;
		}
		int v = getedge(A[i], B[i]);
		//cout << i << ": " << v << endl;
		used[v] = false;
		used[i] = true;
		henhari(i);
		coloring();
	}
	
	// Step #4. Output
	for (int i = 1; i <= Q; i++) {
		if (lim[L[i]] > R[i]) printf("YES\n");
		else printf("NO\n");
	}
}

void dfs(int pos, int dep) {
	col[pos] = dep;
	for (int i : X[pos]) {
		if (col[i] != -1) {
			if (col[i] == dep) flag = true;
			continue;
		}
		dfs(i, dep ^ 1);
	}
}

bool solve(int l, int r) {
	flag = false;
	for (int i = 1; i <= N; i++) X[i].clear();
	for (int i = 1; i <= M; i++) {
		if (l <= i && i <= r) continue;
		X[A[i]].push_back(B[i]);
		X[B[i]].push_back(A[i]);
	}
	for (int i = 1; i <= N; i++) col[i] = -1;
	for (int i = 1; i <= N; i++) {
		if (col[i] >= 0) continue;
		dfs(i, 0);
	}
	return flag;
}

void solve_subtask1() {
	for (int i = 1; i <= Q; i++) {
		bool ans = solve(L[i], R[i]);
		if (ans == false) printf("NO\n");
		else printf("YES\n");
	}
}

int main() {
	scanf("%d%d%d", &N, &M, &Q);
	for (int i = 1; i <= M; i++) scanf("%d%d", &A[i], &B[i]);
	for (int i = 1; i <= Q; i++) scanf("%d%d", &L[i], &R[i]);
	
	if (N <= 2000 && M <= 2000 && Q <= 2000) solve_subtask1();
	else solve_subtask3();
	return 0;
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:161:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  161 |  scanf("%d%d%d", &N, &M, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:162:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  162 |  for (int i = 1; i <= M; i++) scanf("%d%d", &A[i], &B[i]);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:163:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  163 |  for (int i = 1; i <= Q; i++) scanf("%d%d", &L[i], &R[i]);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 10 ms 12672 KB Output is correct
4 Correct 9 ms 12672 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 9 ms 12704 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 12 ms 12648 KB Output is correct
10 Correct 11 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 13 ms 12672 KB Output is correct
13 Correct 11 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12672 KB Output is correct
16 Correct 10 ms 12672 KB Output is correct
17 Correct 10 ms 12800 KB Output is correct
18 Correct 11 ms 12672 KB Output is correct
19 Correct 10 ms 12672 KB Output is correct
20 Correct 10 ms 12672 KB Output is correct
21 Correct 10 ms 12672 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 10 ms 12672 KB Output is correct
26 Correct 10 ms 12800 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 11 ms 12672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 10 ms 12672 KB Output is correct
4 Correct 9 ms 12672 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 9 ms 12704 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 12 ms 12648 KB Output is correct
10 Correct 11 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 13 ms 12672 KB Output is correct
13 Correct 11 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12672 KB Output is correct
16 Correct 10 ms 12672 KB Output is correct
17 Correct 10 ms 12800 KB Output is correct
18 Correct 11 ms 12672 KB Output is correct
19 Correct 10 ms 12672 KB Output is correct
20 Correct 10 ms 12672 KB Output is correct
21 Correct 10 ms 12672 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 10 ms 12672 KB Output is correct
26 Correct 10 ms 12800 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 11 ms 12672 KB Output is correct
29 Correct 114 ms 12800 KB Output is correct
30 Correct 158 ms 12800 KB Output is correct
31 Correct 153 ms 12800 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 108 ms 12800 KB Output is correct
34 Correct 163 ms 12800 KB Output is correct
35 Correct 170 ms 12800 KB Output is correct
36 Correct 75 ms 12800 KB Output is correct
37 Correct 143 ms 12800 KB Output is correct
38 Correct 146 ms 12800 KB Output is correct
39 Correct 128 ms 12800 KB Output is correct
40 Correct 113 ms 12800 KB Output is correct
41 Correct 114 ms 12800 KB Output is correct
42 Correct 111 ms 12800 KB Output is correct
43 Correct 135 ms 12800 KB Output is correct
44 Correct 155 ms 12800 KB Output is correct
45 Correct 180 ms 12800 KB Output is correct
46 Correct 192 ms 12800 KB Output is correct
47 Correct 108 ms 13048 KB Output is correct
48 Correct 119 ms 12800 KB Output is correct
49 Correct 136 ms 12800 KB Output is correct
50 Correct 161 ms 12800 KB Output is correct
51 Correct 114 ms 12920 KB Output is correct
52 Correct 126 ms 12800 KB Output is correct
53 Correct 142 ms 12800 KB Output is correct
54 Correct 170 ms 12800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 166 ms 28104 KB Output is correct
4 Correct 224 ms 28024 KB Output is correct
5 Correct 267 ms 25848 KB Output is correct
6 Correct 156 ms 22392 KB Output is correct
7 Correct 170 ms 22008 KB Output is correct
8 Correct 157 ms 20472 KB Output is correct
9 Correct 196 ms 22264 KB Output is correct
10 Correct 304 ms 25592 KB Output is correct
11 Correct 179 ms 22388 KB Output is correct
12 Correct 251 ms 25588 KB Output is correct
13 Correct 133 ms 18424 KB Output is correct
14 Correct 159 ms 20472 KB Output is correct
15 Correct 249 ms 23672 KB Output is correct
16 Correct 306 ms 25464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 10 ms 12672 KB Output is correct
4 Correct 9 ms 12672 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 9 ms 12704 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 12 ms 12648 KB Output is correct
10 Correct 11 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 13 ms 12672 KB Output is correct
13 Correct 11 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12672 KB Output is correct
16 Correct 10 ms 12672 KB Output is correct
17 Correct 10 ms 12800 KB Output is correct
18 Correct 11 ms 12672 KB Output is correct
19 Correct 10 ms 12672 KB Output is correct
20 Correct 10 ms 12672 KB Output is correct
21 Correct 10 ms 12672 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 10 ms 12672 KB Output is correct
26 Correct 10 ms 12800 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 11 ms 12672 KB Output is correct
29 Correct 166 ms 28104 KB Output is correct
30 Correct 224 ms 28024 KB Output is correct
31 Correct 267 ms 25848 KB Output is correct
32 Correct 156 ms 22392 KB Output is correct
33 Correct 170 ms 22008 KB Output is correct
34 Correct 157 ms 20472 KB Output is correct
35 Correct 196 ms 22264 KB Output is correct
36 Correct 304 ms 25592 KB Output is correct
37 Correct 179 ms 22388 KB Output is correct
38 Correct 251 ms 25588 KB Output is correct
39 Correct 133 ms 18424 KB Output is correct
40 Correct 159 ms 20472 KB Output is correct
41 Correct 249 ms 23672 KB Output is correct
42 Correct 306 ms 25464 KB Output is correct
43 Execution timed out 2052 ms 27096 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 10 ms 12672 KB Output is correct
4 Correct 9 ms 12672 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 9 ms 12704 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 12 ms 12648 KB Output is correct
10 Correct 11 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 13 ms 12672 KB Output is correct
13 Correct 11 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12672 KB Output is correct
16 Correct 10 ms 12672 KB Output is correct
17 Correct 10 ms 12800 KB Output is correct
18 Correct 11 ms 12672 KB Output is correct
19 Correct 10 ms 12672 KB Output is correct
20 Correct 10 ms 12672 KB Output is correct
21 Correct 10 ms 12672 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 10 ms 12672 KB Output is correct
26 Correct 10 ms 12800 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 11 ms 12672 KB Output is correct
29 Correct 114 ms 12800 KB Output is correct
30 Correct 158 ms 12800 KB Output is correct
31 Correct 153 ms 12800 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 108 ms 12800 KB Output is correct
34 Correct 163 ms 12800 KB Output is correct
35 Correct 170 ms 12800 KB Output is correct
36 Correct 75 ms 12800 KB Output is correct
37 Correct 143 ms 12800 KB Output is correct
38 Correct 146 ms 12800 KB Output is correct
39 Correct 128 ms 12800 KB Output is correct
40 Correct 113 ms 12800 KB Output is correct
41 Correct 114 ms 12800 KB Output is correct
42 Correct 111 ms 12800 KB Output is correct
43 Correct 135 ms 12800 KB Output is correct
44 Correct 155 ms 12800 KB Output is correct
45 Correct 180 ms 12800 KB Output is correct
46 Correct 192 ms 12800 KB Output is correct
47 Correct 108 ms 13048 KB Output is correct
48 Correct 119 ms 12800 KB Output is correct
49 Correct 136 ms 12800 KB Output is correct
50 Correct 161 ms 12800 KB Output is correct
51 Correct 114 ms 12920 KB Output is correct
52 Correct 126 ms 12800 KB Output is correct
53 Correct 142 ms 12800 KB Output is correct
54 Correct 170 ms 12800 KB Output is correct
55 Execution timed out 2055 ms 25604 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 10 ms 12672 KB Output is correct
4 Correct 9 ms 12672 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 9 ms 12704 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 12 ms 12648 KB Output is correct
10 Correct 11 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 13 ms 12672 KB Output is correct
13 Correct 11 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12672 KB Output is correct
16 Correct 10 ms 12672 KB Output is correct
17 Correct 10 ms 12800 KB Output is correct
18 Correct 11 ms 12672 KB Output is correct
19 Correct 10 ms 12672 KB Output is correct
20 Correct 10 ms 12672 KB Output is correct
21 Correct 10 ms 12672 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 10 ms 12672 KB Output is correct
26 Correct 10 ms 12800 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 11 ms 12672 KB Output is correct
29 Correct 114 ms 12800 KB Output is correct
30 Correct 158 ms 12800 KB Output is correct
31 Correct 153 ms 12800 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 108 ms 12800 KB Output is correct
34 Correct 163 ms 12800 KB Output is correct
35 Correct 170 ms 12800 KB Output is correct
36 Correct 75 ms 12800 KB Output is correct
37 Correct 143 ms 12800 KB Output is correct
38 Correct 146 ms 12800 KB Output is correct
39 Correct 128 ms 12800 KB Output is correct
40 Correct 113 ms 12800 KB Output is correct
41 Correct 114 ms 12800 KB Output is correct
42 Correct 111 ms 12800 KB Output is correct
43 Correct 135 ms 12800 KB Output is correct
44 Correct 155 ms 12800 KB Output is correct
45 Correct 180 ms 12800 KB Output is correct
46 Correct 192 ms 12800 KB Output is correct
47 Correct 108 ms 13048 KB Output is correct
48 Correct 119 ms 12800 KB Output is correct
49 Correct 136 ms 12800 KB Output is correct
50 Correct 161 ms 12800 KB Output is correct
51 Correct 114 ms 12920 KB Output is correct
52 Correct 126 ms 12800 KB Output is correct
53 Correct 142 ms 12800 KB Output is correct
54 Correct 170 ms 12800 KB Output is correct
55 Correct 166 ms 28104 KB Output is correct
56 Correct 224 ms 28024 KB Output is correct
57 Correct 267 ms 25848 KB Output is correct
58 Correct 156 ms 22392 KB Output is correct
59 Correct 170 ms 22008 KB Output is correct
60 Correct 157 ms 20472 KB Output is correct
61 Correct 196 ms 22264 KB Output is correct
62 Correct 304 ms 25592 KB Output is correct
63 Correct 179 ms 22388 KB Output is correct
64 Correct 251 ms 25588 KB Output is correct
65 Correct 133 ms 18424 KB Output is correct
66 Correct 159 ms 20472 KB Output is correct
67 Correct 249 ms 23672 KB Output is correct
68 Correct 306 ms 25464 KB Output is correct
69 Execution timed out 2052 ms 27096 KB Time limit exceeded
70 Halted 0 ms 0 KB -