Submission #544541

# Submission time Handle Problem Language Result Execution time Memory
544541 2022-04-02T09:29:34 Z benson1029 Jail (JOI22_jail) C++14
100 / 100
2342 ms 348616 KB
#include<bits/stdc++.h>
using namespace std;

int q, n, m;
int timer = 0;
int st[120005], ed[120005];
int s[120005], t[120005];
int pst[120005], ped[120005];
vector<int> edg[120005], cyc[4200005];
int lift[120005][17];
int vis[4200005];
bool ans;

void dfs(int x, int p) {
	st[x] = ++timer;
	lift[x][0] = p;
	for(int i=1; i<=16; i++) {
		if(lift[x][i-1]==-1) lift[x][i] = -1;
		else lift[x][i] = lift[lift[x][i-1]][i-1];
	}
	for(int i:edg[x]) {
		if(i!=p) dfs(i, x);
	}
	ed[x] = ++timer;
}

bool anc(int x, int y) {
	return st[x]<=st[y] && ed[x]>=ed[y];
}

int lca(int x, int y) {
	if(anc(x, y)) return x;
	for(int i=16; i>=0; i--) {
		if(lift[x][i]==-1) continue;
		if(!anc(lift[x][i], y)) x = lift[x][i];
	}
	return lift[x][0];
}

void dfs2(int x) {
//	cout << x << " start\n";
	vis[x] = 1;
	for(int i:cyc[x]) {
		if(vis[i] == 1) {
//			cout << "gg " << i << "\n";
			ans = false;
		}
		if(!vis[i]) dfs2(i);
	}
	vis[x] = 2;
//	cout << x << " end\n"; 
}

void solve() {
	cin >> n;
	for(int i=1; i<=n; i++) edg[i].clear();
	for(int i=1; i<=35*n; i++) cyc[i].clear();
	for(int i=1; i<n; i++) {
		int u,v;
		cin >> u >> v;
		edg[u].push_back(v);
		edg[v].push_back(u);
	}
	dfs(1, -1);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=16; j++) {
			cyc[n*(j+1) + i].push_back(n*(j-1+1) + i);
			if(lift[i][j-1] != -1)
				cyc[n*(j+1) + i].push_back(n*(j-1+1) + lift[i][j-1]); 
			
			cyc[n*(j-1+18) + i].push_back(n*(j+18) + i);
			if(lift[i][j-1] != -1)
				cyc[n*(j-1+18) + lift[i][j-1]].push_back(n*(j+18) + i); 
		}
	}
	cin >> m;
	for(int i=1; i<=m; i++) {
		cin >> s[i] >> t[i];
		pst[s[i]] = i;
		ped[t[i]] = i;
	}
	for(int i=1; i<=m; i++) {
		cyc[i].push_back(n*18 + t[i]);
		cyc[n*1 + s[i]].push_back(i);
		cyc[n*18 + s[i]].push_back(i);
		cyc[i].push_back(n*1 + t[i]);
		
		int l = lca(s[i], t[i]);
		
		int p = lift[s[i]][0];
		
		if(p!=-1) {
			if(!anc(p, l)) {
				for(int j=16; j>=0; j--) {
					if(lift[p][j]==-1) continue;
					if(!anc(lift[p][j], l)) {
						cyc[n*(18+j) + p].push_back(i);
						cyc[i].push_back(n*(1+j) + p);
						p = lift[p][j];
					}
				}
				cyc[n*18 + p].push_back(i);
				cyc[i].push_back(n*1 + p);
			}
		}
		
		p = lift[t[i]][0];
		
		if(p!=-1) {
			if(!anc(p, l)) {
				for(int j=16; j>=0; j--) {
					if(lift[p][j]==-1) continue;
					if(!anc(lift[p][j], l)) {
						cyc[n*(18+j) + p].push_back(i);
						cyc[i].push_back(n*(1+j) + p);
						p = lift[p][j];
					}
				}
				cyc[n*18 + p].push_back(i);
				cyc[i].push_back(n*1 + p);
			}
		}
		
		if(pst[l] != i) cyc[i].push_back(n*1 + l);
		if(ped[l] != i) cyc[n*18 + l].push_back(i);
	}
	for(int i=1; i<=35*n; i++) vis[i] = 0;
	ans = true;
	for(int i=1; i<=35*n; i++) {
		if(!vis[i]) dfs2(i);
	}
	cout << (ans ? "Yes\n" : "No\n");
}

