Submission #669275

# Submission time Handle Problem Language Result Execution time Memory
669275 2022-12-06T07:22:30 Z fatemetmhr Jail (JOI22_jail) C++17
10 / 100
1828 ms 434880 KB
// ~ Be Name Khoda ~

#include<bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  2e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const int lg    =  18;
const ll  inf   =  1e18;

int mark[maxn5 * lg * 2], s[maxn5], t[maxn5], h[maxn5];
int par[lg][maxn5], newnode = 0, in[lg][maxn5], out[lg][maxn5];
vector <int> adj[maxn5], ed[maxn5 * lg * 2];
bool cy = false;


void dfs_lca(int v){
    if(!out[0][v])
        out[0][v] = newnode++;
    if(!in[0][v])
        in[0][v] = newnode++;
    for(int i = 1; i < lg && par[i - 1][v] != -1; i++){
        par[i][v] = par[i - 1][par[i - 1][v]];
        out[i][v] = newnode++;
        in[i][v] = newnode++;
        ed[out[i][v]].pb(out[i - 1][v]);
        ed[out[i][v]].pb(out[i - 1][par[i - 1][v]]);
        ed[in[i - 1][v]].pb(in[i][v]);
        ed[in[i - 1][par[i - 1][v]]].pb(in[i][v]);
    }
    for(auto u : adj[v]) if(u != par[0][v]){
        par[0][u] = v;
        h[u] = h[v] + 1;
        dfs_lca(u);
    }
}

void dfs(int v){
    mark[v] = 1;
    for(auto u : ed[v]){
        if(mark[u] == 0)
            dfs(u);
        else if(mark[u] == 1)
            cy = true;
    }
    mark[v] = 2;
}

inline int lca(int a, int b){
    if(h[a] < h[b])
        swap(a, b);
    int d = h[a] - h[b];
    for(int i = 0; i < lg; i++) if((d >> i)&1)
        a = par[i][a];
    if(a == b)
        return a;
    for(int i = lg - 1; i >= 0; i--) if(par[i][a] != par[i][b]){
        a = par[i][a];
        b = par[i][b];
    }
    return par[0][a];
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    int tt; cin >> tt;
    while(tt--){
        int n; cin >> n;
        for(int i = 0; i < n; i++){
            adj[i].clear(); 
            for(int j = 0; j < lg; j++){
                par[j][i] = -1;
                in[j][i] = out[j][i] = 0;
            }
        }
        for(int i = 0; i < n - 1; i++){
            int a, b; cin >> a >> b;
            a--; b--;
            adj[a].pb(b);
            adj[b].pb(a);
        }
        int m; cin >> m;
        newnode = m;
        for(int i = 0; i < m; i++){
            cin >> s[i] >> t[i];
            s[i]--; t[i]--;
            if(!in[0][s[i]])
                in[0][s[i]] = newnode++;
            if(!out[0][t[i]])
                out[0][t[i]] = newnode++;
            ed[i].pb(in[0][s[i]]);
            ed[out[0][t[i]]].pb(i);
        }
        h[0] = 0;
        dfs_lca(0);
        for(int i = 0; i < m; i++){
            int v = s[i], u = t[i];
            if(v == u)
                continue;
            ed[i].pb(out[0][v]);
            ed[in[0][u]].pb(i);
            if(h[v] < h[u])
                swap(u, v);
            int c = lca(u, v);
            bool re = true;
            if(c == u){
                re = false;
                v = par[0][v];
            }
            else{
                u = par[0][u];
                v = par[0][v];
            }
            int d = h[v] - h[u];
            for(int j = 0; j < lg; j++) if((d >> j)&1){
                ed[i].pb(out[j][v]);
                ed[in[j][v]].pb(i);
                v = par[j][v];
            }
            if(re){
                ed[i].pb(out[0][v]);
                ed[in[0][v]].pb(i);
            }
            if(u == v)
                continue;
            for(int j = lg - 1; j >= 0; j--) if(par[j][v] != par[j][u]){
                ed[i].pb(out[j][v]);
                ed[i].pb(out[j][u]);
                ed[in[j][v]].pb(i);
                ed[in[j][u]].pb(i);
                v = par[j][v];
                u = par[j][u];
            }
            ed[i].pb(out[0][v]);
            ed[i].pb(out[0][par[0][v]]);
            ed[in[0][v]].pb(i);
            ed[in[0][par[0][v]]].pb(i);
        }
        cy = false;
        for(int i = 0; i < newnode; i++) if(!mark[i])
            dfs(i);
        cout << (cy ? "No\n" : "Yes\n");
        for(int i = 0; i < newnode; i++){
            ed[i].clear();
            mark[i] = 0;
        }
    }
}

