답안 #546977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546977 2022-04-09T05:20:52 Z ngrace Jail (JOI22_jail) C++14
77 / 100
5000 ms 844212 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;
v<int> ps;
v<int> pe;
v<v<int>> num;
void dfs(int cur, int from){
    if(depth[cur]!=-1) return;
    depth[cur]=depth[from]+1;
    par[cur][0]=from;
    if(ps[cur]!=-1) num[cur][0]++;
    if(pe[cur]!=-1) num[cur][0]++;
    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];
}

v<set<int>> diadj;
v<bool> prevrec;
v<bool> vis;
bool find_cycle(int cur){
    if(!vis[cur]){
        vis[cur]=true;
        prevrec[cur]=true;
        for(int it:diadj[cur]){
            if(!vis[it] && find_cycle(it)){
                return true;
            }
            else if(prevrec[it]){
                return true;
            }
        }
    }
    prevrec[cur]=false;
    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;
        }
 
        cin>>M;
        v<pii> rooms(M);
        ps = v<int>(N,-1);
        pe = v<int>(N,-1);
        for(int i=0; i<M; i++){
            cin>>rooms[i].fi>>rooms[i].se;
            rooms[i].fi--;
            rooms[i].se--;
            ps[rooms[i].fi]=i;
            pe[rooms[i].se]=i;
        }


        depth=v<int>(N,-1);
        par=v<v<int>>(N,v<int>(anc,-1));
        num=v<v<int>>(N,v<int>(anc,0));
        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];
                num[j][i]=num[par[j][i-1]][i-1]+num[j][i-1];
            }
        }


        bool cycle=false;
        diadj = v<set<int>>(M);
        for(int i=0; i<M; i++){
            int a=rooms[i].fi;
            int b=rooms[i].se;
            int l=lca(a,b);

            while(true){
                for(int j=anc-1; j>=0; j--){
                    if(depth[par[a][j]]<depth[l]) continue;
                    if(num[a][j]==0) a=par[a][j];
                }
                if(ps[a]!=-1 && ps[a]!=i) diadj[ps[a]].insert(i);
                if(pe[a]!=-1 && pe[a]!=i) diadj[i].insert(pe[a]);
                if(a==l) break;
                a=par[a][0];
            }
            while(b!=l){
                for(int j=anc-1; j>=0; j--){
                    if(depth[par[b][j]]<=depth[l]) continue;
                    if(num[b][j]==0) b=par[b][j];
                }
                if(ps[b]!=-1 && ps[b]!=i) diadj[ps[b]].insert(i);
                if(pe[b]!=-1 && pe[b]!=i) diadj[i].insert(pe[b]);
                b=par[b][0];
            }

            for(int it:diadj[i]){
                if(diadj[it].find(i)!=diadj[it].end()) cycle=true;
                if(cycle) break;
                for(int jt:diadj[it]){
                    if(diadj[jt].find(i)!=diadj[jt].end()) cycle=true;
                    if(diadj[jt].find(it)!=diadj[jt].end()) cycle=true;
                    if(cycle) break;
                }
                if(cycle) break;
            }
            if(cycle) break;
        }

        vis = v<bool>(M,false);
        prevrec = v<bool>(M,false);
        for(int i=0; i<M; i++){
            if(cycle) break;
            if(!vis[i] && find_cycle(i)){
                cycle=true;
                break;
            }
        }
 
        if(cycle) cout<<"No\n";
        else cout<<"Yes\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 24 ms 212 KB Output is correct