int main() {
	ios::sync_with_stdio(false);
	cin >> q;
	while(q--) {
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 46 ms 101708 KB Output is correct
2 Correct 46 ms 101764 KB Output is correct
3 Correct 47 ms 101744 KB Output is correct
4 Correct 92 ms 102288 KB Output is correct
5 Correct 142 ms 102604 KB Output is correct
6 Correct 56 ms 102152 KB Output is correct
7 Correct 53 ms 102092 KB Output is correct
8 Correct 52 ms 102220 KB Output is correct
9 Correct 374 ms 110908 KB Output is correct
10 Correct 931 ms 257852 KB Output is correct
11 Correct 66 ms 101940 KB Output is correct
12 Correct 162 ms 102844 KB Output is correct
13 Correct 1014 ms 263868 KB Output is correct
14 Correct 967 ms 263804 KB Output is correct
15 Correct 1569 ms 294568 KB Output is correct
16 Correct 2017 ms 348616 KB Output is correct
17 Correct 1109 ms 308704 KB Output is correct
18 Correct 930 ms 272912 KB Output is correct
19 Correct 1128 ms 275848 KB Output is correct
20 Correct 1071 ms 290712 KB Output is correct
21 Correct 1094 ms 294992 KB Output is correct
22 Correct 895 ms 263048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 101768 KB Output is correct
2 Correct 51 ms 101752 KB Output is correct
3 Correct 58 ms 102048 KB Output is correct
4 Correct 66 ms 102160 KB Output is correct
5 Correct 64 ms 102092 KB Output is correct
6 Correct 64 ms 102156 KB Output is correct
7 Correct 64 ms 102140 KB Output is correct
8 Correct 56 ms 102092 KB Output is correct
9 Correct 61 ms 102160 KB Output is correct
10 Correct 56 ms 102136 KB Output is correct
11 Correct 58 ms 102084 KB Output is correct
12 Correct 61 ms 102044 KB Output is correct
13 Correct 52 ms 102032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 101768 KB Output is correct
2 Correct 51 ms 101752 KB Output is correct
3 Correct 58 ms 102048 KB Output is correct
4 Correct 66 ms 102160 KB Output is correct
5 Correct 64 ms 102092 KB Output is correct
6 Correct 64 ms 102156 KB Output is correct
7 Correct 64 ms 102140 KB Output is correct
8 Correct 56 ms 102092 KB Output is correct
9 Correct 61 ms 102160 KB Output is correct
10 Correct 56 ms 102136 KB Output is correct
11 Correct 58 ms 102084 KB Output is correct
12 Correct 61 ms 102044 KB Output is correct
13 Correct 52 ms 102032 KB Output is correct
14 Correct 59 ms 101792 KB Output is correct
15 Correct 51 ms 101760 KB Output is correct
16 Correct 55 ms 102052 KB Output is correct
17 Correct 61 ms 102168 KB Output is correct
18 Correct 58 ms 102108 KB Output is correct
19 Correct 48 ms 101704 KB Output is correct
20 Correct 54 ms 102184 KB Output is correct
21 Correct 61 ms 102144 KB Output is correct
22 Correct 63 ms 102080 KB Output is correct
23 Correct 51 ms 101692 KB Output is correct
24 Correct 52 ms 101944 KB Output is correct
25 Correct 54 ms 102088 KB Output is correct
26 Correct 50 ms 102060 KB Output is correct
27 Correct 57 ms 102112 KB Output is correct
28 Correct 62 ms 101720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 101768 KB Output is correct
2 Correct 51 ms 101752 KB Output is correct
3 Correct 58 ms 102048 KB Output is correct
4 Correct 66 ms 102160 KB Output is correct
5 Correct 64 ms 102092 KB Output is correct
6 Correct 64 ms 102156 KB Output is correct
7 Correct 64 ms 102140 KB Output is correct
8 Correct 56 ms 102092 KB Output is correct
9 Correct 61 ms 102160 KB Output is correct
10 Correct 56 ms 102136 KB Output is correct
11 Correct 58 ms 102084 KB Output is correct
12 Correct 61 ms 102044 KB Output is correct
13 Correct 52 ms 102032 KB Output is correct
14 Correct 59 ms 101792 KB Output is correct
15 Correct 51 ms 101760 KB Output is correct
16 Correct 55 ms 102052 KB Output is correct
17 Correct 61 ms 102168 KB Output is correct
18 Correct 58 ms 102108 KB Output is correct
19 Correct 48 ms 101704 KB Output is correct
20 Correct 54 ms 102184 KB Output is correct
21 Correct 61 ms 102144 KB Output is correct
22 Correct 63 ms 102080 KB Output is correct
23 Correct 51 ms 101692 KB Output is correct
24 Correct 52 ms 101944 KB Output is correct
25 Correct 54 ms 102088 KB Output is correct
26 Correct 50 ms 102060 KB Output is correct
27 Correct 57 ms 102112 KB Output is correct
28 Correct 62 ms 101720 KB Output is correct
29 Correct 58 ms 102228 KB Output is correct
30 Correct 57 ms 102260 KB Output is correct
31 Correct 64 ms 102176 KB Output is correct
32 Correct 55 ms 102092 KB Output is correct
33 Correct 55 ms 102212 KB Output is correct
34 Correct 56 ms 102112 KB Output is correct
35 Correct 59 ms 102092 KB Output is correct
36 Correct 60 ms 102144 KB Output is correct
37 Correct 51 ms 102032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 101768 KB Output is correct
2 Correct 51 ms 101752 KB Output is correct
3 Correct 58 ms 102048 KB Output is correct
4 Correct 66 ms 102160 KB Output is correct
5 Correct 64 ms 102092 KB Output is correct
6 Correct 64 ms 102156 KB Output is correct
7 Correct 64 ms 102140 KB Output is correct
8 Correct 56 ms 102092 KB Output is correct
9 Correct 61 ms 102160 KB Output is correct
10 Correct 56 ms 102136 KB Output is correct
11 Correct 58 ms 102084 KB Output is correct
12 Correct 61 ms 102044 KB Output is correct
13 Correct 52 ms 102032 KB Output is correct
14 Correct 59 ms 101792 KB Output is correct
15 Correct 51 ms 101760 KB Output is correct
16 Correct 55 ms 102052 KB Output is correct
17 Correct 61 ms 102168 KB Output is correct
18 Correct 58 ms 102108 KB Output is correct
19 Correct 48 ms 101704 KB Output is correct
20 Correct 54 ms 102184 KB Output is correct
21 Correct 61 ms 102144 KB Output is correct
22 Correct 63 ms 102080 KB Output is correct
23 Correct 51 ms 101692 KB Output is correct
24 Correct 52 ms 101944 KB Output is correct
25 Correct 54 ms 102088 KB Output is correct
26 Correct 50 ms 102060 KB Output is correct
27 Correct 57 ms 102112 KB Output is correct
28 Correct 62 ms 101720 KB Output is correct
29 Correct 58 ms 102228 KB Output is correct
30 Correct 57 ms 102260 KB Output is correct
31 Correct 64 ms 102176 KB Output is correct
32 Correct 55 ms 102092 KB Output is correct
33 Correct 55 ms 102212 KB Output is correct
34 Correct 56 ms 102112 KB Output is correct
35 Correct 59 ms 102092 KB Output is correct
36 Correct 60 ms 102144 KB Output is correct
37 Correct 51 ms 102032 KB Output is correct
38 Correct 390 ms 110964 KB Output is correct
39 Correct 919 ms 257992 KB Output is correct
40 Correct 363 ms 112116 KB Output is correct
41 Correct 324 ms 111064 KB Output is correct
42 Correct 329 ms 111696 KB Output is correct
43 Correct 322 ms 112100 KB Output is correct
44 Correct 79 ms 103372 KB Output is correct
45 Correct 975 ms 255564 KB Output is correct
46 Correct 1099 ms 255532 KB Output is correct
47 Correct 946 ms 256260 KB Output is correct
48 Correct 1000 ms 256256 KB Output is correct
49 Correct 977 ms 258164 KB Output is correct
50 Correct 917 ms 258272 KB Output is correct
51 Correct 993 ms 258856 KB Output is correct
52 Correct 992 ms 258888 KB Output is correct
53 Correct 177 ms 113404 KB Output is correct
54 Correct 1309 ms 255948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 101780 KB Output is correct
2 Correct 56 ms 101752 KB Output is correct
3 Correct 54 ms 101776 KB Output is correct
4 Correct 56 ms 101708 KB Output is correct
5 Correct 75 ms 101860 KB Output is correct
6 Correct 63 ms 102040 KB Output is correct
7 Correct 70 ms 102072 KB Output is correct
8 Correct 68 ms 101748 KB Output is correct
9 Correct 66 ms 101708 KB Output is correct
10 Correct 57 ms 101876 KB Output is correct
11 Correct 67 ms 101788 KB Output is correct
12 Correct 71 ms 102088 KB Output is correct
13 Correct 119 ms 102508 KB Output is correct
14 Correct 192 ms 102856 KB Output is correct
15 Correct 148 ms 102604 KB Output is correct
16 Correct 1032 ms 256688 KB Output is correct
17 Correct 1407 ms 267372 KB Output is correct
18 Correct 1558 ms 285588 KB Output is correct
19 Correct 1060 ms 258232 KB Output is correct
20 Correct 1159 ms 257804 KB Output is correct
21 Correct 1094 ms 257756 KB Output is correct
22 Correct 1127 ms 264384 KB Output is correct
23 Correct 966 ms 263244 KB Output is correct
24 Correct 1052 ms 263032 KB Output is correct
25 Correct 1064 ms 262940 KB Output is correct
26 Correct 1103 ms 262916 KB Output is correct
27 Correct 882 ms 272392 KB Output is correct
28 Correct 806 ms 275216 KB Output is correct
29 Correct 907 ms 269256 KB Output is correct
30 Correct 1051 ms 263668 KB Output is correct
31 Correct 944 ms 264884 KB Output is correct
32 Correct 1112 ms 263732 KB Output is correct
33 Correct 1043 ms 265268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 101708 KB Output is correct
2 Correct 46 ms 101764 KB Output is correct
3 Correct 47 ms 101744 KB Output is correct
4 Correct 92 ms 102288 KB Output is correct
5 Correct 142 ms 102604 KB Output is correct
6 Correct 56 ms 102152 KB Output is correct
7 Correct 53 ms 102092 KB Output is correct
8 Correct 52 ms 102220 KB Output is correct
9 Correct 374 ms 110908 KB Output is correct
10 Correct 931 ms 257852 KB Output is correct
11 Correct 66 ms 101940 KB Output is correct
12 Correct 162 ms 102844 KB Output is correct
13 Correct 1014 ms 263868 KB Output is correct
14 Correct 967 ms 263804 KB Output is correct
15 Correct 1569 ms 294568 KB Output is correct
16 Correct 2017 ms 348616 KB Output is correct
17 Correct 1109 ms 308704 KB Output is correct
18 Correct 930 ms 272912 KB Output is correct
19 Correct 1128 ms 275848 KB Output is correct
20 Correct 1071 ms 290712 KB Output is correct
21 Correct 1094 ms 294992 KB Output is correct
22 Correct 895 ms 263048 KB Output is correct
23 Correct 61 ms 101768 KB Output is correct
24 Correct 51 ms 101752 KB Output is correct
25 Correct 58 ms 102048 KB Output is correct
26 Correct 66 ms 102160 KB Output is correct
27 Correct 64 ms 102092 KB Output is correct
28 Correct 64 ms 102156 KB Output is correct
29 Correct 64 ms 102140 KB Output is correct
30 Correct 56 ms 102092 KB Output is correct
31 Correct 61 ms 102160 KB Output is correct
32 Correct 56 ms 102136 KB Output is correct
33 Correct 58 ms 102084 KB Output is correct
34 Correct 61 ms 102044 KB Output is correct
35 Correct 52 ms 102032 KB Output is correct
36 Correct 59 ms 101792 KB Output is correct
37 Correct 51 ms 101760 KB Output is correct
38 Correct 55 ms 102052 KB Output is correct
39 Correct 61 ms 102168 KB Output is correct
40 Correct 58 ms 102108 KB Output is correct
41 Correct 48 ms 101704 KB Output is correct
42 Correct 54 ms 102184 KB Output is correct
43 Correct 61 ms 102144 KB Output is correct
44 Correct 63 ms 102080 KB Output is correct
45 Correct 51 ms 101692 KB Output is correct
46 Correct 52 ms 101944 KB Output is correct
47 Correct 54 ms 102088 KB Output is correct
48 Correct 50 ms 102060 KB Output is correct
49 Correct 57 ms 102112 KB Output is correct
50 Correct 62 ms 101720 KB Output is correct
51 Correct 58 ms 102228 KB Output is correct
52 Correct 57 ms 102260 KB Output is correct
53 Correct 64 ms 102176 KB Output is correct
54 Correct 55 ms 102092 KB Output is correct
55 Correct 55 ms 102212 KB Output is correct
56 Correct 56 ms 102112 KB Output is correct
57 Correct 59 ms 102092 KB Output is correct
58 Correct 60 ms 102144 KB Output is correct
59 Correct 51 ms 102032 KB Output is correct
60 Correct 390 ms 110964 KB Output is correct
61 Correct 919 ms 257992 KB Output is correct
62 Correct 363 ms 112116 KB Output is correct
63 Correct 324 ms 111064 KB Output is correct
64 Correct 329 ms 111696 KB Output is correct
65 Correct 322 ms 112100 KB Output is correct
66 Correct 79 ms 103372 KB Output is correct
67 Correct 975 ms 255564 KB Output is correct
68 Correct 1099 ms 255532 KB Output is correct
69 Correct 946 ms 256260 KB Output is correct
70 Correct 1000 ms 256256 KB Output is correct
71 Correct 977 ms 258164 KB Output is correct
72 Correct 917 ms 258272 KB Output is correct
73 Correct 993 ms 258856 KB Output is correct
74 Correct 992 ms 258888 KB Output is correct
75 Correct 177 ms 113404 KB Output is correct
76 Correct 1309 ms 255948 KB Output is correct
77 Correct 65 ms 101780 KB Output is correct
78 Correct 56 ms 101752 KB Output is correct
79 Correct 54 ms 101776 KB Output is correct
80 Correct 56 ms 101708 KB Output is correct
81 Correct 75 ms 101860 KB Output is correct
82 Correct 63 ms 102040 KB Output is correct
83 Correct 70 ms 102072 KB Output is correct
84 Correct 68 ms 101748 KB Output is correct
85 Correct 66 ms 101708 KB Output is correct
86 Correct 57 ms 101876 KB Output is correct
87 Correct 67 ms 101788 KB Output is correct
88 Correct 71 ms 102088 KB Output is correct
89 Correct 119 ms 102508 KB Output is correct
90 Correct 192 ms 102856 KB Output is correct
91 Correct 148 ms 102604 KB Output is correct
92 Correct 1032 ms 256688 KB Output is correct
93 Correct 1407 ms 267372 KB Output is correct
94 Correct 1558 ms 285588 KB Output is correct
95 Correct 1060 ms 258232 KB Output is correct
96 Correct 1159 ms 257804 KB Output is correct
97 Correct 1094 ms 257756 KB Output is correct
98 Correct 1127 ms 264384 KB Output is correct
99 Correct 966 ms 263244 KB Output is correct
100 Correct 1052 ms 263032 KB Output is correct
101 Correct 1064 ms 262940 KB Output is correct
102 Correct 1103 ms 262916 KB Output is correct
103 Correct 882 ms 272392 KB Output is correct
104 Correct 806 ms 275216 KB Output is correct
105 Correct 907 ms 269256 KB Output is correct
106 Correct 1051 ms 263668 KB Output is correct
107 Correct 944 ms 264884 KB Output is correct
108 Correct 1112 ms 263732 KB Output is correct
109 Correct 1043 ms 265268 KB Output is correct
110 Correct 164 ms 103080 KB Output is correct
111 Correct 130 ms 102612 KB Output is correct
112 Correct 1771 ms 301028 KB Output is correct
113 Correct 1144 ms 260412 KB Output is correct
114 Correct 1257 ms 292132 KB Output is correct
115 Correct 409 ms 254612 KB Output is correct
116 Correct 1386 ms 261560 KB Output is correct
117 Correct 1747 ms 293320 KB Output is correct
118 Correct 1301 ms 256000 KB Output is correct
119 Correct 1185 ms 256060 KB Output is correct
120 Correct 131 ms 115212 KB Output is correct
121 Correct 1445 ms 263472 KB Output is correct
122 Correct 1431 ms 263208 KB Output is correct
123 Correct 1384 ms 260660 KB Output is correct
124 Correct 1262 ms 260836 KB Output is correct
125 Correct 1363 ms 261132 KB Output is correct
126 Correct 2342 ms 341284 KB Output is correct
127 Correct 1984 ms 307008 KB Output is correct
128 Correct 1620 ms 307636 KB Output is correct
129 Correct 1607 ms 270520 KB Output is correct
130 Correct 1764 ms 301808 KB Output is correct