/*
Why so fast...? 
Hold on...
Hold your breath...
Need anything else?

Nein, Kein Problem...
Das Leben war schon immer so grausam :)
*/
# Verdict Execution time Memory Grader output
1 Correct 78 ms 174312 KB Output is correct
2 Correct 79 ms 174352 KB Output is correct
3 Correct 81 ms 174344 KB Output is correct
4 Correct 105 ms 174580 KB Output is correct
5 Correct 135 ms 174572 KB Output is correct
6 Correct 82 ms 174652 KB Output is correct
7 Correct 84 ms 174588 KB Output is correct
8 Correct 88 ms 174596 KB Output is correct
9 Correct 343 ms 181980 KB Output is correct
10 Correct 1119 ms 352772 KB Output is correct
11 Correct 95 ms 174572 KB Output is correct
12 Correct 149 ms 174820 KB Output is correct
13 Correct 1022 ms 358352 KB Output is correct
14 Correct 974 ms 358368 KB Output is correct
15 Correct 1428 ms 383788 KB Output is correct
16 Correct 1828 ms 434880 KB Output is correct
17 Correct 1226 ms 361696 KB Output is correct
18 Correct 1024 ms 361684 KB Output is correct
19 Correct 1193 ms 361828 KB Output is correct
20 Correct 1194 ms 361712 KB Output is correct
21 Correct 1211 ms 378044 KB Output is correct
22 Correct 879 ms 356952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 174396 KB Output is correct
2 Correct 79 ms 174392 KB Output is correct
3 Correct 82 ms 174628 KB Output is correct
4 Correct 81 ms 174576 KB Output is correct
5 Correct 82 ms 174536 KB Output is correct
6 Correct 92 ms 174488 KB Output is correct
7 Correct 81 ms 174608 KB Output is correct
8 Correct 82 ms 174676 KB Output is correct
9 Correct 88 ms 174588 KB Output is correct
10 Correct 88 ms 174580 KB Output is correct
11 Correct 80 ms 174584 KB Output is correct
12 Correct 79 ms 174408 KB Output is correct
13 Correct 78 ms 174540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 174396 KB Output is correct
2 Correct 79 ms 174392 KB Output is correct
3 Correct 82 ms 174628 KB Output is correct
4 Correct 81 ms 174576 KB Output is correct
5 Correct 82 ms 174536 KB Output is correct
6 Correct 92 ms 174488 KB Output is correct
7 Correct 81 ms 174608 KB Output is correct
8 Correct 82 ms 174676 KB Output is correct
9 Correct 88 ms 174588 KB Output is correct
10 Correct 88 ms 174580 KB Output is correct
11 Correct 80 ms 174584 KB Output is correct
12 Correct 79 ms 174408 KB Output is correct
13 Correct 78 ms 174540 KB Output is correct
14 Correct 80 ms 174388 KB Output is correct
15 Correct 89 ms 174404 KB Output is correct
16 Correct 86 ms 174656 KB Output is correct
17 Correct 81 ms 174564 KB Output is correct
18 Correct 84 ms 174604 KB Output is correct
19 Incorrect 84 ms 174340 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 174396 KB Output is correct
2 Correct 79 ms 174392 KB Output is correct
3 Correct 82 ms 174628 KB Output is correct
4 Correct 81 ms 174576 KB Output is correct
5 Correct 82 ms 174536 KB Output is correct
6 Correct 92 ms 174488 KB Output is correct
7 Correct 81 ms 174608 KB Output is correct
8 Correct 82 ms 174676 KB Output is correct
9 Correct 88 ms 174588 KB Output is correct
10 Correct 88 ms 174580 KB Output is correct
11 Correct 80 ms 174584 KB Output is correct
12 Correct 79 ms 174408 KB Output is correct
13 Correct 78 ms 174540 KB Output is correct
14 Correct 80 ms 174388 KB Output is correct
15 Correct 89 ms 174404 KB Output is correct
16 Correct 86 ms 174656 KB Output is correct
17 Correct 81 ms 174564 KB Output is correct
18 Correct 84 ms 174604 KB Output is correct
19 Incorrect 84 ms 174340 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 174396 KB Output is correct
2 Correct 79 ms 174392 KB Output is correct
3 Correct 82 ms 174628 KB Output is correct
4 Correct 81 ms 174576 KB Output is correct
5 Correct 82 ms 174536 KB Output is correct
6 Correct 92 ms 174488 KB Output is correct
7 Correct 81 ms 174608 KB Output is correct
8 Correct 82 ms 174676 KB Output is correct
9 Correct 88 ms 174588 KB Output is correct
10 Correct 88 ms 174580 KB Output is correct
11 Correct 80 ms 174584 KB Output is correct
12 Correct 79 ms 174408 KB Output is correct
13 Correct 78 ms 174540 KB Output is correct
14 Correct 80 ms 174388 KB Output is correct
15 Correct 89 ms 174404 KB Output is correct
16 Correct 86 ms 174656 KB Output is correct
17 Correct 81 ms 174564 KB Output is correct
18 Correct 84 ms 174604 KB Output is correct
19 Incorrect 84 ms 174340 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 88 ms 174388 KB Output is correct
2 Correct 80 ms 174372 KB Output is correct
3 Correct 79 ms 174292 KB Output is correct
4 Correct 83 ms 174356 KB Output is correct
5 Correct 89 ms 174416 KB Output is correct
6 Correct 78 ms 174460 KB Output is correct
7 Correct 81 ms 174504 KB Output is correct
8 Incorrect 87 ms 174428 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 174312 KB Output is correct
2 Correct 79 ms 174352 KB Output is correct
3 Correct 81 ms 174344 KB Output is correct
4 Correct 105 ms 174580 KB Output is correct
5 Correct 135 ms 174572 KB Output is correct
6 Correct 82 ms 174652 KB Output is correct
7 Correct 84 ms 174588 KB Output is correct
8 Correct 88 ms 174596 KB Output is correct
9 Correct 343 ms 181980 KB Output is correct
10 Correct 1119 ms 352772 KB Output is correct
11 Correct 95 ms 174572 KB Output is correct
12 Correct 149 ms 174820 KB Output is correct
13 Correct 1022 ms 358352 KB Output is correct
14 Correct 974 ms 358368 KB Output is correct
15 Correct 1428 ms 383788 KB Output is correct
16 Correct 1828 ms 434880 KB Output is correct
17 Correct 1226 ms 361696 KB Output is correct
18 Correct 1024 ms 361684 KB Output is correct
19 Correct 1193 ms 361828 KB Output is correct
20 Correct 1194 ms 361712 KB Output is correct
21 Correct 1211 ms 378044 KB Output is correct
22 Correct 879 ms 356952 KB Output is correct
23 Correct 79 ms 174396 KB Output is correct
24 Correct 79 ms 174392 KB Output is correct
25 Correct 82 ms 174628 KB Output is correct
26 Correct 81 ms 174576 KB Output is correct
27 Correct 82 ms 174536 KB Output is correct
28 Correct 92 ms 174488 KB Output is correct
29 Correct 81 ms 174608 KB Output is correct
30 Correct 82 ms 174676 KB Output is correct
31 Correct 88 ms 174588 KB Output is correct
32 Correct 88 ms 174580 KB Output is correct
33 Correct 80 ms 174584 KB Output is correct
34 Correct 79 ms 174408 KB Output is correct
35 Correct 78 ms 174540 KB Output is correct
36 Correct 80 ms 174388 KB Output is correct
37 Correct 89 ms 174404 KB Output is correct
38 Correct 86 ms 174656 KB Output is correct
39 Correct 81 ms 174564 KB Output is correct
40 Correct 84 ms 174604 KB Output is correct
41 Incorrect 84 ms 174340 KB Output isn't correct
42 Halted 0 ms 0 KB -