답안 #618002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
618002 2022-08-01T18:58:38 Z patrikpavic2 Jail (JOI22_jail) C++17
100 / 100
2191 ms 361812 KB
#include <cstdio>
#include <vector>
#include <algorithm>

#define X first
#define Y second
#define PB push_back

using namespace std;

const int N = 120500;
const int LOG = 17;

vector < int > v[N], poc[N], zav[N], sm[35 * N];
int n, q, st[N], en[N], bio[35 * N], dep[N], par[N][LOG];
int dp[N][LOG][2], cv[N];

void dfs(int x, int lst){
	dep[x] = dep[lst] + 1;
	par[x][0] = lst;
	for(int y : v[x])
		if(y != lst) dfs(y, x);
}

int naso = 0, node = 1;

void make_node(int i, int j, int k){
	if(dp[i][j][k]) return;
	dp[i][j][k] = node++;
	make_node(i, j - 1, k);
	make_node(par[i][j - 1], j - 1, k);
	if(!k){
		sm[dp[i][j][k]].PB(dp[i][j - 1][k]);
		sm[dp[i][j][k]].PB(dp[par[i][j - 1]][j - 1][k]);
	}
	else{
		sm[dp[i][j - 1][k]].PB(dp[i][j][k]);
		sm[dp[par[i][j - 1]][j - 1][k]].PB(dp[i][j][k]);
	}
}

int digni(int a, int k){
	for(int j = 0;j < LOG;j++)
		if(k & (1 << j)) a = par[a][j];
	return a;	
}

int lca(int a, int b){
	if(dep[a] < dep[b]) swap(a, b);
	a = digni(a, dep[a] - dep[b]);
	if(a == b) return a;
	for(int j = LOG - 1;j >= 0;j--)
		if(par[a][j] != par[b][j])
			a = par[a][j], b = par[b][j];
	return par[a][0];
}

void oznaci(int a, int b, int k, int tko){
	if(dep[a] < dep[b]) swap(a, b);
	for(int j = LOG - 1;j >= 0;j--){
		if(dep[a] - dep[b] >= (1 << j)){
			make_node(a, j, k);
			if(!k) sm[tko].PB(dp[a][j][k]);
			else sm[dp[a][j][k]].PB(tko);
			a = par[a][j];
		}
	}
	if(a == b) {
		make_node(a, 0, k);
		if(!k) sm[tko].PB(dp[a][0][k]);
		else sm[dp[a][0][k]].PB(tko);
		return;
	}
	for(int j = LOG - 1;j >= 0;j--){
		if(par[a][j] != par[b][j]){
			make_node(a, j, k); make_node(b, j, k);
			if(!k) 
				sm[tko].PB(dp[a][j][k]),
				sm[tko].PB(dp[b][j][k]);
			else 
				sm[dp[a][j][k]].PB(tko),
				sm[dp[b][j][k]].PB(tko);
			a = par[a][j], b = par[b][j];
		}
	}
	make_node(a, 1, k); make_node(b, 0, k);
	if(!k) sm[tko].PB(dp[a][1][k]), sm[tko].PB(dp[b][0][k]);
	else sm[dp[a][1][k]].PB(tko), sm[dp[b][0][k]].PB(tko);;
	return;
}

void ciklus(int x){
	if(bio[x] == 1) { naso = 1; }
	if(bio[x]) return;
	bio[x] = 1;
	for(int y : sm[x]){
		ciklus(y);
	}
	bio[x] = 2;
}

