Submission #737809

# Submission time Handle Problem Language Result Execution time Memory
737809 2023-05-07T18:27:06 Z myrcella Jail (JOI22_jail) C++17
100 / 100
1514 ms 345252 KB
//by szh
#include<bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
 
const int maxn = 2e5+10;
 
int n,m;
vector <int> edge[maxn];
vector <int> ne[maxn + maxn*17*2];//[0,m): paths, [m,m+n): starts at i, [m+n,m+n+n): ends at i
int fa[maxn][17],dep[maxn];
pii path[maxn];
int deg[maxn + maxn*17*2];
 
void adde(int u,int v) {
//	if (u<m or v<m) debug(u),debug(v);
	ne[u].pb(v);
	deg[v]++;
}
 
void dfs(int u,int lst) {
	fa[u][0] = lst;
	dep[u] = lst==-1?0:dep[lst]+1;
	rep(i,1,17) {
		if (fa[u][i-1]==-1) break;
		adde(m+u*17+i-1,m+u*17+i);
		adde(m+n*17+u*17+i,m+n*17+u*17+i-1);
		adde(m+fa[u][i-1]*17+i-1,m+u*17+i);
		adde(m+n*17+u*17+i,m+n*17+fa[u][i-1]*17+i-1);
		fa[u][i] = fa[fa[u][i-1]][i-1];
	}
	for (int v:edge[u]) {
		if (v==lst) continue;
		dfs(v,u);
	}
}
 
void update(int pathid,int u,int v) { //exclude u,v
	bool uu=false,vv=false;
	if (dep[u]>dep[v]) uu=true,u = fa[u][0];
	else if (dep[u]<dep[v]) vv=true,v=fa[v][0];
	if (dep[u]<dep[v]) swap(u,v),swap(uu,vv);
	for (int i=16;i>=0;i--) {
		if (dep[u] - (1<<i) >= dep[v]) {
			adde(m+u*17+i,pathid);
			adde(pathid,m+n*17+u*17+i);
			u = fa[u][i];
		}
	}
	if (u==v) return;
	if (vv==false) v=fa[v][0];
	if (uu==false) u=fa[u][0];
	if (dep[u]>dep[v]) adde(m+u*17,pathid),adde(pathid,m+n*17+u*17),u=fa[u][0];
	if (dep[v]>dep[u]) adde(m+v*17,pathid),adde(pathid,m+n*17+v*17),v=fa[v][0];
	if (u==v) {
		adde(m+u*17,pathid),adde(pathid,m+n*17+u*17);
		return;
	}
	for (int i=16;i>=0;i--) if (fa[u][i]!=fa[v][i]) {
		adde(m+u*17+i,pathid);
		adde(pathid,m+n*17+u*17+i);
		u = fa[u][i];
		adde(m+v*17+i,pathid);
		adde(pathid,m+n*17+v*17+i);
		v = fa[v][i];
	}
	adde(m+u*17,pathid);
	adde(pathid,m+n*17+u*17);
	adde(m+v*17+1,pathid);
	adde(pathid,m+n*17+v*17+1);
}
 
 
 
void solve() {
	cin>>n;
	rep(i,0,n)
		rep(j,0,17) fa[i][j]=-1;
	rep(i,1,n) {
		int u,v;
		cin>>u>>v;
		u--,v--;
		edge[u].pb(v);
		edge[v].pb(u);
	}	
	cin>>m;dfs(0,-1);
	rep(i,0,m) {
		cin>>path[i].fi>>path[i].se;
		path[i].fi--,path[i].se--;
		adde(i,m+path[i].fi*17);
		adde(m+n*17+path[i].se*17,i);
		update(i,path[i].fi,path[i].se);
		adde(m+path[i].se*17,i);
		adde(i,m+n*17+path[i].fi*17);
	}
	
	queue <int> q;
	rep(i,0,m+n*17*2) if (deg[i]==0) q.push(i);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int v:ne[u]) {
			deg[v]--;
			if (deg[v]==0)q.push(v);
		}
	}
	bool ok = true;
	rep(i,0,m+n*17*2) {
		if (deg[i]!=0) ok=false,deg[i]=0;
		while (!ne[i].empty()) ne[i].pop_back();
		if (i<n) while (!edge[i].empty()) edge[i].pop_back();
	}
	if (ok) cout<<"Yes\n";
	else cout<<"No\n";
	return;
	
}
 
