답안 #990539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
990539 2024-05-30T15:44:21 Z OAleksa Jail (JOI22_jail) C++14
0 / 100
7 ms 10912 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 120069;
const int K = 20;
int n, m, a[N], b[N];
int tin[N], tout[N], timer, up[N][K], dep[N], is, vis[N];
vector<int> g[N];
vector<int> gg[N];
bool anc(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
	if (anc(a, b))
		return a;
	else if (anc(b, a))
		return b;
	for (int i = K - 1;i >= 0;i--) {
		if (!anc(up[a][i], b) && up[a][i] > 0)
			a = up[a][i];
	}
	return up[a][0];
}
void dfs(int v, int p) {
	up[v][0] = p;
	tin[v] = ++timer;
	for (int i = 1;i < K;i++)
		up[v][i] = up[up[v][i - 1]][i - 1];
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dep[u] = dep[v] + 1;
		dfs(u, v);
	}
	tout[v] = timer;
}
void dfs(int v) {
	if (vis[v]) {
		is += (vis[v] == 1);
		return;
	}
	vis[v] = 1;
	for (auto u : gg[v])
		dfs(u);
	vis[v] = 2;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int tt;
	cin >> tt;
	while (tt--) {
		cin >> n;
		timer = is = 0;
		for (int i = 1;i <= n;i++) {
			g[i].clear();
			gg[i].clear();
			vis[i] = 0;
			for (int j = 0;j < K;j++)
				up[i][j] = 0;
		}
		for (int i = 1;i <= n - 1;i++) {
			int a, b;
			cin >> a >> b;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		cin >> m;
		for (int i = 1;i <= m;i++)
			cin >> a[i] >> b[i];
		dep[1] = 0;
		dfs(1, 0);
		for (int i = 1;i <= m;i++) {
			for (int j = i + 1;j <= m;j++) {
				// da li je i u j
				//ako b[i] pripada onda j mora zavrsim pre i
				int dis = dep[a[j]] + dep[b[j]] - 2 * dep[lca(a[j], b[j])];
				int dd1 = dep[a[j]] + dep[b[i]] - 2 * dep[lca(a[j], b[i])];
				int dd2 = dep[b[j]] + dep[b[i]] - 2 * dep[lca(b[j], b[i])];
				if (dis == dd1 + dd2) 
					gg[j].push_back(i);
					dd1 = dep[a[j]] + dep[a[i]] - 2 * dep[lca(a[j], a[i])];
					dd2 = dep[b[j]] + dep[a[i]] - 2 * dep[lca(b[j], a[i])];
					if (dis == dd1 + dd2)
						gg[i].push_back(j);
			}
		}
		for (int i = 1;i <= n;i++) {
			if (!vis[i]) {
				dfs(i);
			}
		}
		if (is)
			cout << "No\n";
		else
			cout << "Yes\n";
	}
  return 0; 
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:81:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   81 |     if (dis == dd1 + dd2)
      |     ^~
jail.cpp:83:6: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   83 |      dd1 = dep[a[j]] + dep[a[i]] - 2 * dep[lca(a[j], a[i])];
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 1 ms 10844 KB Output is correct
4 Incorrect 7 ms 10912 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10844 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10844 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10844 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10844 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 1 ms 10844 KB Output is correct
4 Correct 2 ms 10852 KB Output is correct
5 Incorrect 6 ms 10840 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 1 ms 10844 KB Output is correct
4 Incorrect 7 ms 10912 KB Output isn't correct
5 Halted 0 ms 0 KB -