Submission #950827

# Submission time Handle Problem Language Result Execution time Memory
950827 2024-03-20T18:43:21 Z vladilius Jail (JOI22_jail) C++17
0 / 100
5000 ms 35104 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct FT{
    vector<int> bit;
    int n;
    FT(int ns){
        n = ns;
        bit.resize(n + 1);
    }
    int get(int v){
        int out = 0;
        while (v > 0){
            out += bit[v];
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
    void upd(int v, int k){
        while (v <= n){
            bit[v] += k;
            v |= (v + 1);
        }
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int q; cin>>q;
    while (q--){
        int n; cin>>n;
        vector<int> g[n + 1];
        for (int i = 1; i < n; i++){
            int a, b; cin>>a>>b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        int m; cin>>m;
        vector<int> s(m + 1), t(m + 1);
        for (int i = 1; i <= m; i++){
            cin>>s[i]>>t[i];
        }
        
        vector<int> tin(n + 1), tout(n + 1), pw[n + 1];
        int lg = ceil(log2(n)) + 2;
        for (int i = 1; i <= n; i++){
            pw[i].resize(lg);
        }
        int timer = 0;
        function<void(int, int)> fill = [&](int v, int pr){
            tin[v] = ++timer;
            pw[v][0] = pr;
            for (int i = 1; i < lg; i++){
                pw[v][i] = pw[pw[v][i - 1]][i - 1];
            }
            for (int i: g[v]){
                if (i == pr) continue;
                fill(i, v);
            }
            tout[v] = timer;
        };
        fill(1, 1);
        
        auto check = [&](int x, int y){
            return (tin[x] <= tin[y] && tout[x] >= tout[y]);
        };
        auto lca = [&](int x, int y){
            if (check(x, y)) return x;
            if (check(y, x)) return y;
            for (int i = lg - 1; i >= 0; i--){
                if (!check(pw[x][i], y)){
                    x = pw[x][i];
                }
            }
            return pw[x][0];
        };
        
        FT T(n);
        auto upd = [&](int v, int k){
            T.upd(tin[v], k);
            T.upd(tout[v] + 1, -k);
        };
        vector<int> l(n + 1), used(n + 1);
        for (int i = 1; i <= m; i++){
            upd(s[i], 1);
            l[i] = lca(s[i], t[i]);
            used[s[i]] = 1;
        }
        
        auto get = [&](int i){
            return T.get(s[i]) + T.get(t[i]) - 2 * T.get(l[i]) + used[l[i]];
        };
        
        vector<bool> vis(m + 1);
        bool out = true;
        for (int i = 1; i <= m; i++){
            bool ind = false;
            for (int j = 1; j <= m; j++){
                if (vis[j]) continue;
                int f = get(j);
                if (f <= 1){
                    vis[j] = ind = true;
                    upd(s[j], -1); used[s[j]]--;
                    upd(t[j], 1); used[t[j]]++;
                    break;
                }
            }
            if (!ind){
                out = false;
                break;
            }
        }
        cout<<(out ? "Yes" : "No")<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 512 KB Output is correct
4 Correct 11 ms 484 KB Output is correct
5 Correct 25 ms 480 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 39 ms 1884 KB Output is correct
10 Correct 60 ms 34640 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 30 ms 500 KB Output is correct
13 Execution timed out 5091 ms 35104 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 5 ms 348 KB Output is correct
6 Incorrect 1 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 512 KB Output is correct
4 Correct 11 ms 484 KB Output is correct
5 Correct 25 ms 480 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 39 ms 1884 KB Output is correct
10 Correct 60 ms 34640 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 30 ms 500 KB Output is correct
13 Execution timed out 5091 ms 35104 KB Time limit exceeded
14 Halted 0 ms 0 KB -