Submission #709150

# Submission time Handle Problem Language Result Execution time Memory
709150 2023-03-13T06:56:37 Z minhcool Jail (JOI22_jail) C++17
61 / 100
5000 ms 49164 KB
#include<bits/stdc++.h>
using namespace std;
 
#define int long long
#define fi first
#define se second
#define pb push_back
 
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 3e5 + 5;
const int mod = 1e9 + 7, oo = 1e18 + 7;
 
int n, m;
 
vector<int> Adj[N];
vector<int> Adj2[N];// all nodes j that go after i
 
int deg[N];
 
int anc[N][20], d[N]; 
 
void init(){
	for(int i = 1; i <= max(n, m); i++){
		Adj[i].clear();
		Adj2[i].clear();
		deg[i] = 0;
		for(int j = 0; j < 19; j++) anc[i][j] = 0;
		d[i] = 0;		
	}
}
 
void dfs(int u, int p){
	for(auto v : Adj[u]){
		if(v == p) continue;
		d[v] = d[u] + 1;
		anc[v][0] = u;
		for(int i = 1; i <= 19; i++) anc[v][i] = anc[anc[v][i - 1]][i - 1];
		dfs(v, u);
	}
}
 
int lca(int x, int y){
	if(d[x] > d[y]) swap(x, y);
	for(int i = 19; i >= 0; i--) if((d[y] - d[x]) >= (1LL << i)) y = anc[y][i];
	if(x == y) return x;
	for(int i = 19; i >= 0; i--){
		if(anc[x][i] != anc[y][i]){
			x = anc[x][i], y = anc[y][i];
		}
	}
	return anc[x][0];
}
 
int dist(int x, int y){
	return d[x] + d[y] - 2 * d[lca(x, y)];
}
 
bool in(int a, int b, int c){
	return (dist(a, c) + dist(b, c) == dist(a, b));
}
 
int st[N], en[N];
 