void solve(){
	scanf("%d", &n);
	for(int i = 1;i < n;i++){
		int a, b; scanf("%d%d", &a, &b);
		v[a].PB(b);
		v[b].PB(a);
	}
	dfs(1, 1);
	for(int j = 1;j < LOG;j++){
		for(int i = 1;i <= n;i++){
			dp[i][j][0] = 0, dp[i][j][1] = 0;
			par[i][j] = par[par[i][j - 1]][j - 1];		
		}
	}
	scanf("%d", &q);
	for(int i = 0;i < q;i++){
		int a, b; scanf("%d%d", &a, &b);
		st[i] = a; en[i] = b; cv[i] = node++;
		poc[a].PB(i); zav[b].PB(i);
	}
	for(int i = 1;i <= n;i++){
		dp[i][0][0] = node++;
		dp[i][0][1] = node++;
		for(int x : poc[i])
			sm[dp[i][0][0]].PB(cv[x]);
		for(int x : zav[i])
			sm[cv[x]].PB(dp[i][0][1]);
	}
	for(int i = 0;i < q;i++){
		int a = st[i], b = en[i];
		int lc = lca(a, b);
		if(lc != a && lc != b){
			oznaci(par[a][0], b, 0, cv[i]);
			oznaci(a, par[b][0], 1, cv[i]);
		}
		else if(lc == a){
			oznaci(digni(b, dep[b] - dep[a] - 1), b, 0, cv[i]);
			oznaci(a, par[b][0], 1, cv[i]);
		}
		else{
			oznaci(par[a][0], b, 0, cv[i]);
			oznaci(a, digni(a, dep[a] - dep[b] - 1), 1, cv[i]);
		}
	}
	for(int i = 1;i < node;i++) ciklus(i);
	for(int i = 1;i < node;i++) bio[i] = 0, sm[i].clear();
	for(int i = 1;i <= n;i++) v[i].clear(), poc[i].clear(), zav[i].clear();
	printf(naso ? "No\n" : "Yes\n");
	naso = 0; node = 1;

}

int main(){
	int T; scanf("%d", &T);
	for(;T--;) solve();
	return 0;
}

Compilation message

