제출 #848682

#제출 시각아이디문제언어결과실행 시간메모리
848682danikoynovJail (JOI22_jail)C++14
61 / 100
5033 ms61944 KiB
/** ____ ____ ____ ____ ____ ____ ||l |||e |||i |||n |||a |||d || ||__|||__|||__|||__|||__|||__|| |/__\|/__\|/__\|/__\|/__\|/__\| **/ #include<bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int maxn = 2e5 + 10; int n, m, s[maxn], t[maxn]; vector < int > adj[maxn]; void input() { cin >> n; for (int i = 1; i < n; i ++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } cin >> m; for (int i = 1; i <= m; i ++) { cin >> s[i] >> t[i]; } } int tin[maxn], tout[maxn], occ[2 * maxn], depth[maxn], timer; void dfs(int v = 1, int p = -1) { tin[v] = ++ timer; occ[timer] = v; for (int u : adj[v]) { if (u == p) continue; depth[u] = depth[v] + 1; dfs(u, v); occ[++ timer] = v; } tout[v] = timer; } const int maxlog = 20; int dp[maxlog][maxn * 2], lg[2 * maxn]; void build_sparse_table() { for (int i = 1; i <= timer; i ++) { dp[0][i] = occ[i]; lg[i] = lg[i / 2] + 1; } for (int j = 1; j < lg[timer]; j ++) { for (int i = 1; i <= timer - (1 << j) + 1; i ++) { dp[j][i] = dp[j - 1][i + (1 << (j - 1))]; if (depth[dp[j - 1][i]] < depth[dp[j][i]]) dp[j][i] = dp[j - 1][i]; } } } int get_lca(int v, int u) { int l = tin[v], r = tin[u]; if (l > r) swap(l, r); int len = lg[r - l + 1] - 1; int lca = dp[len][r - (1 << len) + 1]; if (depth[dp[len][l]] < depth[lca]) lca = dp[len][l]; return lca; } vector < int > graph[maxn]; bool is_cycle; bool in_subtree(int v, int u) { return (tin[v] <= tin[u] && tout[v] >= tout[u]); } bool on_path(int v, int u, int w) { int lca = get_lca(v, u); if (in_subtree(lca, w) && in_subtree(w, v)) return true; if (in_subtree(lca, w) && in_subtree(w, u)) return true; return false; } void check_prisoners(int i, int j) { if (on_path(s[i], t[i], s[j]) && on_path(s[i], t[i], t[j])) { is_cycle = true; return; } if (on_path(s[i], t[i], s[j])) { graph[i].push_back(j); return; } if (on_path(s[i], t[i], t[j])) { graph[j].push_back(i); return; } } void build_graph() { for (int i = 1; i <= m; i ++) { for (int j = 1; j <= m; j ++) { if (i != j) check_prisoners(i, j); } } } int used[maxn]; void check_dag(int v) { used[v] = 1; for (int u : graph[v]) { if (used[u] == 2) continue; ///cout << v << " : " << u << endl; if (used[u] == 1) is_cycle = 1; else { check_dag(u); } } used[v] = 2; } void check_graph() { for (int i = 1; i <= m; i ++) { if (!used[i]) check_dag(i); } if (is_cycle) cout << "No" << endl; else cout << "Yes" << endl; } void clear_data() { is_cycle = false; for (int i = 1; i <= m; i ++) graph[i].clear(), used[i] = 0; for (int i = 1; i <= n; i ++) { adj[i].clear(); } timer = 0; } void solve() { input(); dfs(); build_sparse_table(); build_graph(); check_graph(); clear_data(); } int main() { speed(); int q; cin >> q; while(q --) solve(); 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...