답안 #994295

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994295 2024-06-07T10:27:21 Z zsombor Jail (JOI22_jail) C++17
0 / 100
29 ms 37236 KB
#include <iostream>
#include <vector>
using namespace std;

int n, m, sz, c;
bool ans;
vector <vector <int>> g(2e5);
vector <int> dep(2e5, 0);
vector <vector <int>> lift(2e5, vector <int>(20, 0));
vector <int> v1(2e5, 0);
vector <int> v2(2e5, 0);
vector <int> kez(2e5, 0);
vector <int> veg(2e5, 0);
vector <vector <int>> g2(2e5);
vector <bool> be(2e5);
vector <bool> ki(2e5);

//vector <bool> del(2e5);

void reset() {
	ans = true;
	for (int i = 0; i <= n; i++) {
		g[i].clear();
		dep[i] = 0;
		fill(lift[i].begin(), lift[i].end(), 0);
		g2[i].clear();
		be[i] = ki[i] = false;
		kez[i] = veg[i] = 0;
	}
}

/*int get_c(int x) {
	if (del[x]) return 0;
	del[x] = true;
	int mx = 0, sum = 1;
	for (int i : g[x]) {

	}
	return sum;
}

void cd(int x) {
	sz = get_c(x);
	get_c(x);
}*/

void dfs(int x) {
	for (int i : g[x]) {
		if (i == lift[x][0]) continue;
		dep[i] = dep[x] + 1;
		lift[i][0] = x;
		dfs(i);
	}
}

int lca(int a, int b) {
	if (dep[a] < dep[b]) swap(a, b);
	for (int j = 19; j >= 0; j--) {
		if (dep[lift[a][j]] >= dep[b]) a = lift[a][j];
	}
	if (a == b) return a;
	for (int j = 19; j >= 0; j--) {
		if (lift[a][j] == lift[b][j]) continue;
		a = lift[a][j];
		b = lift[b][j];
	}
	return lift[a][0];
}

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

bool on_path(int a, int b, int c) {
	int l = lca(a, b);
	if (dep[c] < dep[l]) return false;
	if (par(a, dep[a] - dep[c]) == c) return true;
	if (par(b, dep[b] - dep[c]) == c) return true;
	return false;
}

void get_path(int i, int a, int c) {
	while (true){
		if (kez[a] && kez[a] != i) g2[kez[a]].push_back(i);
		if (veg[a] && veg[a] != i) g2[i].push_back(veg[a]);
		if (a == c) break;
		a = lift[a][0];
	}
}

void tp(int x) {
	if (be[x] && !ki[x]) ans = false;
	if (be[x]) return;
	be[x] = true;
	for (int i : g2[x]) tp(i);
	ki[x] = true;
}

void solve() {
	cin >> n;
	reset();
	int a, b;
	for (int i = 0; i < n - 1; i++) {
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(1);
	for (int j = 1; j < 20; j++) {
		for (int i = 1; i <= n; i++) {
			lift[i][j] = lift[lift[i][j - 1]][j - 1];
		}
	}
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		v1[i] = a; v2[i] = b;
		kez[a] = i;
		veg[b] = i;
	}
	for (int i = 0; i < m; i++) {
		a = v1[i]; b = v2[i];
		int c = lca(a, b);
		get_path(i, a, c);
		get_path(i, b, c);
	}
	for (int i = 0; i < m; i++) tp(i);
	cout << (ans ? "Yes\n" : "No\n");
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	while (t--) solve();
}

/*

1
4
1 2
1 3
1 4
3
2 3
3 4
4 2

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Correct 16 ms 37212 KB Output is correct
4 Incorrect 23 ms 37212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Incorrect 18 ms 37212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Incorrect 18 ms 37212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Incorrect 18 ms 37212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Incorrect 18 ms 37212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 37208 KB Output is correct
2 Correct 29 ms 37212 KB Output is correct
3 Correct 19 ms 37236 KB Output is correct
4 Correct 27 ms 37212 KB Output is correct
5 Incorrect 22 ms 37212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 37208 KB Output is correct
2 Correct 17 ms 37212 KB Output is correct
3 Correct 16 ms 37212 KB Output is correct
4 Incorrect 23 ms 37212 KB Output isn't correct
5 Halted 0 ms 0 KB -