Submission #546937

# Submission time Handle Problem Language Result Execution time Memory
546937 2022-04-09T03:15:54 Z ngrace Jail (JOI22_jail) C++14
66 / 100
5000 ms 23300 KB
#include <vector>
#include <iostream>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
#define v vector
#define pii pair<int,int>
#define fi first
#define se second

int N,M;
int anc=20;
v<v<int>> adj;
v<int> depth;
v<v<int>> par;
void dfs(int cur, int from){
    if(depth[cur]!=-1) return;
    depth[cur]=depth[from]+1;
    par[cur][0]=from;
    for(int it:adj[cur]) dfs(it, cur);
}
bool is_anc(int cur, int other){
    for(int i=anc-1; i>=0; i--){
        if(depth[par[cur][i]]>=depth[other]) cur=par[cur][i];
    }
    if(cur==other) return true;
    return false;
}

int lca(int a, int b){
    if(depth[a]<depth[b]) swap(a,b);
    for(int i=anc-1; i>=0; i--){
        if(depth[par[a][i]]>=depth[b]) a=par[a][i];
    }
    if(a==b) return a;
    for(int i=anc-1; i>=0; i--){
        if(par[a][i]!=par[b][i]){
            a=par[a][i];
            b=par[b][i];
        }
    }
    return par[a][0];
}
bool path(int a, int b, int c){//does c lie on the path of a and b
    int l=lca(a,b);
    if(is_anc(c,l) && (is_anc(a,c)||is_anc(b,c))) return true;
    return false;
}

v<int> stack;
v<bool> vis;
bool find_cycle(int cur){
    if(vis[cur]){
        for(int it:stack){
            if(it==cur) return true;
        }
        return false;
    }
    vis[cur]=true;
    stack.push_back(cur);
    for(int it:adj[cur]){
        bool b=find_cycle(it);
        if(b) return true;
    }
    stack.pop_back();
    return false;
}

