제출 #879538

#제출 시각아이디문제언어결과실행 시간메모리
879538Shayan86Jail (JOI22_jail)C++17
100 / 100
2089 ms454880 KiB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

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

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll L = 20;
const ll K = 5e6;
const ll N = 12e4 + 50;
const ll Mod = 1e9 + 7;

ll n, m, k, s[N], t[N], qu[N][2], par[N][L], id[N][L][2], h[N], idx[K];

bool mark[K];
vector<int> adj[N], neigh[K], topol;

void dfs(int v, int p = 0){
    par[v][0] = p;
    id[v][0][0] = ++k;
    if(qu[v][0]) neigh[qu[v][0]].pb(k);
    id[v][0][1] = ++k;
    if(qu[v][1]) neigh[k].pb(qu[v][1]);

    for(int i = 1; i < L; i++){
        par[v][i] = par[par[v][i-1]][i-1];

        id[v][i][0] = ++k;
        neigh[id[v][i-1][0]].pb(k);
        if(par[v][i-1]) neigh[id[par[v][i-1]][i-1][0]].pb(k);
        id[v][i][1] = ++k;
        neigh[k].pb(id[v][i-1][1]);
        if(par[v][i-1]) neigh[k].pb(id[par[v][i-1]][i-1][1]);
    }

    for(int u: adj[v]){
        if(u == p) continue;
        h[u] = h[v] + 1; dfs(u, v);
    }
}

int getPar(int v, int x){
    for(int i = 0; i < L; i++){
        if(x >> i & 1) v = par[v][i];
    }
    return v;
}

int lca(int v, int u){
    if(h[v] < h[u]) swap(u, v);
    v = getPar(v, h[v] - h[u]);
    if(v == u) return v;

    for(int i = L-1; i >= 0; i--){
        if(par[v][i] != par[u][i]){
            v = par[v][i]; u = par[u][i];
        }
    }
    return par[v][0];
}

void add0(int x, int v, int p){
    int y = h[v] - h[p];
    if(y <= 0) return;

    for(int i = 0; i < L; i++){
        if(y >> i & 1){
            neigh[id[v][i][0]].pb(x);
            v = par[v][i];
        }
    }
}

void add1(int x, int v, int p){
    int y = h[v] - h[p];
    if(y <= 0) return;

    for(int i = 0; i < L; i++){
        if(y >> i & 1){
            neigh[x].pb(id[v][i][1]);
            v = par[v][i];
        }
    }
}

void tdfs(int v){
    mark[v] = true;
    for(int u: neigh[v]){
        if(!mark[u]) tdfs(u);
    }
    topol.pb(v);
}

int main(){
    fast_io;

    int T;
    cin >> T;

    while(T--){
        cin >> n;

        int u, v;
        for(int i = 1; i < n; i++){
            cin >> u >> v;
            adj[u].pb(v);
            adj[v].pb(u);
        }

        cin >> m;

        for(int i = 1; i <= m; i++){
            cin >> s[i] >> t[i]; 
            qu[s[i]][0] = i; qu[t[i]][1] = i;
        }

        k = m;
        h[1] = 1; dfs(1);

        for(int i = 1; i <= m; i++){
            int slt = lca(s[i], t[i]);

            if(s[i] == slt){
                add0(i, t[i], slt);
                add1(i, par[t[i]][0], par[slt][0]);
                continue;
            }
            if(t[i] == slt){
                add0(i, par[s[i]][0], par[slt][0]);
                add1(i, s[i], slt);
                continue;
            }

            add0(i, par[s[i]][0], slt);
            add0(i, t[i], par[slt][0]);
            add1(i, s[i], par[slt][0]);
            add1(i, par[t[i]][0], slt);
        }

        for(int i = 1; i <= k; i++){
            if(!mark[i]) tdfs(i);
        }
        for(int i = 0; i < k; i++) idx[topol[i]] = i+1;

        bool ch = true;
        for(int i = 1; i <= k; i++){
            for(int j: neigh[i]) if(idx[j] > idx[i]) ch = false;
        }

        cout << (ch ? "Yes" : "No") << endl;

        topol.clear();
        for(int i = 1; i <= n; i++) adj[i].clear();
        for(int i = 1; i <= n; i++) qu[i][0] = qu[i][1] = 0;

        for(int i = 1; i <= k; i++){
            neigh[i].clear(); idx[i] = 0; mark[i] = 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...