Submission #550653

# Submission time Handle Problem Language Result Execution time Memory
550653 2022-04-18T18:57:02 Z doowey Jail (JOI22_jail) C++14
0 / 100
4 ms 6132 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = 123456;
vector<int> T[N];

int par[N];
int S[N], E[N];
int ss[N], ee[N];
int tin[N];
int tout[N];
int ti;

void dfs(int u, int pp){
    ti ++ ;
    tin[u] = ti;
    par[u] = pp;
    for(auto x : T[u]){
        if(x == pp) continue;
        dfs(x, u);
    }
    tout[u] = ti;
}

bool is_par(int u, int v){
    if(tin[u] <= tin[v] && tout[u] >= tout[v]) return true;
    return false;
}

vector<int> G[N];

bool cycle;
int vis[N];

void dfs1(int u){
    if(vis[u] == 1){
        cycle = true;
        return;
    }
    if(vis[u] == 2){
        return;
    }
    vis[u] = 1;
    for(auto x : G[u]){
        dfs1(x);
    }
    vis[u] = 2;
}

void solve(){
    int n, m;
    cin >> n;
    for(int i = 1; i <= n; i ++ ){
        T[i].clear();
        G[i].clear();
        ss[i] = ee[i] = -1;
    }
    int u, v;
    for(int i = 1; i < n; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        T[v].push_back(u);
    }
    ti = 0;
    dfs(1, -1);
    cin >> m;
    for(int i = 1; i <= m ; i ++ ){
        cin >> S[i] >> E[i];
        ss[S[i]] = i;
        ee[E[i]] = i;
    }
    int uu;
    for(int iq = 1; iq <= m ; iq ++ ){
        uu = S[iq];
        while(!is_par(uu, E[iq])){
            if(ss[uu] != -1 && ss[uu] != iq){
                G[ss[uu]].push_back(iq);
            }
            if(ee[uu] != -1 && ee[uu] != iq){
                G[iq].push_back(ee[uu]);
            }
            uu = par[uu];
        }
        uu = E[iq];
        while(!is_par(uu, S[iq])){
            if(ss[uu] != -1 && ss[uu] != iq){
                G[ss[uu]].push_back(iq);
            }
            if(ee[uu] != -1 && ee[uu] != iq){
                G[iq].push_back(ee[uu]);
            }
            uu = par[uu];
        }
        vis[iq] = 0;
    }
    cycle = false;
    for(int i = 1; i <= m ; i ++ ){
        if(vis[i] == 0){
            dfs1(i);
        }
    }
    if(!cycle){
        cout << "Yes\n";
    }
    else{
        cout << "No\n";
    }
}

int main(){
    fastIO;
    //freopen("in.txt","r",stdin);
    int tc;
    cin >> tc;
    for(int iq = 1; iq <= tc; iq ++ ){
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Incorrect 4 ms 6100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6132 KB Output is correct
2 Incorrect 4 ms 6132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6132 KB Output is correct
2 Incorrect 4 ms 6132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6132 KB Output is correct
2 Incorrect 4 ms 6132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6132 KB Output is correct
2 Incorrect 4 ms 6132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 3 ms 6132 KB Output is correct
3 Correct 3 ms 6100 KB Output is correct
4 Incorrect 4 ms 6100 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 3 ms 6100 KB Output is correct
3 Incorrect 4 ms 6100 KB Output isn't correct
4 Halted 0 ms 0 KB -