Submission #631946

# Submission time Handle Problem Language Result Execution time Memory
631946 2022-08-19T08:00:41 Z S2speed Jail (JOI22_jail) C++17
77 / 100
5000 ms 686824 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize ("Ofast")

#define all(x) x.begin() , x.end()
typedef long long ll;
typedef pair<ll , ll> pll;

const ll maxn = 2e5 + 17 , inf = 2e16;

vector<ll> adj[maxn];
ll jad[maxn][20] , bt[maxn] , ft[maxn] , tme , dis[maxn] , dep = 0;

void jDFS(ll r , ll par){
	bt[r] = tme++;
	dis[r] = dep++;
	jad[r][0] = par;
	for(ll j = 1 ; j < 20 ; j++){
		if(jad[r][j - 1] == -1) break;
		jad[r][j] = jad[jad[r][j - 1]][j - 1];
	}
	for(auto i : adj[r]){
		if(i == par) continue;
		jDFS(i , r);
	}
	ft[r] = tme;
	dep--;
	return;
}

ll find_jad(ll v , ll d){
	d = dis[v] - d;
	for(ll j = 19 ; ~j ; j--){
		if(d & (1 << j)){
			v = jad[v][j];
		}
	}
	return v;
}

bool too_masir(ll v , ll u , ll k){
	bool zv = (bt[v] >= bt[k] && ft[v] <= ft[k]) , zu = (bt[u] >= bt[k] && ft[u] <= ft[k]);
	if(!zv && !zu) return false;
	if(zv ^ zu) return true;
	ll hv = find_jad(v , dis[k] + 1) , hu = find_jad(u , dis[k] + 1);
	return (hv != hu);
}

ll n , m;
ll a[maxn] , b[maxn] , d[maxn] , x[maxn] , y[maxn];
vector<ll> jda[maxn];
priority_queue<pll , vector<pll> , greater<pll>> pq;
vector<pll> s1;

bool sub1(){
	s1.clear();
	for(ll i = 0 ; i < m ; i++){
		s1.push_back({a[i] , b[i]});
	}
	sort(all(s1));
	for(ll i = 1 ; i < m ; i++){
		if(s1[i].second < s1[i - 1].second) return false;
	}
	return true;
}

bool sub6(){
	for(ll i = 0 ; i < m ; i++){
		int v = a[i];
		while(true){
			if(x[v] != -1 && x[v] != i){
				jda[i].push_back(x[v]); d[x[v]]++;
			}
			if(y[v] != -1 && y[v] != i){
				jda[y[v]].push_back(i); d[i]++;
			}
			if(bt[b[i]] >= bt[v] && ft[b[i]] <= ft[v]) break;
			v = jad[v][0];
		}
		v = b[i];
		while(!(bt[a[i]] >= bt[v] && ft[a[i]] <= ft[v])){
			if(x[v] != -1 && x[v] != i){
				jda[i].push_back(x[v]); d[x[v]]++;
			}
			if(y[v] != -1 && y[v] != i){
				jda[y[v]].push_back(i); d[i]++;
			}
			v = jad[v][0];
		}
	}
	for(ll i = 0 ; i < m ; i++){
		pq.push({d[i] , i});
	}
	while(!pq.empty()){
		pll p = pq.top(); pq.pop();
		ll v = p.second;
		if(d[v] != p.first) continue;
		if(d[v] != 0){
			return false;
		}
		for(auto i : jda[v]){
			d[i]--;
			pq.push({d[i] , i});
		}
	}
	return true;
}

