Submission #1259162

#TimeUsernameProblemLanguageResultExecution timeMemory
1259162ewirlanJail (JOI22_jail)C++20
72 / 100
4590 ms1114112 KiB
//
#ifndef __SIZEOF_INT128__
  #define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
    string name{""};
    time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
    duration<float, std::milli> dur;
    Timer() = default;
    Timer(string nm): name(nm) {}
    ~Timer() {
        end = high_resolution_clock::now(); dur= end - start;
        cout << "@" << name << "> " << dur.count() << " ms" << '\n';
    }
};
template <typename T = int> inline T in()
{
    static T x;
    std::cin >> x;
    return x;
}
std::string yn(bool b)
{
    if(b) return "YES\n";
    else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
    for(const auto& i : wek)out << i << ' ';
    return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
    out << '{'<<par.first<<", "<<par.second<<"}";
    return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
std::int32_t main()
{
    std::cout.tie(nullptr); //for luck
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
    test{
        int n(in());
        vector <vector<int>> g(n);
        rep(i, 1, n){
            int a(in()-1), b(in()-1);
            g[a].pb(b);
            g[b].pb(a);
        }
        int m(in());
        vector <int> s(m), t(m);
        vector <vector <int>> vs(n), vt(n);
        rep(i, 0, m){
            cin >> s[i] >> t[i];
            vs[--s[i]].pb(i);
            vt[--t[i]].pb(i);
        }
        vector <int> p(n), o(n);
        stack <pair<int, int>> dfs;
        dfs.push({0,0});
        while(sz(dfs)){
            auto [w, q] = dfs.top(); dfs.pop();
            p[w] = q;
            for(auto i: g[w])if(i != o[w]){
                o[i] = w;
                dfs.push({i, q+1});
            }
        }
        int sd(m);
        vector <vector <int>> d(sd);
        rep(i, 0, m){
            int a(s[i]), b(t[i]);
            if(p[a] < p[b])swap(a, b);
            while(p[a] > p[b]){
                for(auto j: vs[a])if(i != j)d[j].pb(i);
                for(auto j: vt[a])if(i != j)d[i].pb(j);
                a = o[a];
            }
            while(a != b){
                for(auto j: vs[a])if(i != j)d[j].pb(i);
                for(auto j: vt[a])if(i != j)d[i].pb(j);
                for(auto j: vs[b])if(i != j)d[j].pb(i);
                for(auto j: vt[b])if(i != j)d[i].pb(j);
                a = o[a];
                b = o[b];
            }
            for(auto j: vs[a])if(i != j)d[j].pb(i);
            for(auto j: vt[a])if(i != j)d[i].pb(j);
        }
        vector <int> dg(sd);
        rep(i, 0, sd)for(auto j: d[i])++dg[j];
        // rep(i, 0, sd)for(auto j: d[i])cout << i  << " -> " << j << '\n';
        queue <int> kul;
        rep(i, 0, sd)if(!dg[i])kul.push(i);
        int cnt(0);
        while(!kul.empty()){
            auto w(kul.front()); kul.pop();
            ++cnt;
            dg[w] = -1;
            for(auto i: d[w])if(!--dg[i])kul.push(i);
        }
        cout << (cnt == sd ? "Yes\n" : "No\n");
    }    
    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...