Submission #560882

# Submission time Handle Problem Language Result Execution time Memory
560882 2022-05-12T04:10:48 Z 8e7 Jail (JOI22_jail) C++17
0 / 100
5 ms 5972 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];
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 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);
		int m;
		cin >> m;
		auto addedge = [&] (int u, int v) {
			g[u].push_back(v);
			deg[v]++;
		};
		for (int i = 0;i < m;i++) {
			int u, v;
			cin >> u >> v;
			int tu = u, tv = v;	
			while (tu != tv) {
				if (dep[tu] > dep[tv]) tu = anc[0][tu];
				else tv = anc[0][tv];
			}
			if (tu == v) {
				while (u != v) {
					addedge(u, v);
					u = anc[0][u];			
				}
			} else {
				tv = anc[0][v];
				while (u != tv) {
					if (dep[u] > dep[tv]) {
						addedge(u, v);
						u = anc[0][u];
					} else {
						addedge(tv, v);
						tv = anc[0][tv];
					}
				}
			}
		}
		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 3 ms 5844 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 3 ms 5972 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5964 KB Output is correct
2 Incorrect 4 ms 5968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5964 KB Output is correct
2 Incorrect 4 ms 5968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5964 KB Output is correct
2 Incorrect 4 ms 5968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5964 KB Output is correct
2 Incorrect 4 ms 5968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 4 ms 5968 KB Output is correct
3 Correct 5 ms 5972 KB Output is correct
4 Incorrect 3 ms 5972 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 3 ms 5972 KB Output isn't correct
4 Halted 0 ms 0 KB -