Submission #994300

# Submission time Handle Problem Language Result Execution time Memory
994300 2024-06-07T10:29:50 Z vjudge1 Jail (JOI22_jail) C++17
0 / 100
1 ms 600 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 120'001, mbit = 18;
int n, m;
int dth[maxn];
int S[maxn], T[maxn];
vector <vector <int> > G;
vector <int> Si, Ti;
vector <int> w;
vector <vector <int> > g;
vector <int> stp;

void dfs_pa(int u = 1){
    for(auto v : G[u]){
        if(v != stp[u]){
            stp[v] = u;
            dth[v] = dth[u] + 1;
            dfs_pa(v);
        }
    }
}


bool dfs_loop(int u){
    if(w[u] == 1) return 1;
    if(w[u] == 2) return 0;
    w[u] = 1;
    for(auto v : g[u]){
        if(dfs_loop(v)) return 1;
    }
    w[u] = 2;
    return 0;
}

bool sol(){
    cin >> n;
    G.assign(n + 1, vector<int>());
    stp.assign(n + 1, 0);
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dth[1] = 0;
    dfs_pa();
    //===============
    cin >> m;
    g.assign(m + 1, vector<int>());
    Si.assign(n + 1, -1);
    Ti.assign(n + 1, -1);
    for(int i = 0; i < m; i++){
        cin >> S[i] >> T[i];
        Si[S[i]] = i;
        Ti[T[i]] = i;
    }
    for(int i = 0; i < m; i++){
        int u = S[i], v = T[i];
        while(u != v){
            if(dth[u] < dth[v]) swap(u, v);
            u = stp[u];
            int s = Si[u], t = Ti[u];
            if(s != -1 && s != i) g[i].push_back(s);
            if(t != -1 && t != i) g[t].push_back(i);
        }
    }
    w.assign(m + 1, 0);
    for(int i = 0; i < m; i++){
        if(dfs_loop(i)) return 0;
    }
    return 1;
}

int main(){
    int Q;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> Q;
    while(Q--){
        cout << (sol() ? "Yes\n" : "No\n");
    }
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Incorrect 0 ms 348 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 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -