답안 #299551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
299551 2020-09-15T07:22:03 Z E869120 Joker (BOI20_joker) C++14
39 / 100
2000 ms 27256 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];
bool reached[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) {
	if (pos == to) return (1 << 30);
	reached[pos] = true;
	int ret = 0;
	for (pair<int, int> i : Y[pos]) {
		if (reached[i.first] == true) continue;
		int z = dfs3(i.first, to);
		ret = max(ret, min(z, i.second));
	}
	return ret;
}

int getedge(int a, int b) {
	for (int i = 1; i <= N; i++) reached[i] = false;
	int x = dfs3(a, b);
	return x;
}

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]);
		if (v >= (1 << 27)) continue;
		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:165:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  165 |  scanf("%d%d%d", &N, &M, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:166:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  166 |  for (int i = 1; i <= M; i++) scanf("%d%d", &A[i], &B[i]);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:167:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  167 |  for (int i = 1; i <= Q; i++) scanf("%d%d", &L[i], &R[i]);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 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 10 ms 12672 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 11 ms 12672 KB Output is correct
9 Correct 10 ms 12672 KB Output is correct
10 Correct 10 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 11 ms 12704 KB Output is correct
13 Correct 10 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 12672 KB Output is correct
18 Correct 10 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 10 ms 12672 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 11 ms 12672 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 10 ms 12800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 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 10 ms 12672 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 11 ms 12672 KB Output is correct
9 Correct 10 ms 12672 KB Output is correct
10 Correct 10 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 11 ms 12704 KB Output is correct
13 Correct 10 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 12672 KB Output is correct
18 Correct 10 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 10 ms 12672 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 11 ms 12672 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 10 ms 12800 KB Output is correct
29 Correct 111 ms 12792 KB Output is correct
30 Correct 161 ms 12800 KB Output is correct
31 Correct 152 ms 12920 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 106 ms 12800 KB Output is correct
34 Correct 161 ms 12800 KB Output is correct
35 Correct 167 ms 12928 KB Output is correct
36 Correct 71 ms 12800 KB Output is correct
37 Correct 137 ms 12928 KB Output is correct
38 Correct 146 ms 12960 KB Output is correct
39 Correct 124 ms 12800 KB Output is correct
40 Correct 111 ms 12920 KB Output is correct
41 Correct 110 ms 12920 KB Output is correct
42 Correct 111 ms 12920 KB Output is correct
43 Correct 125 ms 13048 KB Output is correct
44 Correct 154 ms 12800 KB Output is correct
45 Correct 174 ms 12924 KB Output is correct
46 Correct 188 ms 12964 KB Output is correct
47 Correct 108 ms 12928 KB Output is correct
48 Correct 117 ms 12920 KB Output is correct
49 Correct 135 ms 12800 KB Output is correct
50 Correct 157 ms 12800 KB Output is correct
51 Correct 113 ms 12800 KB Output is correct
52 Correct 133 ms 12800 KB Output is correct
53 Correct 139 ms 12832 KB Output is correct
54 Correct 168 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 161 ms 25336 KB Output is correct
4 Correct 211 ms 27256 KB Output is correct
5 Correct 267 ms 24952 KB Output is correct
6 Correct 157 ms 21604 KB Output is correct
7 Correct 171 ms 21112 KB Output is correct
8 Correct 149 ms 19576 KB Output is correct
9 Correct 194 ms 21368 KB Output is correct
10 Correct 301 ms 24824 KB Output is correct
11 Correct 182 ms 21364 KB Output is correct
12 Correct 243 ms 24688 KB Output is correct
13 Correct 136 ms 17400 KB Output is correct
14 Correct 155 ms 19448 KB Output is correct
15 Correct 246 ms 22776 KB Output is correct
16 Correct 300 ms 24844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 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 10 ms 12672 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 11 ms 12672 KB Output is correct
9 Correct 10 ms 12672 KB Output is correct
10 Correct 10 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 11 ms 12704 KB Output is correct
13 Correct 10 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 12672 KB Output is correct
18 Correct 10 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 10 ms 12672 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 11 ms 12672 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 10 ms 12800 KB Output is correct
29 Correct 161 ms 25336 KB Output is correct
30 Correct 211 ms 27256 KB Output is correct
31 Correct 267 ms 24952 KB Output is correct
32 Correct 157 ms 21604 KB Output is correct
33 Correct 171 ms 21112 KB Output is correct
34 Correct 149 ms 19576 KB Output is correct
35 Correct 194 ms 21368 KB Output is correct
36 Correct 301 ms 24824 KB Output is correct
37 Correct 182 ms 21364 KB Output is correct
38 Correct 243 ms 24688 KB Output is correct
39 Correct 136 ms 17400 KB Output is correct
40 Correct 155 ms 19448 KB Output is correct
41 Correct 246 ms 22776 KB Output is correct
42 Correct 300 ms 24844 KB Output is correct
43 Execution timed out 2085 ms 24928 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 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 10 ms 12672 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 11 ms 12672 KB Output is correct
9 Correct 10 ms 12672 KB Output is correct
10 Correct 10 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 11 ms 12704 KB Output is correct
13 Correct 10 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 12672 KB Output is correct
18 Correct 10 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 10 ms 12672 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 11 ms 12672 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 10 ms 12800 KB Output is correct
29 Correct 111 ms 12792 KB Output is correct
30 Correct 161 ms 12800 KB Output is correct
31 Correct 152 ms 12920 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 106 ms 12800 KB Output is correct
34 Correct 161 ms 12800 KB Output is correct
35 Correct 167 ms 12928 KB Output is correct
36 Correct 71 ms 12800 KB Output is correct
37 Correct 137 ms 12928 KB Output is correct
38 Correct 146 ms 12960 KB Output is correct
39 Correct 124 ms 12800 KB Output is correct
40 Correct 111 ms 12920 KB Output is correct
41 Correct 110 ms 12920 KB Output is correct
42 Correct 111 ms 12920 KB Output is correct
43 Correct 125 ms 13048 KB Output is correct
44 Correct 154 ms 12800 KB Output is correct
45 Correct 174 ms 12924 KB Output is correct
46 Correct 188 ms 12964 KB Output is correct
47 Correct 108 ms 12928 KB Output is correct
48 Correct 117 ms 12920 KB Output is correct
49 Correct 135 ms 12800 KB Output is correct
50 Correct 157 ms 12800 KB Output is correct
51 Correct 113 ms 12800 KB Output is correct
52 Correct 133 ms 12800 KB Output is correct
53 Correct 139 ms 12832 KB Output is correct
54 Correct 168 ms 12920 KB Output is correct
55 Execution timed out 2065 ms 23496 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 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 10 ms 12672 KB Output is correct
7 Correct 10 ms 12672 KB Output is correct
8 Correct 11 ms 12672 KB Output is correct
9 Correct 10 ms 12672 KB Output is correct
10 Correct 10 ms 12672 KB Output is correct
11 Correct 10 ms 12672 KB Output is correct
12 Correct 11 ms 12704 KB Output is correct
13 Correct 10 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 12672 KB Output is correct
18 Correct 10 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 10 ms 12672 KB Output is correct
23 Correct 10 ms 12672 KB Output is correct
24 Correct 10 ms 12672 KB Output is correct
25 Correct 11 ms 12672 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12672 KB Output is correct
28 Correct 10 ms 12800 KB Output is correct
29 Correct 111 ms 12792 KB Output is correct
30 Correct 161 ms 12800 KB Output is correct
31 Correct 152 ms 12920 KB Output is correct
32 Correct 124 ms 12800 KB Output is correct
33 Correct 106 ms 12800 KB Output is correct
34 Correct 161 ms 12800 KB Output is correct
35 Correct 167 ms 12928 KB Output is correct
36 Correct 71 ms 12800 KB Output is correct
37 Correct 137 ms 12928 KB Output is correct
38 Correct 146 ms 12960 KB Output is correct
39 Correct 124 ms 12800 KB Output is correct
40 Correct 111 ms 12920 KB Output is correct
41 Correct 110 ms 12920 KB Output is correct
42 Correct 111 ms 12920 KB Output is correct
43 Correct 125 ms 13048 KB Output is correct
44 Correct 154 ms 12800 KB Output is correct
45 Correct 174 ms 12924 KB Output is correct
46 Correct 188 ms 12964 KB Output is correct
47 Correct 108 ms 12928 KB Output is correct
48 Correct 117 ms 12920 KB Output is correct
49 Correct 135 ms 12800 KB Output is correct
50 Correct 157 ms 12800 KB Output is correct
51 Correct 113 ms 12800 KB Output is correct
52 Correct 133 ms 12800 KB Output is correct
53 Correct 139 ms 12832 KB Output is correct
54 Correct 168 ms 12920 KB Output is correct
55 Correct 161 ms 25336 KB Output is correct
56 Correct 211 ms 27256 KB Output is correct
57 Correct 267 ms 24952 KB Output is correct
58 Correct 157 ms 21604 KB Output is correct
59 Correct 171 ms 21112 KB Output is correct
60 Correct 149 ms 19576 KB Output is correct
61 Correct 194 ms 21368 KB Output is correct
62 Correct 301 ms 24824 KB Output is correct
63 Correct 182 ms 21364 KB Output is correct
64 Correct 243 ms 24688 KB Output is correct
65 Correct 136 ms 17400 KB Output is correct
66 Correct 155 ms 19448 KB Output is correct
67 Correct 246 ms 22776 KB Output is correct
68 Correct 300 ms 24844 KB Output is correct
69 Execution timed out 2085 ms 24928 KB Time limit exceeded
70 Halted 0 ms 0 KB -