Submission #994190

#TimeUsernameProblemLanguageResultExecution timeMemory
994190vjudge1Jail (JOI22_jail)C++17
61 / 100
5097 ms20480 KiB
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g, g2; vector<int> vis; bool dfs(int v, int go) { vis[v] = 1; if(v == go) {//cout << v << endl; return 1; } for(auto x : g[v]) { if(vis[x] == 0 && dfs(x, go)) return 1; } vis[v] = 2; return 0; } bool c(int v) { vis[v] = 1; for(auto x : g2[v]) { //cout << v << " " << x << endl; if(vis[x] == 1) return 1; if(vis[x] == 0) if(c(x)) return 1; } vis[v] = 2; return 0; } void solve() { int n, m; cin >> n; g.assign(n + 1, {}); vis.assign(n + 1, 0); for(int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } cin >> m; g2.assign(m + 1, {}); vector<int> a(m), b(m); for(int i = 0; i < m; i++) cin >> a[i] >> b[i]; for(int i = 0; i < m; i++) { fill(vis.begin(), vis.end(), 0); dfs(a[i], b[i]); for(int j = 0; j < m; j++) { if(i == j) continue; if(vis[a[j]] == 1) { g2[i].push_back(j); } if(vis[b[j]] == 1) { //cout << i << " " << j << endl; g2[j].push_back(i); } } } vis.assign(m + 1, 0); for(int i = 0; i < m; i++) { if(vis[i] == 0 && c(i)) { cout << "No\n"; return; } } cout << "Yes\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int q; cin >> q; while(q--) solve(); }
#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...