Submission #990531

#TimeUsernameProblemLanguageResultExecution timeMemory
990531OAleksaJail (JOI22_jail)C++14
0 / 100
8 ms11100 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 120069; const int K = 20; int n, m, a[N], b[N]; int tin[N], tout[N], timer, up[N][K], dep[N], is, vis[N]; vector<int> g[N]; vector<int> gg[N]; bool anc(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } int lca(int a, int b) { if (anc(a, b)) return a; else if (anc(b, a)) return b; for (int i = K - 1;i >= 0;i--) { if (!anc(up[a][i], b) && up[a][i] > 0) a = up[a][i]; } return up[a][0]; } void dfs(int v, int p) { up[v][0] = p; tin[v] = ++timer; for (int i = 1;i < K;i++) up[v][i] = up[up[v][i - 1]][i - 1]; for (auto u : g[v]) { if (u == p) continue; dep[u] = dep[v] + 1; dfs(u, v); } tout[v] = ++timer; } void dfs(int v) { if (vis[v]) { is += (vis[v] == 1); return; } vis[v] = 1; for (auto u : gg[v]) dfs(u); vis[v] = 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { cin >> n; timer = is = 0; for (int i = 1;i <= n;i++) { g[i].clear(); gg[i].clear(); vis[i] = 0; for (int j = 0;j < K;j++) up[i][j] = 0; } for (int i = 1;i <= n - 1;i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } cin >> m; for (int i = 1;i <= m;i++) cin >> a[i] >> b[i]; dfs(1, 0); for (int i = 1;i <= m;i++) { for (int j = 1;j <= m;j++) { if (i == j) continue; // da li je i u j //ako b[i] pripada onda j mora zavrsim pre i int dis = dep[a[j]] + dep[b[j]] - 2 * dep[lca(a[j], b[j])]; int dd1 = dep[a[j]] + dep[b[i]] - 2 * dep[lca(a[j], b[i])]; int dd2 = dep[b[j]] + dep[b[i]] - 2 * dep[lca(b[j], b[i])]; if (dis == dd1 + dd2) gg[j].push_back(i); } } for (int i = 1;i <= n;i++) { if (!vis[i]) { dfs(i); } } if (is) cout << "No\n"; else cout << "Yes\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...