Submission #1296309

#TimeUsernameProblemLanguageResultExecution timeMemory
1296309Neco_arcJail (JOI22_jail)C++17
61 / 100
487 ms284024 KiB
#include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// #include </home/vamh/Documents/cp/idkman/debug.h>
// #endif //ONLINE_JUDGE

#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Neco"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) (120000 + 3)

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int LOG = 17;

void Input();
void reset();

int n, m;
vector<int> edges[maxn], g[40 * maxn];
int cha[maxn][18], idUP[maxn][18], idDOWN[maxn][18];
int S[maxn], T[maxn], h[maxn];
int ID_S[maxn], ID_T[maxn];

void pre_dfs(int u, int par) {
    for(int v : edges[u]) if(v != par) {
        cha[v][0] = u, h[v] = h[u] + 1;
        fi(i, 1, LOG) cha[v][i] = cha[cha[v][i - 1]][i - 1];

        pre_dfs(v, u);
    }
}

int lca(int u, int v) {
    if(h[u] < h[v]) swap(u, v);
    fid(i, LOG, 0) if(h[u] - (1 << i) >= h[v]) u = cha[u][i];
    if(u == v) return u;

    fid(i, LOG, 0) if(cha[u][i] != cha[v][i]) {
        u = cha[u][i], v = cha[v][i];
    }
    return cha[u][0];
}

int ID_mapping() {
    int IDN = m;
    
    fi(i, 1, n) ID_S[i] = ++IDN, ID_T[i] = ++IDN;
    fi(i, 1, m) ID_S[S[i]] = ID_T[T[i]] = i;

    fi(i, 1, n) fi(j, 1, LOG) if(cha[i][j]) {
        idUP[i][j] = ++IDN;
        idDOWN[i][j] = ++IDN;
    }
    
    fi(i, 1, n) if(cha[i][0]) {
        idUP[i][0] = ID_S[cha[i][0]];
        idDOWN[i][0] = ID_T[cha[i][0]];

    }


    auto connect = [](int u, int v) {
        if(u == 0 || v == 0) return;
        g[u].push_back(v);
    };

    fi(j, 1, LOG) {
        fi(i, 1, n) if(cha[i][j]) {
            connect(idUP[i][j - 1], idUP[i][j]);
            connect(idUP[cha[i][j - 1]][j - 1], idUP[i][j]);

            connect(idDOWN[i][j], idDOWN[i][j - 1]);
            connect(idDOWN[i][j], idDOWN[cha[i][j - 1]][j - 1]);
        }
    }


    return IDN;
}

void AddS(int u, int p, int id) {  
    fid(i, LOG, 0) if(h[u] - (1 << i) > h[p]) {
        g[idUP[u][i]].push_back(id);
        // cout << u << " " << idUP[u][i] << " " << id << '\n';
        u = cha[u][i];

    }
}

void AddT(int u, int p, int id) {
    fid(i, LOG, 0) if(h[u] - (1 << i) > h[p]) {
        g[id].push_back(idDOWN[u][i]);
        u = cha[u][i];
    }
}

bool topoSort(int N) {
    vector<int> deg(N + 1, 0);
    fi(i, 1, N) for(int v : g[i]) deg[v] ++;

    queue<int> q;
    fi(i, 1, N) if(!deg[i]) q.push(i);

    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(int v : g[u]) if(--deg[v] == 0) {
            q.push(v);
        }
    }

    fi(i, 1, N) g[i].clear();
    return *max_element(all(deg)) == 0; 
}

void solve()
{
    Input();
    if(m > 500) return;

    pre_dfs(1, 0);
    int N = ID_mapping();

    fi(i, 1, m) {
        int u = S[i], v = T[i], p = lca(S[i], T[i]);

        AddS(u, p, i);
        AddS(v, p, i);

        AddT(u, p, i);
        AddT(v, p, i);

        g[ID_S[v]].push_back(i);
        g[i].push_back(ID_T[u]);

        if(u != p) g[ID_S[p]].push_back(i);
        if(v != p) g[i].push_back(ID_T[p]);

    }

    if(topoSort(N)) cout << "Yes\n";
    else cout << "No\n";

    reset();
}

void Input() {
    cin >> n;
    fi(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    cin >> m;
    fi(i, 1, m) cin >> S[i] >> T[i];
}

void reset() {
    fi(i, 1, n) edges[i].clear();
    fi(i, 1, n) fi(j, 0, LOG) cha[i][j] = idUP[i][j] = idDOWN[i][j] = 0;
}

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }

    int nTest = 1;
    cin >> nTest;

    while(nTest--) {
        solve();
    }


    return 0;
}

Compilation message (stderr)

jail.cpp: In function 'int main()':
jail.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  185 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...