Submission #560912

# Submission time Handle Problem Language Result Execution time Memory
560912 2022-05-12T04:48:43 Z 8e7 Jail (JOI22_jail) C++17
0 / 100
5 ms 6108 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 120005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn], g[maxn];
int anc[18][maxn], dep[maxn], deg[maxn];
pii ed[maxn];
void dfs(int n, int par, int d) {
	anc[0][n] = par;
	dep[n] = d;	
	for (int v:adj[n]) {
		if (v != par) {
			dfs(v, n, d + 1);
		}
	}
}
int lca(int a, int b) {
	if (dep[a] < dep[b]) swap(a, b);
	int hdif = dep[a] - dep[b], cnt = 0;
	while (hdif) {
		if (hdif & 1) a = anc[cnt][a];
		hdif >>= 1, cnt++;
	}
	if (a == b) return a;
	for (int i = 17;i >= 0;i--) {
		if (anc[i][a] != anc[i][b]) {
			a = anc[i][a], b = anc[i][b];
		}
	}
	return anc[0][a];
}
int getdis(int u, int v) {
	return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
bool onl(int u, int v, int x) {
	return getdis(u, v) == getdis(u, x) + getdis(x, v);
}
int main() {
	io
	int T;
	cin >> T;
	while (T--) {

		int n;
		cin >> n;
		for (int i = 1;i <= n;i++) {
			adj[i].clear();
			g[i].clear();
			deg[i] = 0;
		}
		for (int i = 0;i < n - 1;i++) {
			int u,v;
			cin >> u >> v;
			adj[u].push_back(v);
			adj[v].push_back(u);
		}
		dfs(1, 0, 0);
		for (int i = 1;i < 18;i++) {
			for (int j = 1;j <= n;j++) anc[i][j] = anc[i-1][anc[i-1][j]];
		}
		int m;
		cin >> m;
		auto addedge = [&] (int u, int v) {
			//debug("edge", u, v);
			g[u].push_back(v);
			deg[v]++;
		};
		for (int i = 1;i <= m;i++) {
			int u, v;
			cin >> u >> v;
			ed[i] = {u, v};
		}
		for (int i = 1;i <= m;i++) {
			for (int j = i+1;j <= m;j++) {
				if (onl(ed[i].ff, ed[i].ss, ed[j].ff)) addedge(j, i);
				if (onl(ed[i].ff, ed[i].ss, ed[j].ss)) addedge(i, j);
				if (onl(ed[j].ff, ed[j].ss, ed[i].ff)) addedge(i, j);
				if (onl(ed[j].ff, ed[j].ss, ed[i].ff)) addedge(j, i);
			}
		}
		queue<int> que;
		for (int i = 1;i <= n;i++) {
			if (deg[i] == 0) que.push(i);
		}
		int cnt = 0;
		while (que.size()) {
			int cur = que.front();que.pop();
			cnt++;
			for (int v:g[cur]) {
				if (--deg[v] == 0) que.push(v);
			}
		}
		cout << (cnt == n ? "Yes" : "No") << "\n";	
	}
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5972 KB Output is correct
2 Incorrect 4 ms 5972 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Incorrect 5 ms 6108 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Incorrect 5 ms 6108 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Incorrect 5 ms 6108 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Incorrect 5 ms 6108 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Incorrect 4 ms 5972 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5972 KB Output is correct
2 Incorrect 4 ms 5972 KB Output isn't correct
3 Halted 0 ms 0 KB -