Submission #884867

#TimeUsernameProblemLanguageResultExecution timeMemory
884867MinaRagy06Jail (JOI22_jail)C++17
0 / 100
5 ms604 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		for (int i = 1, u, v; i < n; i++) {
			cin >> u >> v;
			u--, v--;
		}
		int m;
		cin >> m;
		int st[n]{}, en[n]{}, st2[n]{}, en2[n]{};
		for (int i = 0; i < m; i++) {
			int s, e;
			cin >> s >> e;
			s--, e--;
			if (s < e) {
				st[s]++;
				en[e]++;
			} else {
				st2[s]++;
				en2[e]++;
			}
		}
		int cnt[n]{}, c = 0;
		for (int i = 0; i < n; i++) {
			c += st[i];
			if (c) {
				cnt[i]++;
			}
			c -= en[i];
		}
		c = 0;
		bool ok = 1;
		for (int i = n - 1; i >= 0; i--) {
			c += st2[i];
			if (c && cnt[i]) {
				ok = 0;
			}
			c -= en2[i];
		}
		cout << (ok? "Yes\n" : "No\n");
	}
	return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...