int main(){
    int Q;
    cin>>Q;
    for(int q=0;q<Q;q++){
        cin>>N;
        adj=v<v<int>>(N);
        bool subone=true;
        for(int i=0; i<N-1; i++){
            int a,b;
            cin>>a>>b;
            a--;
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
            if(a+1!=b) subone=false;
        }

        if(subone){
            cin>>M;
            v<pii> rooms(M);
            v<pii> rooms2(M);
            for(int i=0; i<M; i++){
                int a,b;
                cin>>a>>b;
                if(b>=a){
                    rooms.push_back({a,b});
                    rooms2.push_back({-a,b});
                    rooms2.push_back({-b,a});
                }
                else{
                    rooms.push_back({a,-b});
                    rooms.push_back({b,-a});
                    rooms2.push_back({-a,-b});
                }
            }

            sort(rooms.begin(), rooms.end());
            sort(rooms2.begin(),rooms2.end());

            pii cur={-1,-1};
            pii def={-1,-1};
            bool out=true;
            for(pii it:rooms){
                if(it.se<=0){
                    if(cur!=def && cur.se>=it.fi){
                        out=false;
                        break;
                    }
                }
                else{
                    if(it.se<cur.se){
                        out=false;
                        break;
                    }
                    cur=it;
                }
            }

            cur={1,1};
            def={1,1};
            for(pii it:rooms2){
                if(it.se>=0){
                    if(cur!=def && cur.se>=it.fi){
                        out=false;
                        break;
                    }
                }
                else{
                    if(it.se<cur.se && cur!=def){
                        out=false;
                        break;
                    }
                    cur=it;
                }
            }

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

            continue;
        }

        depth=v<int>(N,-1);
        par=v<v<int>>(N,v<int>(anc,-1));
        dfs(0,0);
        for(int i=1; i<anc; i++){
            for(int j=0; j<N; j++){
                par[j][i]=par[par[j][i-1]][i-1];
            }
        }

        cin>>M;
        v<pii> rooms(M);
        for(int i=0; i<M; i++){
            cin>>rooms[i].fi>>rooms[i].se;
            rooms[i].fi--;
            rooms[i].se--;
        }

        adj = v<v<int>>(M);
        bool cycle=false;
        for(int i=0; i<M; i++){
            for(int j=i+1; j<M; j++){
                bool start1=path(rooms[j].fi, rooms[j].se, rooms[i].fi);
                bool start2=path(rooms[i].fi, rooms[i].se, rooms[j].fi);
                bool end1=path(rooms[j].fi, rooms[j].se, rooms[i].se);
                bool end2=path(rooms[i].fi, rooms[i].se, rooms[j].se);

                if((start1&&start2) || (start1&&end1) || (start2&&end2) || (end1&&end2)) cycle=true;
                if(cycle) break;

                if(start1 || end2) adj[i].push_back(j);
                if(start2 || end1) adj[j].push_back(i);
            }
            if(cycle) break;
        }

        vis = v<bool>(M,false);
        for(int i=0; i<M; i++){
            if(cycle) break;
            while(stack.size()>0) stack.pop_back();
            if(!vis[i] && find_cycle(i)){
                cycle=true;
                break;
            }
        }

        if(cycle) cout<<"No\n";
        else cout<<"Yes\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 25 ms 212 KB Output is correct
5 Correct 51 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 3 ms 212 KB Output is correct
8 Correct 3 ms 212 KB Output is correct
9 Correct 68 ms 788 KB Output is correct
10 Correct 74 ms 6792 KB Output is correct
11 Correct 13 ms 212 KB Output is correct
12 Correct 69 ms 340 KB Output is correct
13 Correct 107 ms 9440 KB Output is correct
14 Correct 110 ms 9444 KB Output is correct
15 Correct 117 ms 9144 KB Output is correct
16 Correct 174 ms 12512 KB Output is correct
17 Correct 112 ms 9372 KB Output is correct
18 Correct 152 ms 12044 KB Output is correct
19 Correct 116 ms 9412 KB Output is correct
20 Correct 111 ms 10136 KB Output is correct
21 Correct 118 ms 9692 KB Output is correct
22 Correct 113 ms 9372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 212 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 212 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 3 ms 212 KB Output is correct
30 Correct 25 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 33 ms 340 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 82 ms 340 KB Output is correct
35 Correct 59 ms 360 KB Output is correct
36 Correct 142 ms 336 KB Output is correct
37 Correct 91 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 212 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 212 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 3 ms 212 KB Output is correct
30 Correct 25 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 33 ms 340 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 82 ms 340 KB Output is correct
35 Correct 59 ms 360 KB Output is correct
36 Correct 142 ms 336 KB Output is correct
37 Correct 91 ms 340 KB Output is correct
38 Correct 74 ms 724 KB Output is correct
39 Correct 77 ms 6804 KB Output is correct
40 Correct 1769 ms 2060 KB Output is correct
41 Correct 3825 ms 2168 KB Output is correct
42 Correct 2427 ms 2180 KB Output is correct
43 Correct 109 ms 2116 KB Output is correct
44 Correct 2359 ms 712 KB Output is correct
45 Correct 293 ms 21364 KB Output is correct
46 Correct 408 ms 21440 KB Output is correct
47 Correct 479 ms 23300 KB Output is correct
48 Correct 522 ms 23300 KB Output is correct
49 Correct 129 ms 21552 KB Output is correct
50 Correct 447 ms 21540 KB Output is correct
51 Correct 152 ms 22016 KB Output is correct
52 Correct 129 ms 22052 KB Output is correct
53 Correct 3067 ms 2308 KB Output is correct
54 Correct 382 ms 21324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 12 ms 212 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 140 ms 332 KB Output is correct
13 Correct 142 ms 340 KB Output is correct
14 Correct 636 ms 212 KB Output is correct
15 Correct 638 ms 340 KB Output is correct
16 Execution timed out 5070 ms 21896 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 25 ms 212 KB Output is correct
5 Correct 51 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 3 ms 212 KB Output is correct
8 Correct 3 ms 212 KB Output is correct
9 Correct 68 ms 788 KB Output is correct
10 Correct 74 ms 6792 KB Output is correct
11 Correct 13 ms 212 KB Output is correct
12 Correct 69 ms 340 KB Output is correct
13 Correct 107 ms 9440 KB Output is correct
14 Correct 110 ms 9444 KB Output is correct
15 Correct 117 ms 9144 KB Output is correct
16 Correct 174 ms 12512 KB Output is correct
17 Correct 112 ms 9372 KB Output is correct
18 Correct 152 ms 12044 KB Output is correct
19 Correct 116 ms 9412 KB Output is correct
20 Correct 111 ms 10136 KB Output is correct
21 Correct 118 ms 9692 KB Output is correct
22 Correct 113 ms 9372 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 3 ms 212 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 4 ms 340 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 4 ms 340 KB Output is correct
43 Correct 4 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 4 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 4 ms 340 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 3 ms 212 KB Output is correct
52 Correct 25 ms 340 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 33 ms 340 KB Output is correct
55 Correct 7 ms 340 KB Output is correct
56 Correct 82 ms 340 KB Output is correct
57 Correct 59 ms 360 KB Output is correct
58 Correct 142 ms 336 KB Output is correct
59 Correct 91 ms 340 KB Output is correct
60 Correct 74 ms 724 KB Output is correct
61 Correct 77 ms 6804 KB Output is correct
62 Correct 1769 ms 2060 KB Output is correct
63 Correct 3825 ms 2168 KB Output is correct
64 Correct 2427 ms 2180 KB Output is correct
65 Correct 109 ms 2116 KB Output is correct
66 Correct 2359 ms 712 KB Output is correct
67 Correct 293 ms 21364 KB Output is correct
68 Correct 408 ms 21440 KB Output is correct
69 Correct 479 ms 23300 KB Output is correct
70 Correct 522 ms 23300 KB Output is correct
71 Correct 129 ms 21552 KB Output is correct
72 Correct 447 ms 21540 KB Output is correct
73 Correct 152 ms 22016 KB Output is correct
74 Correct 129 ms 22052 KB Output is correct
75 Correct 3067 ms 2308 KB Output is correct
76 Correct 382 ms 21324 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 12 ms 212 KB Output is correct
82 Correct 2 ms 340 KB Output is correct
83 Correct 2 ms 340 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 1 ms 212 KB Output is correct
87 Correct 1 ms 212 KB Output is correct
88 Correct 140 ms 332 KB Output is correct
89 Correct 142 ms 340 KB Output is correct
90 Correct 636 ms 212 KB Output is correct
91 Correct 638 ms 340 KB Output is correct
92 Execution timed out 5070 ms 21896 KB Time limit exceeded
93 Halted 0 ms 0 KB -