Submission #1244170

#TimeUsernameProblemLanguageResultExecution timeMemory
1244170d1mkJail (JOI22_jail)C++20
0 / 100
50 ms724 KiB
/// 407!3 +/ \!<07!\*
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

using namespace std;

using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!

using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvvi = vector<vvi>;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

// pairs
#define mp make_pair
#define f first
#define s second

// vectors
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define er erase
#define ub upper_bound
#define lb lower_bound

// loops
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const int LG = 20;
const ll INF = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!
const char nl = '\n';

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

template<typename T>
using indexed_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order

void _input() {return;}
template <typename T, typename... V>
void _input(T &t, V&... v) {cin >> (t); if (sizeof...(v))  _input(v...);}
void __input(vi &v, const int n){F0R(i, n)cin >> v[i];}
void __input(vvi &v, const int n, const int m){F0R(i, n)F0R(j, m)cin>>v[i][j];}

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}


int lca(int u, int v, vvi &st, vi &d) {
    if (d[u] < d[v])swap(u, v);
    R0F(k, LG) if (d[st[u][k]] >= d[v])u = st[u][k];
    //_print(u, v);
    if (u == v)return u;
    R0F(k, LG) {
        if (st[u][k] != st[v][k]) {
            u = st[u][k];
            v = st[v][k];
        }
    }
    if (u == v)return u;
    return st[u][0];
}

void dfs(int u, vi &d, vvi &st, vvi &g) {
    trav(v, g[u]) {
        if (st[u][0] == v)continue;
        st[v][0] = u;
        d[v] = d[u] + 1;
        dfs(v, d, st, g);
    }
}

bool dfs2(int u, vi& vis, vvi &gc) {
    vis[u] = 1;
    trav(v, gc[u]) {
        if (v == u)continue;
        if (vis[v] == 1)return false;
        if (vis[v] == 2)continue;
        if (!dfs2(v, vis, gc))return false;
    }
    vis[u] = 2;
    return true;
}

void solve(){
    int n, m;
    cin >> n;
    vvi g(n + 1);
    vi check(n + 1), d(n + 1);
    vvi st(n + 1, vi(LG));
    vvvi e(n + 1, vvi(LG));
    F0R(i, n - 1) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    cin >> m;
    vpi par(m);
    F0R(i, m) {
        cin >> par[i].f >> par[i].s;
        check[par[i].f] = 1;
    }
    d[1] = 1;
    dfs(1, d, st, g);
    FOR(i, 1, n + 1) if (check[st[i][0]])e[i][0].pb(st[i][0]);
    FOR(j, 1, LG) {
        FOR(i, 1, n + 1) {
            trav(el, e[i][j - 1])e[i][j].pb(el);
            trav(el, e[st[i][j - 1]][j - 1])e[i][j].pb(el);
            st[i][j] = st[st[i][j - 1]][j - 1];
        }
    }
    //_print(d, st);
    vvi cg(m + 1);
    vi chg(n + 1, -1);
    F0R(i, m) chg[par[i].f] = i;
    F0R(i, m) {
        int s = par[i].f, t = par[i].s;
        int lc = lca(s, t, st, d);
        //_print(lc);
        R0F(k, LG) {
            if (d[st[s][k]] >= d[lc]) {
                trav(el, e[s][k])cg[i].pb(chg[el]);
                s = st[s][k];
            }
        }
        //_print(cg[i]);
        if (check[t])cg[i].pb(chg[t]);
        R0F(k, LG) {
            if (d[st[t][k]] >= d[lc]) {
                trav(el, e[t][k])cg[i].pb(chg[el]);
                t = st[t][k];
            }
        }
        //_print(cg[i]);
    }
    //_print(cg);
    vi vis(m + 1);
    cout << (dfs2(1, vis, cg) ? "Yes\n" : "No\n");

}


/*
3
3
1 2
2 3
2
2 1
3 2
7
1 2
2 3
3 4
4 5
5 6
6 7
3
1 3
4 2
2 5
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8
*/

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t; cin >> t;while(t--){
        solve();
    }
    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...