int main() {
//	freopen("input.txt","r",stdin);	
	std::ios::sync_with_stdio(false);cin.tie(0);
	int _;cin>>_;
	while (_--) solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 79 ms 169372 KB Output is correct
2 Correct 92 ms 169424 KB Output is correct
3 Correct 79 ms 169340 KB Output is correct
4 Correct 122 ms 169532 KB Output is correct
5 Correct 167 ms 169520 KB Output is correct
6 Correct 101 ms 169520 KB Output is correct
7 Correct 82 ms 169624 KB Output is correct
8 Correct 81 ms 169572 KB Output is correct
9 Correct 414 ms 176408 KB Output is correct
10 Correct 1049 ms 330272 KB Output is correct
11 Correct 102 ms 169500 KB Output is correct
12 Correct 195 ms 169796 KB Output is correct
13 Correct 1218 ms 334360 KB Output is correct
14 Correct 975 ms 334336 KB Output is correct
15 Correct 987 ms 336172 KB Output is correct
16 Correct 1240 ms 344684 KB Output is correct
17 Correct 1341 ms 337228 KB Output is correct
18 Correct 1339 ms 338668 KB Output is correct
19 Correct 1361 ms 337416 KB Output is correct
20 Correct 1169 ms 337208 KB Output is correct
21 Correct 952 ms 337656 KB Output is correct
22 Correct 983 ms 334416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169416 KB Output is correct
2 Correct 83 ms 169304 KB Output is correct
3 Correct 105 ms 169564 KB Output is correct
4 Correct 91 ms 169608 KB Output is correct
5 Correct 97 ms 169560 KB Output is correct
6 Correct 84 ms 169488 KB Output is correct
7 Correct 86 ms 169604 KB Output is correct
8 Correct 96 ms 169608 KB Output is correct
9 Correct 91 ms 169584 KB Output is correct
10 Correct 86 ms 169548 KB Output is correct
11 Correct 90 ms 169608 KB Output is correct
12 Correct 82 ms 169488 KB Output is correct
13 Correct 85 ms 169556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169416 KB Output is correct
2 Correct 83 ms 169304 KB Output is correct
3 Correct 105 ms 169564 KB Output is correct
4 Correct 91 ms 169608 KB Output is correct
5 Correct 97 ms 169560 KB Output is correct
6 Correct 84 ms 169488 KB Output is correct
7 Correct 86 ms 169604 KB Output is correct
8 Correct 96 ms 169608 KB Output is correct
9 Correct 91 ms 169584 KB Output is correct
10 Correct 86 ms 169548 KB Output is correct
11 Correct 90 ms 169608 KB Output is correct
12 Correct 82 ms 169488 KB Output is correct
13 Correct 85 ms 169556 KB Output is correct
14 Correct 96 ms 169396 KB Output is correct
15 Correct 93 ms 169340 KB Output is correct
16 Correct 86 ms 169676 KB Output is correct
17 Correct 90 ms 169620 KB Output is correct
18 Correct 100 ms 169516 KB Output is correct
19 Correct 83 ms 169404 KB Output is correct
20 Correct 84 ms 169668 KB Output is correct
21 Correct 90 ms 169544 KB Output is correct
22 Correct 92 ms 169676 KB Output is correct
23 Correct 85 ms 169344 KB Output is correct
24 Correct 82 ms 169432 KB Output is correct
25 Correct 90 ms 169656 KB Output is correct
26 Correct 91 ms 169596 KB Output is correct
27 Correct 84 ms 169552 KB Output is correct
28 Correct 80 ms 169364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169416 KB Output is correct
2 Correct 83 ms 169304 KB Output is correct
3 Correct 105 ms 169564 KB Output is correct
4 Correct 91 ms 169608 KB Output is correct
5 Correct 97 ms 169560 KB Output is correct
6 Correct 84 ms 169488 KB Output is correct
7 Correct 86 ms 169604 KB Output is correct
8 Correct 96 ms 169608 KB Output is correct
9 Correct 91 ms 169584 KB Output is correct
10 Correct 86 ms 169548 KB Output is correct
11 Correct 90 ms 169608 KB Output is correct
12 Correct 82 ms 169488 KB Output is correct
13 Correct 85 ms 169556 KB Output is correct
14 Correct 96 ms 169396 KB Output is correct
15 Correct 93 ms 169340 KB Output is correct
16 Correct 86 ms 169676 KB Output is correct
17 Correct 90 ms 169620 KB Output is correct
18 Correct 100 ms 169516 KB Output is correct
19 Correct 83 ms 169404 KB Output is correct
20 Correct 84 ms 169668 KB Output is correct
21 Correct 90 ms 169544 KB Output is correct
22 Correct 92 ms 169676 KB Output is correct
23 Correct 85 ms 169344 KB Output is correct
24 Correct 82 ms 169432 KB Output is correct
25 Correct 90 ms 169656 KB Output is correct
26 Correct 91 ms 169596 KB Output is correct
27 Correct 84 ms 169552 KB Output is correct
28 Correct 80 ms 169364 KB Output is correct
29 Correct 87 ms 169640 KB Output is correct
30 Correct 89 ms 169928 KB Output is correct
31 Correct 89 ms 169932 KB Output is correct
32 Correct 90 ms 169624 KB Output is correct
33 Correct 86 ms 169660 KB Output is correct
34 Correct 87 ms 169548 KB Output is correct
35 Correct 92 ms 169628 KB Output is correct
36 Correct 83 ms 169584 KB Output is correct
37 Correct 84 ms 169592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169416 KB Output is correct
2 Correct 83 ms 169304 KB Output is correct
3 Correct 105 ms 169564 KB Output is correct
4 Correct 91 ms 169608 KB Output is correct
5 Correct 97 ms 169560 KB Output is correct
6 Correct 84 ms 169488 KB Output is correct
7 Correct 86 ms 169604 KB Output is correct
8 Correct 96 ms 169608 KB Output is correct
9 Correct 91 ms 169584 KB Output is correct
10 Correct 86 ms 169548 KB Output is correct
11 Correct 90 ms 169608 KB Output is correct
12 Correct 82 ms 169488 KB Output is correct
13 Correct 85 ms 169556 KB Output is correct
14 Correct 96 ms 169396 KB Output is correct
15 Correct 93 ms 169340 KB Output is correct
16 Correct 86 ms 169676 KB Output is correct
17 Correct 90 ms 169620 KB Output is correct
18 Correct 100 ms 169516 KB Output is correct
19 Correct 83 ms 169404 KB Output is correct
20 Correct 84 ms 169668 KB Output is correct
21 Correct 90 ms 169544 KB Output is correct
22 Correct 92 ms 169676 KB Output is correct
23 Correct 85 ms 169344 KB Output is correct
24 Correct 82 ms 169432 KB Output is correct
25 Correct 90 ms 169656 KB Output is correct
26 Correct 91 ms 169596 KB Output is correct
27 Correct 84 ms 169552 KB Output is correct
28 Correct 80 ms 169364 KB Output is correct
29 Correct 87 ms 169640 KB Output is correct
30 Correct 89 ms 169928 KB Output is correct
31 Correct 89 ms 169932 KB Output is correct
32 Correct 90 ms 169624 KB Output is correct
33 Correct 86 ms 169660 KB Output is correct
34 Correct 87 ms 169548 KB Output is correct
35 Correct 92 ms 169628 KB Output is correct
36 Correct 83 ms 169584 KB Output is correct
37 Correct 84 ms 169592 KB Output is correct
38 Correct 378 ms 177720 KB Output is correct
39 Correct 1056 ms 331668 KB Output is correct
40 Correct 325 ms 177972 KB Output is correct
41 Correct 241 ms 174720 KB Output is correct
42 Correct 212 ms 176952 KB Output is correct
43 Correct 468 ms 177796 KB Output is correct
44 Correct 111 ms 170340 KB Output is correct
45 Correct 593 ms 259476 KB Output is correct
46 Correct 600 ms 259776 KB Output is correct
47 Correct 1241 ms 321596 KB Output is correct
48 Correct 1228 ms 321732 KB Output is correct
49 Correct 737 ms 289168 KB Output is correct
50 Correct 795 ms 289320 KB Output is correct
51 Correct 959 ms 310348 KB Output is correct
52 Correct 947 ms 310400 KB Output is correct
53 Correct 124 ms 175920 KB Output is correct
54 Correct 692 ms 252324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 169392 KB Output is correct
2 Correct 81 ms 169356 KB Output is correct
3 Correct 87 ms 169352 KB Output is correct
4 Correct 92 ms 169376 KB Output is correct
5 Correct 101 ms 169456 KB Output is correct
6 Correct 99 ms 169496 KB Output is correct
7 Correct 87 ms 169496 KB Output is correct
8 Correct 91 ms 169384 KB Output is correct
9 Correct 85 ms 169332 KB Output is correct
10 Correct 90 ms 169420 KB Output is correct
11 Correct 84 ms 169416 KB Output is correct
12 Correct 87 ms 169584 KB Output is correct
13 Correct 144 ms 170060 KB Output is correct
14 Correct 188 ms 170608 KB Output is correct
15 Correct 152 ms 170204 KB Output is correct
16 Correct 599 ms 243792 KB Output is correct
17 Correct 642 ms 251640 KB Output is correct
18 Correct 821 ms 260512 KB Output is correct
19 Correct 814 ms 245592 KB Output is correct
20 Correct 619 ms 244828 KB Output is correct
21 Correct 638 ms 244948 KB Output is correct
22 Correct 652 ms 251132 KB Output is correct
23 Correct 625 ms 251288 KB Output is correct
24 Correct 678 ms 251516 KB Output is correct
25 Correct 678 ms 252356 KB Output is correct
26 Correct 578 ms 251760 KB Output is correct
27 Correct 524 ms 240572 KB Output is correct
28 Correct 459 ms 240640 KB Output is correct
29 Correct 457 ms 240564 KB Output is correct
30 Correct 518 ms 235048 KB Output is correct
31 Correct 425 ms 234988 KB Output is correct
32 Correct 471 ms 235004 KB Output is correct
33 Correct 434 ms 234968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 169372 KB Output is correct
2 Correct 92 ms 169424 KB Output is correct
3 Correct 79 ms 169340 KB Output is correct
4 Correct 122 ms 169532 KB Output is correct
5 Correct 167 ms 169520 KB Output is correct
6 Correct 101 ms 169520 KB Output is correct
7 Correct 82 ms 169624 KB Output is correct
8 Correct 81 ms 169572 KB Output is correct
9 Correct 414 ms 176408 KB Output is correct
10 Correct 1049 ms 330272 KB Output is correct
11 Correct 102 ms 169500 KB Output is correct
12 Correct 195 ms 169796 KB Output is correct
13 Correct 1218 ms 334360 KB Output is correct
14 Correct 975 ms 334336 KB Output is correct
15 Correct 987 ms 336172 KB Output is correct
16 Correct 1240 ms 344684 KB Output is correct
17 Correct 1341 ms 337228 KB Output is correct
18 Correct 1339 ms 338668 KB Output is correct
19 Correct 1361 ms 337416 KB Output is correct
20 Correct 1169 ms 337208 KB Output is correct
21 Correct 952 ms 337656 KB Output is correct
22 Correct 983 ms 334416 KB Output is correct
23 Correct 84 ms 169416 KB Output is correct
24 Correct 83 ms 169304 KB Output is correct
25 Correct 105 ms 169564 KB Output is correct
26 Correct 91 ms 169608 KB Output is correct
27 Correct 97 ms 169560 KB Output is correct
28 Correct 84 ms 169488 KB Output is correct
29 Correct 86 ms 169604 KB Output is correct
30 Correct 96 ms 169608 KB Output is correct
31 Correct 91 ms 169584 KB Output is correct
32 Correct 86 ms 169548 KB Output is correct
33 Correct 90 ms 169608 KB Output is correct
34 Correct 82 ms 169488 KB Output is correct
35 Correct 85 ms 169556 KB Output is correct
36 Correct 96 ms 169396 KB Output is correct
37 Correct 93 ms 169340 KB Output is correct
38 Correct 86 ms 169676 KB Output is correct
39 Correct 90 ms 169620 KB Output is correct
40 Correct 100 ms 169516 KB Output is correct
41 Correct 83 ms 169404 KB Output is correct
42 Correct 84 ms 169668 KB Output is correct
43 Correct 90 ms 169544 KB Output is correct
44 Correct 92 ms 169676 KB Output is correct
45 Correct 85 ms 169344 KB Output is correct
46 Correct 82 ms 169432 KB Output is correct
47 Correct 90 ms 169656 KB Output is correct
48 Correct 91 ms 169596 KB Output is correct
49 Correct 84 ms 169552 KB Output is correct
50 Correct 80 ms 169364 KB Output is correct
51 Correct 87 ms 169640 KB Output is correct
52 Correct 89 ms 169928 KB Output is correct
53 Correct 89 ms 169932 KB Output is correct
54 Correct 90 ms 169624 KB Output is correct
55 Correct 86 ms 169660 KB Output is correct
56 Correct 87 ms 169548 KB Output is correct
57 Correct 92 ms 169628 KB Output is correct
58 Correct 83 ms 169584 KB Output is correct
59 Correct 84 ms 169592 KB Output is correct
60 Correct 378 ms 177720 KB Output is correct
61 Correct 1056 ms 331668 KB Output is correct
62 Correct 325 ms 177972 KB Output is correct
63 Correct 241 ms 174720 KB Output is correct
64 Correct 212 ms 176952 KB Output is correct
65 Correct 468 ms 177796 KB Output is correct
66 Correct 111 ms 170340 KB Output is correct
67 Correct 593 ms 259476 KB Output is correct
68 Correct 600 ms 259776 KB Output is correct
69 Correct 1241 ms 321596 KB Output is correct
70 Correct 1228 ms 321732 KB Output is correct
71 Correct 737 ms 289168 KB Output is correct
72 Correct 795 ms 289320 KB Output is correct
73 Correct 959 ms 310348 KB Output is correct
74 Correct 947 ms 310400 KB Output is correct
75 Correct 124 ms 175920 KB Output is correct
76 Correct 692 ms 252324 KB Output is correct
77 Correct 82 ms 169392 KB Output is correct
78 Correct 81 ms 169356 KB Output is correct
79 Correct 87 ms 169352 KB Output is correct
80 Correct 92 ms 169376 KB Output is correct
81 Correct 101 ms 169456 KB Output is correct
82 Correct 99 ms 169496 KB Output is correct
83 Correct 87 ms 169496 KB Output is correct
84 Correct 91 ms 169384 KB Output is correct
85 Correct 85 ms 169332 KB Output is correct
86 Correct 90 ms 169420 KB Output is correct
87 Correct 84 ms 169416 KB Output is correct
88 Correct 87 ms 169584 KB Output is correct
89 Correct 144 ms 170060 KB Output is correct
90 Correct 188 ms 170608 KB Output is correct
91 Correct 152 ms 170204 KB Output is correct
92 Correct 599 ms 243792 KB Output is correct
93 Correct 642 ms 251640 KB Output is correct
94 Correct 821 ms 260512 KB Output is correct
95 Correct 814 ms 245592 KB Output is correct
96 Correct 619 ms 244828 KB Output is correct
97 Correct 638 ms 244948 KB Output is correct
98 Correct 652 ms 251132 KB Output is correct
99 Correct 625 ms 251288 KB Output is correct
100 Correct 678 ms 251516 KB Output is correct
101 Correct 678 ms 252356 KB Output is correct
102 Correct 578 ms 251760 KB Output is correct
103 Correct 524 ms 240572 KB Output is correct
104 Correct 459 ms 240640 KB Output is correct
105 Correct 457 ms 240564 KB Output is correct
106 Correct 518 ms 235048 KB Output is correct
107 Correct 425 ms 234988 KB Output is correct
108 Correct 471 ms 235004 KB Output is correct
109 Correct 434 ms 234968 KB Output is correct
110 Correct 172 ms 170700 KB Output is correct
111 Correct 165 ms 170236 KB Output is correct
112 Correct 1120 ms 333652 KB Output is correct
113 Correct 1314 ms 325536 KB Output is correct
114 Correct 828 ms 284312 KB Output is correct
115 Correct 187 ms 223228 KB Output is correct
116 Correct 788 ms 256252 KB Output is correct
117 Correct 916 ms 269960 KB Output is correct
118 Correct 693 ms 250832 KB Output is correct
119 Correct 689 ms 250752 KB Output is correct
120 Correct 140 ms 177048 KB Output is correct
121 Correct 928 ms 266104 KB Output is correct
122 Correct 975 ms 263648 KB Output is correct
123 Correct 1505 ms 328892 KB Output is correct
124 Correct 1085 ms 328876 KB Output is correct
125 Correct 1514 ms 329516 KB Output is correct
126 Correct 1315 ms 345252 KB Output is correct
127 Correct 1385 ms 334432 KB Output is correct
128 Correct 1202 ms 331580 KB Output is correct
129 Correct 1436 ms 335812 KB Output is correct
130 Correct 1225 ms 335524 KB Output is correct