Submission #896820

#TimeUsernameProblemLanguageResultExecution timeMemory
896820GrindMachineJail (JOI22_jail)C++17
61 / 100
5021 ms33332 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* read some solutions long time ago and slightly remember some ideas from there */ const int MOD = 1e9 + 7; const int N = 1.2e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; vector<ll> adj1[N]; struct lca_algo { // LCA template (for graphs with 1-based indexing) int LOG = 1; vector<int> depth; vector<vector<int>> up; vector<int> tin, tout; int timer = 1; lca_algo() { } lca_algo(int n) { lca_init(n); } void lca_init(int n) { while ((1 << LOG) < n) LOG++; up = vector<vector<int>>(n + 1, vector<int>(LOG, 1)); depth = vector<int>(n + 1); tin = vector<int>(n + 1); tout = vector<int>(n + 1); lca_dfs(1, -1); } void lca_dfs(int node, int par) { tin[node] = timer++; trav(child, adj1[node]) { if (child == par) conts; up[child][0] = node; rep1(j, LOG - 1) { up[child][j] = up[up[child][j - 1]][j - 1]; } depth[child] = depth[node] + 1; lca_dfs(child, node); } tout[node] = timer-1; } int lift(int u, int k) { rep(j, LOG) { if (k & (1 << j)) { u = up[u][j]; } } return u; } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; u = lift(u, k); if (u == v) return u; rev(j, LOG - 1, 0) { if (up[u][j] != up[v][j]) { u = up[u][j]; v = up[v][j]; } } u = up[u][0]; return u; } int get_dis(int u, int v) { int lca = query(u, v); return depth[u] + depth[v] - 2 * depth[lca]; } bool is_ances(int u, int v){ return tin[u] <= tin[v] and tout[u] >= tout[v]; } }; vector<ll> adj2[N]; vector<bool> vis(N), recstack(N); bool cyc; void dfs(ll u){ vis[u] = recstack[u] = 1; trav(v,adj2[u]){ if(vis[v]){ if(recstack[v]){ cyc = true; } } else{ dfs(v); } } recstack[u] = 0; } void solve(int test_case) { ll n; cin >> n; rep1(i,n){ adj1[i].clear(); adj2[i].clear(); vis[i] = recstack[i] = 0; } cyc = false; rep1(i,n-1){ ll u,v; cin >> u >> v; adj1[u].pb(v), adj1[v].pb(u); } ll m; cin >> m; vector<pll> a(m+5); rep1(i,m) cin >> a[i].ff >> a[i].ss; lca_algo LCA(n); auto on_path = [&](ll u, ll v, ll w){ // does w lie on the path from u to v? ll lca = LCA.query(u,v); if(LCA.is_ances(lca,w) and LCA.is_ances(w,u)){ return true; } if(LCA.is_ances(lca,w) and LCA.is_ances(w,v)){ return true; } return false; }; rep1(i,m){ rep1(j,m){ if(i == j) conts; auto [u1,v1] = a[i]; auto [u2,v2] = a[j]; bool b1 = on_path(u1,v1,u2); bool b2 = on_path(u1,v1,v2); if(b1 and b2){ no; return; } if(b1){ // j needs to be moved before i adj2[j].pb(i); } else if(b2){ // i needs to be moved before j adj2[i].pb(j); } } } rep1(i,n){ if(vis[i]) conts; dfs(i); } if(!cyc){ yes; } else{ no; } } int main() { fastio; int t = 1; cin >> t; rep1(i, t) { solve(i); } 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...