#include <bits/stdc++.h>
using namespace std;
using ll = long long;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
struct BIT {
int n, lg;
vector<int> bit;
BIT() {}
BIT(int _n) { init(_n); }
BIT(vector<int> &a) { init(a); }
void init(int x) {
n = x; lg = 0;
while(2 * (1 << lg) <= n) lg++;
bit.resize(n + 2,0);
}
void init(vector<int> &a) {
init(a.size());
for(int i = 1; i <= n; i++) {
bit[i] += a[i - 1]; //check this
if(i + (i & -i) <= n)
bit[i + (i & -i)] += bit[i];
}
}
int qry(int x) {
x = min(x, n);
int ans = 0;
for(; x > 0; x -= (x & -x))
ans += bit[x];
return ans;
}
int qry(int l, int r) { return qry(r) - qry(l - 1); }
void upd(int x, int v) {
if(x <= 0) return;
for(; x <= n; x += (x & -x))
bit[x] += v;
}
int lower_bound(int sum) {
int x = 0;
for(int k = lg; k >= 0; k--) {
int nx = x + (1 << k);
if(nx <= n && sum > bit[nx]) {
x = nx;
sum -= bit[x];
}
}
return x + 1;
}
};
void solve() {
int n; cin >> n;
for(int u, v, i = 1; i < n; i++)
cin >> u >> v;
int m; cin >> m;
vector<array<int, 2>> a(m);
BIT bit(n);
for(auto &[u, v] : a) {
cin >> v >> u;
bit.upd(v, 1);
}
sort(a.begin(), a.end(), greater<>());
bool okay = true;
for(int i = 0; i < m && okay; i++) {
auto &[v, u] = a[i];
if(bit.qry(u, v) != 1)
okay = false;
else {
bit.upd(u, -1);
bit.upd(v, 1);
}
}
cout << (okay ? "Yes" : "No") << '\n';
}
signed main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int tc; cin >> tc;
while(tc--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |