답안 #256180

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256180 2020-08-02T11:00:51 Z 구재현(#5035) Joker (BOI20_joker) C++17
100 / 100
596 ms 73416 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int MAXN = 400005;

struct event{
	int *p;
	int v;
};

int n, m, q;
pi a[MAXN];

void rollback(vector<event> &E){
	reverse(all(E));
	for(auto &i : E){
		*i.p = i.v;
	}
	E.clear();
}

struct btracker{
	int pa[MAXN], rk[MAXN];
	int is_cycle;
	void init(int n){
		iota(pa, pa + n + 1, 0);
		memset(rk, 0, sizeof(rk));
		is_cycle = 0;
	}
	int find(int x){
		return pa[x] == x ? x : find(pa[x]);
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		if(rk[p] > rk[q]) swap(p, q);
		pa[p] = q;
		if(rk[p] == rk[q]) rk[q]++;
		return 1;
	}
	bool uni(int p, int q, vector<event> &snap){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		if(rk[p] > rk[q]) swap(p, q);
		snap.push_back((event){&pa[p], pa[p]});
		pa[p] = q;
		if(rk[p] == rk[q]){
			snap.push_back((event){&rk[q], rk[q]});
			rk[q]++;
		}
		return 1;
	}
	void add_edge(int x, int y){
		uni(x, y + n);
		uni(y, x + n);
		if(find(x) == find(x + n)) is_cycle = 1;
	}
	void add_edge(int x, int y, vector<event> &snap){
		uni(x, y + n, snap);
		uni(y, x + n, snap);
		if(find(x) == find(x + n)){
			snap.push_back((event){&is_cycle, is_cycle});
			is_cycle = 1;
		}
	}
}disj;

int dap[MAXN];

void solve(int s, int e, int ps, int pe){
	if(s > e) return;
	int m = (s + e) / 2;
	vector<event> snap1, snap2;
	for(int i = e; i >= m; i--){
		disj.add_edge(a[i].first, a[i].second, snap1);
	}
	for(int i = ps; i <= pe; i++){
		disj.add_edge(a[i].first, a[i].second, snap2);
		if(disj.is_cycle){
			dap[m] = i;
			break;
		}
	}
	rollback(snap2);
	solve(s, m - 1, ps, dap[m]);
	rollback(snap1);
	for(int i = ps; i <= dap[m] - 1; i++){
		disj.add_edge(a[i].first, a[i].second, snap1);
	}
	solve(m + 1, e, dap[m], pe);
	rollback(snap1);
}

int main(){
	scanf("%d %d %d",&n,&m,&q);
	for(int i=1; i<=m; i++){
		scanf("%d %d",&a[i].first,&a[i].second);
		a[i + m] = a[i];
	}
	disj.init(n+n);
	int stpos = -1;
	for(int i=m; i; i--){
		disj.add_edge(a[i].first, a[i].second);
		if(disj.is_cycle){
			stpos = i + 1;
			break;
		}
	}
	if(disj.is_cycle == 0){
		while(q--) puts("NO");
		return 0;
	}
	disj.init(n+n);
	{
		vector<event> v;
		dap[m + 1] = 2 * m + 1;
		for(int j=m + 1; j<=2*m; j++){
			disj.add_edge(a[j].first, a[j].second, v);
			if(disj.is_cycle){
				dap[m + 1] = j;
				break;
			}
		}
		rollback(v);
	}
	solve(stpos, m, m + 1, dap[m + 1]);
	while(q--){
		int x, y; scanf("%d %d",&x,&y);
		if(y + 1 < stpos) puts("YES");
		else puts(dap[y + 1] < x + m? "YES" : "NO");
	}
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&n,&m,&q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
Joker.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a[i].first,&a[i].second);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:134:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y; scanf("%d %d",&x,&y);
             ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 1 ms 1920 KB Output is correct
4 Correct 2 ms 1920 KB Output is correct
5 Correct 1 ms 1920 KB Output is correct
6 Correct 1 ms 1920 KB Output is correct
7 Correct 1 ms 1920 KB Output is correct
8 Correct 2 ms 1920 KB Output is correct
9 Correct 1 ms 1920 KB Output is correct
10 Correct 1 ms 1920 KB Output is correct
11 Correct 1 ms 1920 KB Output is correct
12 Correct 2 ms 2048 KB Output is correct
13 Correct 1 ms 1920 KB Output is correct
14 Correct 1 ms 1920 KB Output is correct
15 Correct 1 ms 1920 KB Output is correct
16 Correct 2 ms 1920 KB Output is correct
17 Correct 2 ms 1920 KB Output is correct
18 Correct 1 ms 1920 KB Output is correct
19 Correct 1 ms 1920 KB Output is correct
20 Correct 1 ms 1920 KB Output is correct
21 Correct 1 ms 1920 KB Output is correct
22 Correct 1 ms 1920 KB Output is correct
23 Correct 1 ms 1920 KB Output is correct
24 Correct 1 ms 1920 KB Output is correct
25 Correct 2 ms 1920 KB Output is correct
26 Correct 1 ms 1920 KB Output is correct
27 Correct 2 ms 1920 KB Output is correct
28 Correct 1 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 1 ms 1920 KB Output is correct
4 Correct 2 ms 1920 KB Output is correct
5 Correct 1 ms 1920 KB Output is correct
6 Correct 1 ms 1920 KB Output is correct
7 Correct 1 ms 1920 KB Output is correct
8 Correct 2 ms 1920 KB Output is correct
9 Correct 1 ms 1920 KB Output is correct
10 Correct 1 ms 1920 KB Output is correct
11 Correct 1 ms 1920 KB Output is correct
12 Correct 2 ms 2048 KB Output is correct
13 Correct 1 ms 1920 KB Output is correct
14 Correct 1 ms 1920 KB Output is correct
15 Correct 1 ms 1920 KB Output is correct
16 Correct 2 ms 1920 KB Output is correct
17 Correct 2 ms 1920 KB Output is correct
18 Correct 1 ms 1920 KB Output is correct
19 Correct 1 ms 1920 KB Output is correct
20 Correct 1 ms 1920 KB Output is correct
21 Correct 1 ms 1920 KB Output is correct
22 Correct 1 ms 1920 KB Output is correct
23 Correct 1 ms 1920 KB Output is correct
24 Correct 1 ms 1920 KB Output is correct
25 Correct 2 ms 1920 KB Output is correct
26 Correct 1 ms 1920 KB Output is correct
27 Correct 2 ms 1920 KB Output is correct
28 Correct 1 ms 1920 KB Output is correct
29 Correct 1 ms 1920 KB Output is correct
30 Correct 4 ms 2176 KB Output is correct
31 Correct 4 ms 2136 KB Output is correct
32 Correct 3 ms 2048 KB Output is correct
33 Correct 3 ms 2048 KB Output is correct
34 Correct 4 ms 2176 KB Output is correct
35 Correct 5 ms 2304 KB Output is correct
36 Correct 3 ms 2048 KB Output is correct
37 Correct 5 ms 2176 KB Output is correct
38 Correct 2 ms 1920 KB Output is correct
39 Correct 3 ms 2176 KB Output is correct
40 Correct 4 ms 2176 KB Output is correct
41 Correct 4 ms 2176 KB Output is correct
42 Correct 4 ms 2176 KB Output is correct
43 Correct 4 ms 2048 KB Output is correct
44 Correct 4 ms 2048 KB Output is correct
45 Correct 6 ms 2176 KB Output is correct
46 Correct 5 ms 2176 KB Output is correct
47 Correct 4 ms 2048 KB Output is correct
48 Correct 4 ms 2048 KB Output is correct
49 Correct 5 ms 2176 KB Output is correct
50 Correct 4 ms 2176 KB Output is correct
51 Correct 6 ms 2048 KB Output is correct
52 Correct 4 ms 2048 KB Output is correct
53 Correct 4 ms 2176 KB Output is correct
54 Correct 5 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 303 ms 13072 KB Output is correct
4 Correct 73 ms 7160 KB Output is correct
5 Correct 222 ms 24636 KB Output is correct
6 Correct 270 ms 24760 KB Output is correct
7 Correct 288 ms 23120 KB Output is correct
8 Correct 305 ms 16016 KB Output is correct
9 Correct 321 ms 17748 KB Output is correct
10 Correct 461 ms 27984 KB Output is correct
11 Correct 311 ms 17912 KB Output is correct
12 Correct 332 ms 25700 KB Output is correct
13 Correct 224 ms 9644 KB Output is correct
14 Correct 298 ms 16076 KB Output is correct
15 Correct 382 ms 21492 KB Output is correct
16 Correct 468 ms 27956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 1 ms 1920 KB Output is correct
4 Correct 2 ms 1920 KB Output is correct
5 Correct 1 ms 1920 KB Output is correct
6 Correct 1 ms 1920 KB Output is correct
7 Correct 1 ms 1920 KB Output is correct
8 Correct 2 ms 1920 KB Output is correct
9 Correct 1 ms 1920 KB Output is correct
10 Correct 1 ms 1920 KB Output is correct
11 Correct 1 ms 1920 KB Output is correct
12 Correct 2 ms 2048 KB Output is correct
13 Correct 1 ms 1920 KB Output is correct
14 Correct 1 ms 1920 KB Output is correct
15 Correct 1 ms 1920 KB Output is correct
16 Correct 2 ms 1920 KB Output is correct
17 Correct 2 ms 1920 KB Output is correct
18 Correct 1 ms 1920 KB Output is correct
19 Correct 1 ms 1920 KB Output is correct
20 Correct 1 ms 1920 KB Output is correct
21 Correct 1 ms 1920 KB Output is correct
22 Correct 1 ms 1920 KB Output is correct
23 Correct 1 ms 1920 KB Output is correct
24 Correct 1 ms 1920 KB Output is correct
25 Correct 2 ms 1920 KB Output is correct
26 Correct 1 ms 1920 KB Output is correct
27 Correct 2 ms 1920 KB Output is correct
28 Correct 1 ms 1920 KB Output is correct
29 Correct 303 ms 13072 KB Output is correct
30 Correct 73 ms 7160 KB Output is correct
31 Correct 222 ms 24636 KB Output is correct
32 Correct 270 ms 24760 KB Output is correct
33 Correct 288 ms 23120 KB Output is correct
34 Correct 305 ms 16016 KB Output is correct
35 Correct 321 ms 17748 KB Output is correct
36 Correct 461 ms 27984 KB Output is correct
37 Correct 311 ms 17912 KB Output is correct
38 Correct 332 ms 25700 KB Output is correct
39 Correct 224 ms 9644 KB Output is correct
40 Correct 298 ms 16076 KB Output is correct
41 Correct 382 ms 21492 KB Output is correct
42 Correct 468 ms 27956 KB Output is correct
43 Correct 306 ms 12944 KB Output is correct
44 Correct 72 ms 7160 KB Output is correct
45 Correct 420 ms 30948 KB Output is correct
46 Correct 280 ms 28092 KB Output is correct
47 Correct 304 ms 23504 KB Output is correct
48 Correct 356 ms 17868 KB Output is correct
49 Correct 435 ms 29632 KB Output is correct
50 Correct 299 ms 14884 KB Output is correct
51 Correct 335 ms 18308 KB Output is correct
52 Correct 383 ms 22060 KB Output is correct
53 Correct 235 ms 10156 KB Output is correct
54 Correct 323 ms 18408 KB Output is correct
55 Correct 387 ms 20976 KB Output is correct
56 Correct 446 ms 29628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 1 ms 1920 KB Output is correct
4 Correct 2 ms 1920 KB Output is correct
5 Correct 1 ms 1920 KB Output is correct
6 Correct 1 ms 1920 KB Output is correct
7 Correct 1 ms 1920 KB Output is correct
8 Correct 2 ms 1920 KB Output is correct
9 Correct 1 ms 1920 KB Output is correct
10 Correct 1 ms 1920 KB Output is correct
11 Correct 1 ms 1920 KB Output is correct
12 Correct 2 ms 2048 KB Output is correct
13 Correct 1 ms 1920 KB Output is correct
14 Correct 1 ms 1920 KB Output is correct
15 Correct 1 ms 1920 KB Output is correct
16 Correct 2 ms 1920 KB Output is correct
17 Correct 2 ms 1920 KB Output is correct
18 Correct 1 ms 1920 KB Output is correct
19 Correct 1 ms 1920 KB Output is correct
20 Correct 1 ms 1920 KB Output is correct
21 Correct 1 ms 1920 KB Output is correct
22 Correct 1 ms 1920 KB Output is correct
23 Correct 1 ms 1920 KB Output is correct
24 Correct 1 ms 1920 KB Output is correct
25 Correct 2 ms 1920 KB Output is correct
26 Correct 1 ms 1920 KB Output is correct
27 Correct 2 ms 1920 KB Output is correct
28 Correct 1 ms 1920 KB Output is correct
29 Correct 1 ms 1920 KB Output is correct
30 Correct 4 ms 2176 KB Output is correct
31 Correct 4 ms 2136 KB Output is correct
32 Correct 3 ms 2048 KB Output is correct
33 Correct 3 ms 2048 KB Output is correct
34 Correct 4 ms 2176 KB Output is correct
35 Correct 5 ms 2304 KB Output is correct
36 Correct 3 ms 2048 KB Output is correct
37 Correct 5 ms 2176 KB Output is correct
38 Correct 2 ms 1920 KB Output is correct
39 Correct 3 ms 2176 KB Output is correct
40 Correct 4 ms 2176 KB Output is correct
41 Correct 4 ms 2176 KB Output is correct
42 Correct 4 ms 2176 KB Output is correct
43 Correct 4 ms 2048 KB Output is correct
44 Correct 4 ms 2048 KB Output is correct
45 Correct 6 ms 2176 KB Output is correct
46 Correct 5 ms 2176 KB Output is correct
47 Correct 4 ms 2048 KB Output is correct
48 Correct 4 ms 2048 KB Output is correct
49 Correct 5 ms 2176 KB Output is correct
50 Correct 4 ms 2176 KB Output is correct
51 Correct 6 ms 2048 KB Output is correct
52 Correct 4 ms 2048 KB Output is correct
53 Correct 4 ms 2176 KB Output is correct
54 Correct 5 ms 2176 KB Output is correct
55 Correct 248 ms 12944 KB Output is correct
56 Correct 69 ms 8696 KB Output is correct
57 Correct 143 ms 18472 KB Output is correct
58 Correct 247 ms 33340 KB Output is correct
59 Correct 298 ms 20700 KB Output is correct
60 Correct 376 ms 21096 KB Output is correct
61 Correct 260 ms 20336 KB Output is correct
62 Correct 325 ms 33968 KB Output is correct
63 Correct 221 ms 15872 KB Output is correct
64 Correct 317 ms 21044 KB Output is correct
65 Correct 378 ms 23172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1920 KB Output is correct
2 Correct 1 ms 2048 KB Output is correct
3 Correct 1 ms 1920 KB Output is correct
4 Correct 2 ms 1920 KB Output is correct
5 Correct 1 ms 1920 KB Output is correct
6 Correct 1 ms 1920 KB Output is correct
7 Correct 1 ms 1920 KB Output is correct
8 Correct 2 ms 1920 KB Output is correct
9 Correct 1 ms 1920 KB Output is correct
10 Correct 1 ms 1920 KB Output is correct
11 Correct 1 ms 1920 KB Output is correct
12 Correct 2 ms 2048 KB Output is correct
13 Correct 1 ms 1920 KB Output is correct
14 Correct 1 ms 1920 KB Output is correct
15 Correct 1 ms 1920 KB Output is correct
16 Correct 2 ms 1920 KB Output is correct
17 Correct 2 ms 1920 KB Output is correct
18 Correct 1 ms 1920 KB Output is correct
19 Correct 1 ms 1920 KB Output is correct
20 Correct 1 ms 1920 KB Output is correct
21 Correct 1 ms 1920 KB Output is correct
22 Correct 1 ms 1920 KB Output is correct
23 Correct 1 ms 1920 KB Output is correct
24 Correct 1 ms 1920 KB Output is correct
25 Correct 2 ms 1920 KB Output is correct
26 Correct 1 ms 1920 KB Output is correct
27 Correct 2 ms 1920 KB Output is correct
28 Correct 1 ms 1920 KB Output is correct
29 Correct 1 ms 1920 KB Output is correct
30 Correct 4 ms 2176 KB Output is correct
31 Correct 4 ms 2136 KB Output is correct
32 Correct 3 ms 2048 KB Output is correct
33 Correct 3 ms 2048 KB Output is correct
34 Correct 4 ms 2176 KB Output is correct
35 Correct 5 ms 2304 KB Output is correct
36 Correct 3 ms 2048 KB Output is correct
37 Correct 5 ms 2176 KB Output is correct
38 Correct 2 ms 1920 KB Output is correct
39 Correct 3 ms 2176 KB Output is correct
40 Correct 4 ms 2176 KB Output is correct
41 Correct 4 ms 2176 KB Output is correct
42 Correct 4 ms 2176 KB Output is correct
43 Correct 4 ms 2048 KB Output is correct
44 Correct 4 ms 2048 KB Output is correct
45 Correct 6 ms 2176 KB Output is correct
46 Correct 5 ms 2176 KB Output is correct
47 Correct 4 ms 2048 KB Output is correct
48 Correct 4 ms 2048 KB Output is correct
49 Correct 5 ms 2176 KB Output is correct
50 Correct 4 ms 2176 KB Output is correct
51 Correct 6 ms 2048 KB Output is correct
52 Correct 4 ms 2048 KB Output is correct
53 Correct 4 ms 2176 KB Output is correct
54 Correct 5 ms 2176 KB Output is correct
55 Correct 303 ms 13072 KB Output is correct
56 Correct 73 ms 7160 KB Output is correct
57 Correct 222 ms 24636 KB Output is correct
58 Correct 270 ms 24760 KB Output is correct
59 Correct 288 ms 23120 KB Output is correct
60 Correct 305 ms 16016 KB Output is correct
61 Correct 321 ms 17748 KB Output is correct
62 Correct 461 ms 27984 KB Output is correct
63 Correct 311 ms 17912 KB Output is correct
64 Correct 332 ms 25700 KB Output is correct
65 Correct 224 ms 9644 KB Output is correct
66 Correct 298 ms 16076 KB Output is correct
67 Correct 382 ms 21492 KB Output is correct
68 Correct 468 ms 27956 KB Output is correct
69 Correct 306 ms 12944 KB Output is correct
70 Correct 72 ms 7160 KB Output is correct
71 Correct 420 ms 30948 KB Output is correct
72 Correct 280 ms 28092 KB Output is correct
73 Correct 304 ms 23504 KB Output is correct
74 Correct 356 ms 17868 KB Output is correct
75 Correct 435 ms 29632 KB Output is correct
76 Correct 299 ms 14884 KB Output is correct
77 Correct 335 ms 18308 KB Output is correct
78 Correct 383 ms 22060 KB Output is correct
79 Correct 235 ms 10156 KB Output is correct
80 Correct 323 ms 18408 KB Output is correct
81 Correct 387 ms 20976 KB Output is correct
82 Correct 446 ms 29628 KB Output is correct
83 Correct 248 ms 12944 KB Output is correct
84 Correct 69 ms 8696 KB Output is correct
85 Correct 143 ms 18472 KB Output is correct
86 Correct 247 ms 33340 KB Output is correct
87 Correct 298 ms 20700 KB Output is correct
88 Correct 376 ms 21096 KB Output is correct
89 Correct 260 ms 20336 KB Output is correct
90 Correct 325 ms 33968 KB Output is correct
91 Correct 221 ms 15872 KB Output is correct
92 Correct 317 ms 21044 KB Output is correct
93 Correct 378 ms 23172 KB Output is correct
94 Correct 311 ms 15248 KB Output is correct
95 Correct 596 ms 73416 KB Output is correct
96 Correct 207 ms 18744 KB Output is correct
97 Correct 279 ms 26204 KB Output is correct
98 Correct 321 ms 24688 KB Output is correct
99 Correct 298 ms 14720 KB Output is correct
100 Correct 399 ms 43000 KB Output is correct
101 Correct 297 ms 15148 KB Output is correct
102 Correct 367 ms 18296 KB Output is correct
103 Correct 445 ms 22812 KB Output is correct
104 Correct 283 ms 13952 KB Output is correct
105 Correct 390 ms 20144 KB Output is correct
106 Correct 442 ms 20356 KB Output is correct
107 Correct 257 ms 16840 KB Output is correct
108 Correct 303 ms 12944 KB Output is correct
109 Correct 315 ms 13076 KB Output is correct
110 Correct 314 ms 13072 KB Output is correct
111 Correct 309 ms 12948 KB Output is correct
112 Correct 312 ms 13072 KB Output is correct
113 Correct 311 ms 13020 KB Output is correct
114 Correct 308 ms 12944 KB Output is correct
115 Correct 306 ms 12944 KB Output is correct
116 Correct 306 ms 12996 KB Output is correct