5 Correct 51 ms 212 KB Output is correct
6 Correct 3 ms 212 KB Output is correct
7 Correct 3 ms 212 KB Output is correct
8 Correct 4 ms 212 KB Output is correct
9 Correct 76 ms 796 KB Output is correct
10 Correct 73 ms 6884 KB Output is correct
11 Correct 13 ms 212 KB Output is correct
12 Correct 70 ms 364 KB Output is correct
13 Correct 105 ms 9432 KB Output is correct
14 Correct 112 ms 9536 KB Output is correct
15 Correct 129 ms 9160 KB Output is correct
16 Correct 182 ms 12480 KB Output is correct
17 Correct 125 ms 9504 KB Output is correct
18 Correct 150 ms 11928 KB Output is correct
19 Correct 125 ms 9524 KB Output is correct
20 Correct 123 ms 10232 KB Output is correct
21 Correct 121 ms 9652 KB Output is correct
22 Correct 107 ms 9396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 5 ms 340 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 5 ms 340 KB Output is correct
11 Correct 4 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 1 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 6 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 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 5 ms 340 KB Output is correct
11 Correct 4 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 1 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 6 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 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 1 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 5 ms 212 KB Output is correct
30 Correct 5 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 6 ms 408 KB Output is correct
35 Correct 10 ms 468 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 5 ms 340 KB Output is correct
11 Correct 4 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 1 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 6 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 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 1 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 5 ms 212 KB Output is correct
30 Correct 5 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 6 ms 408 KB Output is correct
35 Correct 10 ms 468 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 72 ms 796 KB Output is correct
39 Correct 81 ms 6896 KB Output is correct
40 Correct 115 ms 2888 KB Output is correct
41 Correct 123 ms 2868 KB Output is correct
42 Correct 116 ms 2976 KB Output is correct
43 Correct 113 ms 2872 KB Output is correct
44 Correct 34 ms 980 KB Output is correct
45 Correct 177 ms 36516 KB Output is correct
46 Correct 168 ms 36516 KB Output is correct
47 Correct 147 ms 38312 KB Output is correct
48 Correct 146 ms 38300 KB Output is correct
49 Correct 165 ms 36684 KB Output is correct
50 Correct 165 ms 36520 KB Output is correct
51 Correct 143 ms 37064 KB Output is correct
52 Correct 156 ms 37124 KB Output is correct
53 Correct 39 ms 3276 KB Output is correct
54 Correct 207 ms 36388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 14 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 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 340 KB Output is correct
13 Correct 69 ms 352 KB Output is correct
14 Correct 116 ms 344 KB Output is correct
15 Correct 101 ms 356 KB Output is correct
16 Correct 223 ms 38024 KB Output is correct
17 Correct 255 ms 40908 KB Output is correct
18 Correct 300 ms 46136 KB Output is correct
19 Correct 247 ms 38112 KB Output is correct
20 Correct 243 ms 37740 KB Output is correct
21 Correct 238 ms 38228 KB Output is correct
22 Correct 245 ms 42280 KB Output is correct
23 Correct 215 ms 39896 KB Output is correct
24 Correct 607 ms 83268 KB Output is correct
25 Correct 384 ms 59760 KB Output is correct
26 Correct 617 ms 84232 KB Output is correct
27 Correct 396 ms 51596 KB Output is correct
28 Correct 440 ms 58692 KB Output is correct
29 Correct 286 ms 45956 KB Output is correct
30 Correct 327 ms 46684 KB Output is correct
31 Correct 327 ms 47528 KB Output is correct
32 Correct 331 ms 45108 KB Output is correct
33 Correct 325 ms 47444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 24 ms 212 KB Output is correct
5 Correct 51 ms 212 KB Output is correct
6 Correct 3 ms 212 KB Output is correct
7 Correct 3 ms 212 KB Output is correct
8 Correct 4 ms 212 KB Output is correct
9 Correct 76 ms 796 KB Output is correct
10 Correct 73 ms 6884 KB Output is correct
11 Correct 13 ms 212 KB Output is correct
12 Correct 70 ms 364 KB Output is correct
13 Correct 105 ms 9432 KB Output is correct
14 Correct 112 ms 9536 KB Output is correct
15 Correct 129 ms 9160 KB Output is correct
16 Correct 182 ms 12480 KB Output is correct
17 Correct 125 ms 9504 KB Output is correct
18 Correct 150 ms 11928 KB Output is correct
19 Correct 125 ms 9524 KB Output is correct
20 Correct 123 ms 10232 KB Output is correct
21 Correct 121 ms 9652 KB Output is correct
22 Correct 107 ms 9396 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 2 ms 212 KB Output is correct
26 Correct 4 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 4 ms 340 KB Output is correct
29 Correct 4 ms 340 KB Output is correct
30 Correct 5 ms 340 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Correct 4 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 1 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 6 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 340 KB Output is correct
47 Correct 4 ms 340 KB Output is correct
48 Correct 1 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 5 ms 212 KB Output is correct
52 Correct 5 ms 340 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
55 Correct 4 ms 340 KB Output is correct
56 Correct 6 ms 408 KB Output is correct
57 Correct 10 ms 468 KB Output is correct
58 Correct 4 ms 340 KB Output is correct
59 Correct 5 ms 340 KB Output is correct
60 Correct 72 ms 796 KB Output is correct
61 Correct 81 ms 6896 KB Output is correct
62 Correct 115 ms 2888 KB Output is correct
63 Correct 123 ms 2868 KB Output is correct
64 Correct 116 ms 2976 KB Output is correct
65 Correct 113 ms 2872 KB Output is correct
66 Correct 34 ms 980 KB Output is correct
67 Correct 177 ms 36516 KB Output is correct
68 Correct 168 ms 36516 KB Output is correct
69 Correct 147 ms 38312 KB Output is correct
70 Correct 146 ms 38300 KB Output is correct
71 Correct 165 ms 36684 KB Output is correct
72 Correct 165 ms 36520 KB Output is correct
73 Correct 143 ms 37064 KB Output is correct
74 Correct 156 ms 37124 KB Output is correct
75 Correct 39 ms 3276 KB Output is correct
76 Correct 207 ms 36388 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 14 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 340 KB Output is correct
87 Correct 1 ms 212 KB Output is correct
88 Correct 6 ms 340 KB Output is correct
89 Correct 69 ms 352 KB Output is correct
90 Correct 116 ms 344 KB Output is correct
91 Correct 101 ms 356 KB Output is correct
92 Correct 223 ms 38024 KB Output is correct
93 Correct 255 ms 40908 KB Output is correct
94 Correct 300 ms 46136 KB Output is correct
95 Correct 247 ms 38112 KB Output is correct
96 Correct 243 ms 37740 KB Output is correct
97 Correct 238 ms 38228 KB Output is correct
98 Correct 245 ms 42280 KB Output is correct
99 Correct 215 ms 39896 KB Output is correct
100 Correct 607 ms 83268 KB Output is correct
101 Correct 384 ms 59760 KB Output is correct
102 Correct 617 ms 84232 KB Output is correct
103 Correct 396 ms 51596 KB Output is correct
104 Correct 440 ms 58692 KB Output is correct
105 Correct 286 ms 45956 KB Output is correct
106 Correct 327 ms 46684 KB Output is correct
107 Correct 327 ms 47528 KB Output is correct
108 Correct 331 ms 45108 KB Output is correct
109 Correct 325 ms 47444 KB Output is correct
110 Correct 113 ms 400 KB Output is correct
111 Correct 85 ms 344 KB Output is correct
112 Correct 190 ms 41836 KB Output is correct
113 Correct 222 ms 40016 KB Output is correct
114 Correct 179 ms 42660 KB Output is correct
115 Correct 128 ms 37388 KB Output is correct
116 Correct 292 ms 42964 KB Output is correct
117 Correct 309 ms 43084 KB Output is correct
118 Correct 175 ms 36420 KB Output is correct
119 Correct 179 ms 36520 KB Output is correct
120 Correct 31 ms 5312 KB Output is correct
121 Correct 788 ms 61020 KB Output is correct
122 Correct 790 ms 61108 KB Output is correct
123 Correct 260 ms 41188 KB Output is correct
124 Correct 171 ms 40192 KB Output is correct
125 Correct 237 ms 41492 KB Output is correct
126 Correct 238 ms 48100 KB Output is correct
127 Correct 213 ms 44000 KB Output is correct
128 Correct 187 ms 40992 KB Output is correct
129 Execution timed out 5084 ms 844212 KB Time limit exceeded
130 Halted 0 ms 0 KB -