void process(){
    cin >> n;
    init();
    for(int i = 1; i < n; i++){
    	int x, y;
    	cin >> x >> y;
    	Adj[x].pb(y);
    	Adj[y].pb(x);
	}
	cin >> m;
	dfs(1, 1);
	for(int i = 1; i <= m; i++) cin >> st[i] >> en[i];
	for(int i = 1; i <= m; i++){
		for(int j = i + 1; j <= m; j++){
			assert(st[i] != st[j]);
			assert(en[i] != en[j]);
			bool ck1 = in(st[i], en[i], st[j]);
			bool ck2 = in(st[i], en[i], en[j]);
			bool ck3 = in(st[j], en[j], st[i]);
			bool ck4 = in(st[j], en[j], en[i]);
			//cout << ck1 << " " << ck2 << " " << ck3 << " " << ck4 << "\n";
			bool temp1 = (ck1 | ck4), temp2 = (ck2 | ck3);
			if(temp1 & temp2){
				cout << "No\n";
				return;
			}
			if(temp1){
				Adj2[j].pb(i);
				deg[i]++;
			}
			if(temp2){
				Adj2[i].pb(j);
				deg[j]++;
			}
		}
	}
	queue<int> q;
	for(int i = 1; i <= m; i++) if(!deg[i]) q.push(i);
	int cnt = 0;
	while(!q.empty()){
		cnt++;
		int u = q.front();
		//cout << u << "\n";
		q.pop();
		for(auto v : Adj2[u]){
			deg[v]--;
			if(!deg[v]) q.push(v);
		}
	}
	if(cnt == m) cout << "Yes\n";
	else cout << "No\n";
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    //freopen("KEK.inp", "r", stdin);
    //freopen("KEK.out", "w", stdout);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) process();
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 20 ms 14820 KB Output is correct
5 Correct 33 ms 15188 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 9 ms 14548 KB Output is correct
8 Correct 45 ms 14548 KB Output is correct
9 Correct 869 ms 18672 KB Output is correct
10 Correct 135 ms 49084 KB Output is correct
11 Correct 17 ms 14548 KB Output is correct
12 Correct 146 ms 15340 KB Output is correct
13 Execution timed out 5034 ms 49164 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14384 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 10 ms 14428 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 12 ms 14428 KB Output is correct
6 Correct 9 ms 14548 KB Output is correct
7 Correct 10 ms 14420 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14424 KB Output is correct
11 Correct 9 ms 14476 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14384 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 10 ms 14428 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 12 ms 14428 KB Output is correct
6 Correct 9 ms 14548 KB Output is correct
7 Correct 10 ms 14420 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14424 KB Output is correct
11 Correct 9 ms 14476 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 9 ms 14548 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 11 ms 14460 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 12 ms 14472 KB Output is correct
21 Correct 12 ms 14432 KB Output is correct
22 Correct 10 ms 14548 KB Output is correct
23 Correct 9 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 8 ms 14476 KB Output is correct
27 Correct 9 ms 14524 KB Output is correct
28 Correct 8 ms 14352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14384 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 10 ms 14428 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 12 ms 14428 KB Output is correct
6 Correct 9 ms 14548 KB Output is correct
7 Correct 10 ms 14420 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14424 KB Output is correct
11 Correct 9 ms 14476 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 9 ms 14548 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 11 ms 14460 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 12 ms 14472 KB Output is correct
21 Correct 12 ms 14432 KB Output is correct
22 Correct 10 ms 14548 KB Output is correct
23 Correct 9 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 8 ms 14476 KB Output is correct
27 Correct 9 ms 14524 KB Output is correct
28 Correct 8 ms 14352 KB Output is correct
29 Correct 34 ms 14560 KB Output is correct
30 Correct 23 ms 14556 KB Output is correct
31 Correct 10 ms 14548 KB Output is correct
32 Correct 28 ms 14548 KB Output is correct
33 Correct 12 ms 14468 KB Output is correct
34 Correct 49 ms 14544 KB Output is correct
35 Correct 39 ms 14560 KB Output is correct
36 Correct 103 ms 14532 KB Output is correct
37 Correct 70 ms 14520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14384 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 10 ms 14428 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 12 ms 14428 KB Output is correct
6 Correct 9 ms 14548 KB Output is correct
7 Correct 10 ms 14420 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 10 ms 14420 KB Output is correct
10 Correct 9 ms 14424 KB Output is correct
11 Correct 9 ms 14476 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 9 ms 14548 KB Output is correct
17 Correct 9 ms 14420 KB Output is correct
18 Correct 11 ms 14460 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 12 ms 14472 KB Output is correct
21 Correct 12 ms 14432 KB Output is correct
22 Correct 10 ms 14548 KB Output is correct
23 Correct 9 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 8 ms 14476 KB Output is correct
27 Correct 9 ms 14524 KB Output is correct
28 Correct 8 ms 14352 KB Output is correct
29 Correct 34 ms 14560 KB Output is correct
30 Correct 23 ms 14556 KB Output is correct
31 Correct 10 ms 14548 KB Output is correct
32 Correct 28 ms 14548 KB Output is correct
33 Correct 12 ms 14468 KB Output is correct
34 Correct 49 ms 14544 KB Output is correct
35 Correct 39 ms 14560 KB Output is correct
36 Correct 103 ms 14532 KB Output is correct
37 Correct 70 ms 14520 KB Output is correct
38 Correct 913 ms 18672 KB Output is correct
39 Correct 164 ms 49120 KB Output is correct
40 Correct 1127 ms 17252 KB Output is correct
41 Correct 2822 ms 17072 KB Output is correct
42 Correct 1545 ms 17420 KB Output is correct
43 Correct 76 ms 17104 KB Output is correct
44 Correct 1438 ms 15228 KB Output is correct
45 Correct 229 ms 40744 KB Output is correct
46 Correct 284 ms 40936 KB Output is correct
47 Correct 356 ms 44632 KB Output is correct
48 Correct 361 ms 44532 KB Output is correct
49 Correct 85 ms 40908 KB Output is correct
50 Correct 316 ms 40924 KB Output is correct
51 Correct 108 ms 42188 KB Output is correct
52 Correct 78 ms 42032 KB Output is correct
53 Correct 2037 ms 16600 KB Output is correct
54 Correct 277 ms 41548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 9 ms 14420 KB Output is correct
5 Correct 18 ms 14548 KB Output is correct
6 Correct 11 ms 14436 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 91 ms 14536 KB Output is correct
13 Correct 80 ms 14924 KB Output is correct
14 Correct 388 ms 15432 KB Output is correct
15 Correct 344 ms 15224 KB Output is correct
16 Execution timed out 5076 ms 41544 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 20 ms 14820 KB Output is correct
5 Correct 33 ms 15188 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 9 ms 14548 KB Output is correct
8 Correct 45 ms 14548 KB Output is correct
9 Correct 869 ms 18672 KB Output is correct
10 Correct 135 ms 49084 KB Output is correct
11 Correct 17 ms 14548 KB Output is correct
12 Correct 146 ms 15340 KB Output is correct
13 Execution timed out 5034 ms 49164 KB Time limit exceeded
14 Halted 0 ms 0 KB -