void solve(){
	while(!pq.empty()) pq.pop();
	tme = 0;
	cin>>n;
	for(ll i = 0 ; i < n ; i++){
		adj[i].clear(); jda[i].clear(); d[i] = 0; x[i] = y[i] = -1;
		for(ll j = 0 ; j < 20 ; j++) jad[i][j] = -1;
	}
	bool c = true;
	for(ll i = 1 ; i < n ; i++){
		ll v , u;
		cin>>v>>u; v--; u--;
		adj[v].push_back(u); adj[u].push_back(v);
		c &= (v == i - 1 && u == i);
	}
	jDFS(0 , -1);
	cin>>m;
	for(ll i = 0 ; i < m ; i++){
		cin>>a[i]>>b[i]; a[i]--; b[i]--;
		x[a[i]] = i; y[b[i]] = i;
	}
	if(c){
		cout<<(sub1() ? "Yes\n" : "No\n");
		return;
	}
	if(m > 500){
		cout<<(sub6() ? "Yes\n" : "No\n");
		return;
	}
	for(ll i = 0 ; i < m ; i++){
		for(ll j = 0 ; j < m ; j++){
			if(j == i) continue;
			if(too_masir(a[i] , b[i] , a[j])){
				jda[i].push_back(j); d[j]++;
			}
			if(too_masir(a[i] , b[i] , b[j])){
				jda[j].push_back(i); d[i]++;
			}
		}
	}
	for(ll i = 0 ; i < m ; i++){
		pq.push({d[i] , i});
	}
	while(!pq.empty()){
		pll p = pq.top(); pq.pop();
		ll v = p.second;
		if(d[v] != p.first) continue;
		if(d[v] != 0){
			cout<<"No\n";
			return;
		}
		for(auto i : jda[v]){
			d[i]--;
			pq.push({d[i] , i});
		}
	}
	cout<<"Yes\n";
	return;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	ll T;
	cin>>T;
	while(T--) solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9664 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 12 ms 9684 KB Output is correct
5 Correct 21 ms 9808 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 28 ms 11476 KB Output is correct
10 Correct 59 ms 43540 KB Output is correct
11 Correct 8 ms 9684 KB Output is correct
12 Correct 24 ms 9808 KB Output is correct
13 Correct 72 ms 45480 KB Output is correct
14 Correct 66 ms 45412 KB Output is correct
15 Correct 79 ms 45484 KB Output is correct
16 Correct 110 ms 47512 KB Output is correct
17 Correct 64 ms 45676 KB Output is correct
18 Correct 76 ms 47568 KB Output is correct
19 Correct 81 ms 45636 KB Output is correct
20 Correct 75 ms 45568 KB Output is correct
21 Correct 87 ms 45624 KB Output is correct
22 Correct 66 ms 45648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9848 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9784 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9764 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9848 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9784 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9764 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 6 ms 9848 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 6 ms 9812 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 6 ms 9848 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9812 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9812 KB Output is correct
26 Correct 5 ms 9764 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9848 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9784 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9764 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 6 ms 9848 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 6 ms 9812 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 6 ms 9848 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9812 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9812 KB Output is correct
26 Correct 5 ms 9764 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9812 KB Output is correct
30 Correct 7 ms 9812 KB Output is correct
31 Correct 10 ms 9908 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9812 KB Output is correct
34 Correct 8 ms 9812 KB Output is correct
35 Correct 11 ms 10156 KB Output is correct
36 Correct 10 ms 9812 KB Output is correct
37 Correct 10 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9848 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9784 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9764 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 6 ms 9848 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 6 ms 9812 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 6 ms 9848 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9812 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9812 KB Output is correct
26 Correct 5 ms 9764 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9812 KB Output is correct
30 Correct 7 ms 9812 KB Output is correct
31 Correct 10 ms 9908 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9812 KB Output is correct
34 Correct 8 ms 9812 KB Output is correct
35 Correct 11 ms 10156 KB Output is correct
36 Correct 10 ms 9812 KB Output is correct
37 Correct 10 ms 9836 KB Output is correct
38 Correct 38 ms 11476 KB Output is correct
39 Correct 55 ms 43468 KB Output is correct
40 Correct 229 ms 13592 KB Output is correct
41 Correct 107 ms 11344 KB Output is correct
42 Correct 126 ms 13772 KB Output is correct
43 Correct 34 ms 11404 KB Output is correct
44 Correct 52 ms 10512 KB Output is correct
45 Correct 66 ms 38460 KB Output is correct
46 Correct 66 ms 38504 KB Output is correct
47 Correct 66 ms 41212 KB Output is correct
48 Correct 68 ms 41240 KB Output is correct
49 Correct 68 ms 38604 KB Output is correct
50 Correct 74 ms 38604 KB Output is correct
51 Correct 60 ms 39376 KB Output is correct
52 Correct 61 ms 39360 KB Output is correct
53 Correct 68 ms 11876 KB Output is correct
54 Correct 74 ms 39092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 8 ms 9788 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9684 KB Output is correct
12 Correct 10 ms 9812 KB Output is correct
13 Correct 53 ms 9852 KB Output is correct
14 Correct 38 ms 9812 KB Output is correct
15 Correct 46 ms 9868 KB Output is correct
16 Correct 76 ms 39632 KB Output is correct
17 Correct 157 ms 50288 KB Output is correct
18 Correct 341 ms 88260 KB Output is correct
19 Correct 83 ms 39844 KB Output is correct
20 Correct 98 ms 41728 KB Output is correct
21 Correct 112 ms 41528 KB Output is correct
22 Correct 146 ms 56780 KB Output is correct
23 Correct 120 ms 54248 KB Output is correct
24 Correct 305 ms 69488 KB Output is correct
25 Correct 178 ms 69720 KB Output is correct
26 Correct 317 ms 69548 KB Output is correct
27 Correct 192 ms 51516 KB Output is correct
28 Correct 142 ms 49448 KB Output is correct
29 Correct 120 ms 49472 KB Output is correct
30 Correct 111 ms 44500 KB Output is correct
31 Correct 91 ms 44484 KB Output is correct
32 Correct 101 ms 44488 KB Output is correct
33 Correct 87 ms 44488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9664 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 12 ms 9684 KB Output is correct
5 Correct 21 ms 9808 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 28 ms 11476 KB Output is correct
10 Correct 59 ms 43540 KB Output is correct
11 Correct 8 ms 9684 KB Output is correct
12 Correct 24 ms 9808 KB Output is correct
13 Correct 72 ms 45480 KB Output is correct
14 Correct 66 ms 45412 KB Output is correct
15 Correct 79 ms 45484 KB Output is correct
16 Correct 110 ms 47512 KB Output is correct
17 Correct 64 ms 45676 KB Output is correct
18 Correct 76 ms 47568 KB Output is correct
19 Correct 81 ms 45636 KB Output is correct
20 Correct 75 ms 45568 KB Output is correct
21 Correct 87 ms 45624 KB Output is correct
22 Correct 66 ms 45648 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 7 ms 9848 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 6 ms 9812 KB Output is correct
30 Correct 7 ms 9784 KB Output is correct
31 Correct 7 ms 9812 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9764 KB Output is correct
34 Correct 5 ms 9812 KB Output is correct
35 Correct 6 ms 9812 KB Output is correct
36 Correct 5 ms 9684 KB Output is correct
37 Correct 5 ms 9684 KB Output is correct
38 Correct 6 ms 9848 KB Output is correct
39 Correct 5 ms 9812 KB Output is correct
40 Correct 6 ms 9812 KB Output is correct
41 Correct 5 ms 9684 KB Output is correct
42 Correct 6 ms 9848 KB Output is correct
43 Correct 7 ms 9812 KB Output is correct
44 Correct 6 ms 9812 KB Output is correct
45 Correct 5 ms 9684 KB Output is correct
46 Correct 5 ms 9684 KB Output is correct
47 Correct 5 ms 9812 KB Output is correct
48 Correct 5 ms 9764 KB Output is correct
49 Correct 6 ms 9812 KB Output is correct
50 Correct 5 ms 9684 KB Output is correct
51 Correct 6 ms 9812 KB Output is correct
52 Correct 7 ms 9812 KB Output is correct
53 Correct 10 ms 9908 KB Output is correct
54 Correct 6 ms 9812 KB Output is correct
55 Correct 6 ms 9812 KB Output is correct
56 Correct 8 ms 9812 KB Output is correct
57 Correct 11 ms 10156 KB Output is correct
58 Correct 10 ms 9812 KB Output is correct
59 Correct 10 ms 9836 KB Output is correct
60 Correct 38 ms 11476 KB Output is correct
61 Correct 55 ms 43468 KB Output is correct
62 Correct 229 ms 13592 KB Output is correct
63 Correct 107 ms 11344 KB Output is correct
64 Correct 126 ms 13772 KB Output is correct
65 Correct 34 ms 11404 KB Output is correct
66 Correct 52 ms 10512 KB Output is correct
67 Correct 66 ms 38460 KB Output is correct
68 Correct 66 ms 38504 KB Output is correct
69 Correct 66 ms 41212 KB Output is correct
70 Correct 68 ms 41240 KB Output is correct
71 Correct 68 ms 38604 KB Output is correct
72 Correct 74 ms 38604 KB Output is correct
73 Correct 60 ms 39376 KB Output is correct
74 Correct 61 ms 39360 KB Output is correct
75 Correct 68 ms 11876 KB Output is correct
76 Correct 74 ms 39092 KB Output is correct
77 Correct 5 ms 9684 KB Output is correct
78 Correct 5 ms 9684 KB Output is correct
79 Correct 5 ms 9684 KB Output is correct
80 Correct 5 ms 9684 KB Output is correct
81 Correct 8 ms 9788 KB Output is correct
82 Correct 6 ms 9812 KB Output is correct
83 Correct 5 ms 9812 KB Output is correct
84 Correct 5 ms 9684 KB Output is correct
85 Correct 5 ms 9684 KB Output is correct
86 Correct 5 ms 9684 KB Output is correct
87 Correct 7 ms 9684 KB Output is correct
88 Correct 10 ms 9812 KB Output is correct
89 Correct 53 ms 9852 KB Output is correct
90 Correct 38 ms 9812 KB Output is correct
91 Correct 46 ms 9868 KB Output is correct
92 Correct 76 ms 39632 KB Output is correct
93 Correct 157 ms 50288 KB Output is correct
94 Correct 341 ms 88260 KB Output is correct
95 Correct 83 ms 39844 KB Output is correct
96 Correct 98 ms 41728 KB Output is correct
97 Correct 112 ms 41528 KB Output is correct
98 Correct 146 ms 56780 KB Output is correct
99 Correct 120 ms 54248 KB Output is correct
100 Correct 305 ms 69488 KB Output is correct
101 Correct 178 ms 69720 KB Output is correct
102 Correct 317 ms 69548 KB Output is correct
103 Correct 192 ms 51516 KB Output is correct
104 Correct 142 ms 49448 KB Output is correct
105 Correct 120 ms 49472 KB Output is correct
106 Correct 111 ms 44500 KB Output is correct
107 Correct 91 ms 44484 KB Output is correct
108 Correct 101 ms 44488 KB Output is correct
109 Correct 87 ms 44488 KB Output is correct
110 Correct 58 ms 10868 KB Output is correct
111 Correct 38 ms 10440 KB Output is correct
112 Execution timed out 5113 ms 686824 KB Time limit exceeded
113 Halted 0 ms 0 KB -