jail.cpp: In function 'void solve()':
jail.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
jail.cpp:105:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   int a, b; scanf("%d%d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~
jail.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
jail.cpp:118:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |   int a, b; scanf("%d%d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~
jail.cpp: In function 'int main()':
jail.cpp:155:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |  int T; scanf("%d", &T);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 107812 KB Output is correct
2 Correct 53 ms 107848 KB Output is correct
3 Correct 53 ms 107788 KB Output is correct
4 Correct 66 ms 108236 KB Output is correct
5 Correct 88 ms 108572 KB Output is correct
6 Correct 50 ms 107928 KB Output is correct
7 Correct 51 ms 107932 KB Output is correct
8 Correct 54 ms 108020 KB Output is correct
9 Correct 178 ms 115464 KB Output is correct
10 Correct 628 ms 231700 KB Output is correct
11 Correct 61 ms 107980 KB Output is correct
12 Correct 111 ms 108836 KB Output is correct
13 Correct 305 ms 184336 KB Output is correct
14 Correct 322 ms 181288 KB Output is correct
15 Correct 1548 ms 300336 KB Output is correct
16 Correct 2191 ms 361812 KB Output is correct
17 Correct 1169 ms 321088 KB Output is correct
18 Correct 199 ms 171736 KB Output is correct
19 Correct 941 ms 277744 KB Output is correct
20 Correct 946 ms 292908 KB Output is correct
21 Correct 1384 ms 311344 KB Output is correct
22 Correct 192 ms 165328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 107768 KB Output is correct
2 Correct 60 ms 107840 KB Output is correct
3 Correct 53 ms 107980 KB Output is correct
4 Correct 52 ms 107880 KB Output is correct
5 Correct 54 ms 107848 KB Output is correct
6 Correct 59 ms 107852 KB Output is correct
7 Correct 55 ms 107884 KB Output is correct
8 Correct 60 ms 107856 KB Output is correct
9 Correct 53 ms 107948 KB Output is correct
10 Correct 52 ms 107852 KB Output is correct
11 Correct 52 ms 107868 KB Output is correct
12 Correct 52 ms 107876 KB Output is correct
13 Correct 52 ms 107916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 107768 KB Output is correct
2 Correct 60 ms 107840 KB Output is correct
3 Correct 53 ms 107980 KB Output is correct
4 Correct 52 ms 107880 KB Output is correct
5 Correct 54 ms 107848 KB Output is correct
6 Correct 59 ms 107852 KB Output is correct
7 Correct 55 ms 107884 KB Output is correct
8 Correct 60 ms 107856 KB Output is correct
9 Correct 53 ms 107948 KB Output is correct
10 Correct 52 ms 107852 KB Output is correct
11 Correct 52 ms 107868 KB Output is correct
12 Correct 52 ms 107876 KB Output is correct
13 Correct 52 ms 107916 KB Output is correct
14 Correct 50 ms 107784 KB Output is correct
15 Correct 55 ms 107812 KB Output is correct
16 Correct 55 ms 107968 KB Output is correct
17 Correct 54 ms 107928 KB Output is correct
18 Correct 52 ms 107972 KB Output is correct
19 Correct 51 ms 107772 KB Output is correct
20 Correct 52 ms 107944 KB Output is correct
21 Correct 61 ms 107944 KB Output is correct
22 Correct 63 ms 107976 KB Output is correct
23 Correct 52 ms 107852 KB Output is correct
24 Correct 51 ms 107792 KB Output is correct
25 Correct 53 ms 107988 KB Output is correct
26 Correct 53 ms 107928 KB Output is correct
27 Correct 52 ms 107916 KB Output is correct
28 Correct 52 ms 107776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 107768 KB Output is correct
2 Correct 60 ms 107840 KB Output is correct
3 Correct 53 ms 107980 KB Output is correct
4 Correct 52 ms 107880 KB Output is correct
5 Correct 54 ms 107848 KB Output is correct
6 Correct 59 ms 107852 KB Output is correct
7 Correct 55 ms 107884 KB Output is correct
8 Correct 60 ms 107856 KB Output is correct
9 Correct 53 ms 107948 KB Output is correct
10 Correct 52 ms 107852 KB Output is correct
11 Correct 52 ms 107868 KB Output is correct
12 Correct 52 ms 107876 KB Output is correct
13 Correct 52 ms 107916 KB Output is correct
14 Correct 50 ms 107784 KB Output is correct
15 Correct 55 ms 107812 KB Output is correct
16 Correct 55 ms 107968 KB Output is correct
17 Correct 54 ms 107928 KB Output is correct
18 Correct 52 ms 107972 KB Output is correct
19 Correct 51 ms 107772 KB Output is correct
20 Correct 52 ms 107944 KB Output is correct
21 Correct 61 ms 107944 KB Output is correct
22 Correct 63 ms 107976 KB Output is correct
23 Correct 52 ms 107852 KB Output is correct
24 Correct 51 ms 107792 KB Output is correct
25 Correct 53 ms 107988 KB Output is correct
26 Correct 53 ms 107928 KB Output is correct
27 Correct 52 ms 107916 KB Output is correct
28 Correct 52 ms 107776 KB Output is correct
29 Correct 56 ms 108068 KB Output is correct
30 Correct 61 ms 107980 KB Output is correct
31 Correct 55 ms 107956 KB Output is correct
32 Correct 54 ms 107904 KB Output is correct
33 Correct 57 ms 107876 KB Output is correct
34 Correct 60 ms 107928 KB Output is correct
35 Correct 55 ms 107936 KB Output is correct
36 Correct 52 ms 107980 KB Output is correct
37 Correct 52 ms 107896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 107768 KB Output is correct
2 Correct 60 ms 107840 KB Output is correct
3 Correct 53 ms 107980 KB Output is correct
4 Correct 52 ms 107880 KB Output is correct
5 Correct 54 ms 107848 KB Output is correct
6 Correct 59 ms 107852 KB Output is correct
7 Correct 55 ms 107884 KB Output is correct
8 Correct 60 ms 107856 KB Output is correct
9 Correct 53 ms 107948 KB Output is correct
10 Correct 52 ms 107852 KB Output is correct
11 Correct 52 ms 107868 KB Output is correct
12 Correct 52 ms 107876 KB Output is correct
13 Correct 52 ms 107916 KB Output is correct
14 Correct 50 ms 107784 KB Output is correct
15 Correct 55 ms 107812 KB Output is correct
16 Correct 55 ms 107968 KB Output is correct
17 Correct 54 ms 107928 KB Output is correct
18 Correct 52 ms 107972 KB Output is correct
19 Correct 51 ms 107772 KB Output is correct
20 Correct 52 ms 107944 KB Output is correct
21 Correct 61 ms 107944 KB Output is correct
22 Correct 63 ms 107976 KB Output is correct
23 Correct 52 ms 107852 KB Output is correct
24 Correct 51 ms 107792 KB Output is correct
25 Correct 53 ms 107988 KB Output is correct
26 Correct 53 ms 107928 KB Output is correct
27 Correct 52 ms 107916 KB Output is correct
28 Correct 52 ms 107776 KB Output is correct
29 Correct 56 ms 108068 KB Output is correct
30 Correct 61 ms 107980 KB Output is correct
31 Correct 55 ms 107956 KB Output is correct
32 Correct 54 ms 107904 KB Output is correct
33 Correct 57 ms 107876 KB Output is correct
34 Correct 60 ms 107928 KB Output is correct
35 Correct 55 ms 107936 KB Output is correct
36 Correct 52 ms 107980 KB Output is correct
37 Correct 52 ms 107896 KB Output is correct
38 Correct 166 ms 114296 KB Output is correct
39 Correct 625 ms 230392 KB Output is correct
40 Correct 136 ms 111708 KB Output is correct
41 Correct 101 ms 110316 KB Output is correct
42 Correct 106 ms 112076 KB Output is correct
43 Correct 81 ms 109704 KB Output is correct
44 Correct 69 ms 108476 KB Output is correct
45 Correct 154 ms 139352 KB Output is correct
46 Correct 149 ms 139340 KB Output is correct
47 Correct 328 ms 172624 KB Output is correct
48 Correct 341 ms 173344 KB Output is correct
49 Correct 153 ms 140728 KB Output is correct
50 Correct 149 ms 140620 KB Output is correct
51 Correct 131 ms 138020 KB Output is correct
52 Correct 133 ms 138000 KB Output is correct
53 Correct 67 ms 110316 KB Output is correct
54 Correct 150 ms 137756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 107736 KB Output is correct
2 Correct 51 ms 107832 KB Output is correct
3 Correct 51 ms 107784 KB Output is correct
4 Correct 51 ms 107828 KB Output is correct
5 Correct 61 ms 107888 KB Output is correct
6 Correct 52 ms 107852 KB Output is correct
7 Correct 53 ms 107888 KB Output is correct
8 Correct 50 ms 107852 KB Output is correct
9 Correct 50 ms 107852 KB Output is correct
10 Correct 58 ms 107816 KB Output is correct
11 Correct 52 ms 107844 KB Output is correct
12 Correct 55 ms 107960 KB Output is correct
13 Correct 80 ms 107980 KB Output is correct
14 Correct 97 ms 108036 KB Output is correct
15 Correct 92 ms 107968 KB Output is correct
16 Correct 183 ms 142976 KB Output is correct
17 Correct 416 ms 165508 KB Output is correct
18 Correct 731 ms 197572 KB Output is correct
19 Correct 248 ms 149200 KB Output is correct
20 Correct 240 ms 149984 KB Output is correct
21 Correct 243 ms 149988 KB Output is correct
22 Correct 388 ms 164680 KB Output is correct
23 Correct 355 ms 164672 KB Output is correct
24 Correct 345 ms 163344 KB Output is correct
25 Correct 317 ms 162544 KB Output is correct
26 Correct 321 ms 163208 KB Output is correct
27 Correct 414 ms 169596 KB Output is correct
28 Correct 399 ms 172616 KB Output is correct
29 Correct 399 ms 166744 KB Output is correct
30 Correct 309 ms 162992 KB Output is correct
31 Correct 320 ms 164424 KB Output is correct
32 Correct 314 ms 160524 KB Output is correct
33 Correct 336 ms 162636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 107812 KB Output is correct
2 Correct 53 ms 107848 KB Output is correct
3 Correct 53 ms 107788 KB Output is correct
4 Correct 66 ms 108236 KB Output is correct
5 Correct 88 ms 108572 KB Output is correct
6 Correct 50 ms 107928 KB Output is correct
7 Correct 51 ms 107932 KB Output is correct
8 Correct 54 ms 108020 KB Output is correct
9 Correct 178 ms 115464 KB Output is correct
10 Correct 628 ms 231700 KB Output is correct
11 Correct 61 ms 107980 KB Output is correct
12 Correct 111 ms 108836 KB Output is correct
13 Correct 305 ms 184336 KB Output is correct
14 Correct 322 ms 181288 KB Output is correct
15 Correct 1548 ms 300336 KB Output is correct
16 Correct 2191 ms 361812 KB Output is correct
17 Correct 1169 ms 321088 KB Output is correct
18 Correct 199 ms 171736 KB Output is correct
19 Correct 941 ms 277744 KB Output is correct
20 Correct 946 ms 292908 KB Output is correct
21 Correct 1384 ms 311344 KB Output is correct
22 Correct 192 ms 165328 KB Output is correct
23 Correct 54 ms 107768 KB Output is correct
24 Correct 60 ms 107840 KB Output is correct
25 Correct 53 ms 107980 KB Output is correct
26 Correct 52 ms 107880 KB Output is correct
27 Correct 54 ms 107848 KB Output is correct
28 Correct 59 ms 107852 KB Output is correct
29 Correct 55 ms 107884 KB Output is correct
30 Correct 60 ms 107856 KB Output is correct
31 Correct 53 ms 107948 KB Output is correct
32 Correct 52 ms 107852 KB Output is correct
33 Correct 52 ms 107868 KB Output is correct
34 Correct 52 ms 107876 KB Output is correct
35 Correct 52 ms 107916 KB Output is correct
36 Correct 50 ms 107784 KB Output is correct
37 Correct 55 ms 107812 KB Output is correct
38 Correct 55 ms 107968 KB Output is correct
39 Correct 54 ms 107928 KB Output is correct
40 Correct 52 ms 107972 KB Output is correct
41 Correct 51 ms 107772 KB Output is correct
42 Correct 52 ms 107944 KB Output is correct
43 Correct 61 ms 107944 KB Output is correct
44 Correct 63 ms 107976 KB Output is correct
45 Correct 52 ms 107852 KB Output is correct
46 Correct 51 ms 107792 KB Output is correct
47 Correct 53 ms 107988 KB Output is correct
48 Correct 53 ms 107928 KB Output is correct
49 Correct 52 ms 107916 KB Output is correct
50 Correct 52 ms 107776 KB Output is correct
51 Correct 56 ms 108068 KB Output is correct
52 Correct 61 ms 107980 KB Output is correct
53 Correct 55 ms 107956 KB Output is correct
54 Correct 54 ms 107904 KB Output is correct
55 Correct 57 ms 107876 KB Output is correct
56 Correct 60 ms 107928 KB Output is correct
57 Correct 55 ms 107936 KB Output is correct
58 Correct 52 ms 107980 KB Output is correct
59 Correct 52 ms 107896 KB Output is correct
60 Correct 166 ms 114296 KB Output is correct
61 Correct 625 ms 230392 KB Output is correct
62 Correct 136 ms 111708 KB Output is correct
63 Correct 101 ms 110316 KB Output is correct
64 Correct 106 ms 112076 KB Output is correct
65 Correct 81 ms 109704 KB Output is correct
66 Correct 69 ms 108476 KB Output is correct
67 Correct 154 ms 139352 KB Output is correct
68 Correct 149 ms 139340 KB Output is correct
69 Correct 328 ms 172624 KB Output is correct
70 Correct 341 ms 173344 KB Output is correct
71 Correct 153 ms 140728 KB Output is correct
72 Correct 149 ms 140620 KB Output is correct
73 Correct 131 ms 138020 KB Output is correct
74 Correct 133 ms 138000 KB Output is correct
75 Correct 67 ms 110316 KB Output is correct
76 Correct 150 ms 137756 KB Output is correct
77 Correct 61 ms 107736 KB Output is correct
78 Correct 51 ms 107832 KB Output is correct
79 Correct 51 ms 107784 KB Output is correct
80 Correct 51 ms 107828 KB Output is correct
81 Correct 61 ms 107888 KB Output is correct
82 Correct 52 ms 107852 KB Output is correct
83 Correct 53 ms 107888 KB Output is correct
84 Correct 50 ms 107852 KB Output is correct
85 Correct 50 ms 107852 KB Output is correct
86 Correct 58 ms 107816 KB Output is correct
87 Correct 52 ms 107844 KB Output is correct
88 Correct 55 ms 107960 KB Output is correct
89 Correct 80 ms 107980 KB Output is correct
90 Correct 97 ms 108036 KB Output is correct
91 Correct 92 ms 107968 KB Output is correct
92 Correct 183 ms 142976 KB Output is correct
93 Correct 416 ms 165508 KB Output is correct
94 Correct 731 ms 197572 KB Output is correct
95 Correct 248 ms 149200 KB Output is correct
96 Correct 240 ms 149984 KB Output is correct
97 Correct 243 ms 149988 KB Output is correct
98 Correct 388 ms 164680 KB Output is correct
99 Correct 355 ms 164672 KB Output is correct
100 Correct 345 ms 163344 KB Output is correct
101 Correct 317 ms 162544 KB Output is correct
102 Correct 321 ms 163208 KB Output is correct
103 Correct 414 ms 169596 KB Output is correct
104 Correct 399 ms 172616 KB Output is correct
105 Correct 399 ms 166744 KB Output is correct
106 Correct 309 ms 162992 KB Output is correct
107 Correct 320 ms 164424 KB Output is correct
108 Correct 314 ms 160524 KB Output is correct
109 Correct 336 ms 162636 KB Output is correct
110 Correct 102 ms 108912 KB Output is correct
111 Correct 80 ms 108564 KB Output is correct
112 Correct 1361 ms 263380 KB Output is correct
113 Correct 790 ms 212192 KB Output is correct
114 Correct 999 ms 239596 KB Output is correct
115 Correct 123 ms 139280 KB Output is correct
116 Correct 271 ms 150700 KB Output is correct
117 Correct 942 ms 209956 KB Output is correct
118 Correct 171 ms 139816 KB Output is correct
119 Correct 160 ms 139736 KB Output is correct
120 Correct 65 ms 112096 KB Output is correct
121 Correct 402 ms 160076 KB Output is correct
122 Correct 409 ms 160104 KB Output is correct
123 Correct 992 ms 234032 KB Output is correct
124 Correct 993 ms 233988 KB Output is correct
125 Correct 1086 ms 237320 KB Output is correct
126 Correct 2105 ms 333380 KB Output is correct
127 Correct 1642 ms 293468 KB Output is correct
128 Correct 1008 ms 279680 KB Output is correct
129 Correct 1143 ms 260832 KB Output is correct
130 Correct 1336 ms 275416 KB Output is correct