# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
851388 |
2023-09-19T17:23:23 Z |
EJIC_B_KEDAX |
Jail (JOI22_jail) |
C++14 |
|
10 ms |
604 KB |
#include <bits/stdc++.h>
#include <random>
#ifndef LOCAL
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef double dd;
typedef long double ld;
typedef unsigned int uii;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void solve();
mt19937_64 mt(1);
int32_t main() {
#ifdef LOCAL
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
//freopen("amusing.in", "r", stdin);
//freopen("amusing.out", "w", stdout);
#endif
cout << fixed << setprecision(30);
int tests = 1;
cin >> tests;
while (tests--) {
solve();
}
}
int tim = 0;
void dfs(int s, int p, vector<vector<int>>& g, vector<int>& tin, vector<int>& tout, vector<int>& pr) {
tin[s] = tim++;
pr[s] = p;
for (int i : g[s]) {
if (i != p) {
dfs(i, s, g, tin, tout, pr);
}
}
tout[s] = tim++;
}
bool a_p_b(int a, int b, vector<int>& tin, vector<int>& tout) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
struct path {
int u, v, lca;
};
void solve() {
tim = 0;
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v; v--; u--;
g[v].push_back(u);
g[u].push_back(v);
}
vector<int> s(n, -1), t(n, -1), tin(n), tout(n), pr(n), cnt(n, 0);
dfs(0, -1, g, tin, tout, pr);
int q, sc = 0;
cin >> q;
bool ans = true;
vector<int> u;
vector<path> z;
while (q--) {
int a, b;
cin >> a >> b; a--; b--;
if (!ans) {
continue;
}
z.push_back({a, b, 0});
int a1 = a, b1 = b;
while (!a_p_b(a1, b, tin, tout)) {
if (s[a1] != -1) {
cnt[s[a1]] += 2;
u.push_back(s[a1]);
}
if (t[a1] != -1) {
cnt[t[a1]]++;
u.push_back(t[a1]);
}
a1 = pr[a1];
}
while (!a_p_b(b1, a, tin, tout)) {
if (s[b1] != -1) {
cnt[s[b1]] += 2;
u.push_back(s[b1]);
}
if (t[b1] != -1) {
cnt[t[b1]]++;
u.push_back(t[b1]);
}
b1 = pr[b1];
}
if (s[b1] != -1) {
cnt[s[b1]] += 2;
u.push_back(s[b1]);
}
if (t[b1] != -1) {
cnt[t[b1]]++;
u.push_back(t[b1]);
}
z[z.size() - 1].lca = a1;
s[a] = sc;
t[b] = sc++;
for (int i : u) {
if (!cnt[i]) {
continue;
}
if (cnt[i] == 1 && a_p_b(a1, z[i].v, tin, tout) && (a_p_b(z[i].v, a, tin, tout) || a_p_b(z[i].v, b, tin, tout)) && a_p_b(z[i].lca, b, tin, tout) && (a_p_b(b, z[i].u, tin, tout) || a_p_b(b, z[i].v, tin, tout))) {
ans = false;
}
if (cnt[i] > 2) {
ans = false;
}
cnt[i] = 0;
}
u.clear();
}
if (ans) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
10 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
10 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |