답안 #567148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
567148 2022-05-23T08:32:24 Z dantoh000 Jail (JOI22_jail) C++14
61 / 100
5000 ms 27776 KB
#include <bits/stdc++.h>
using namespace std;
int q;
int n,m;
int s[120005], t[120005];
int p[120005][18];
int d[120005];
vector<int> G2[120005];
vector<int> G[120005];
int vis[120005];
int lca(int x, int y){
    ///printf("LCA %d %d\n",x,y);
    if (d[x] > d[y]) swap(x,y);
    int diff = d[y]-d[x];
    for (int k = 0; k < 18; k++){
        if (diff>>k&1) {
            y = p[y][k];
        }
    }
    if (x == y) return x;
    for (int k = 17; k >= 0; k--){
        if (p[x][k] && p[y][k] && p[x][k] != p[y][k]){
            x = p[x][k];
            y = p[y][k];
        }
    }
    assert(p[x][0] == p[y][0]);
    return p[x][0];
}
void dfs(int u, int p_){
    p[u][0] = p_;
    for (auto v:  G[u]){
        if (v == p_) continue;
        d[v] = d[u]+1;
        dfs(v,u);

    }
}
bool check(int x, int y, int z){
    int r = lca(x,y);
    return (lca(r,z) == r) && (lca(x,z) == z || lca(y,z) == z);
}
bool dfs2(int u){
    vis[u] = 2;
    bool can = true;
    for (auto v : G2[u]){
        if (vis[v] == 2) return false;
        else if (vis[v] == 0){
            can &= dfs2(v);
        };
    }
    vis[u] = 1;
    return can;
}
int main(){
    scanf("%d",&q);
    while (q--){
        scanf("%d",&n);
        for (int i = 1; i <= n; i++) G[i].clear();
        for (int i = 1,a,b; i < n; i++){
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        d[1] = 0;
        dfs(1,0);
        for (int k = 1; k < 18; k++){
            for (int i = 1; i <= n; i++){
                p[i][k] = p[p[i][k-1]][k-1];
            }
        }
        scanf("%d",&m);
        for (int i = 0; i < m; i++){
            scanf("%d%d",&s[i],&t[i]);
        }
        for (int i = 0; i < m; i++) G2[i].clear();
        for (int i = 0; i < m; i++){
            for (int j = 0; j < m; j++){
                if (i == j) continue;
                if (check(s[i], t[i], s[j])){
                    ///printf("%d before %d\n",j,i);
                    G2[i].push_back(j);
                }
                if (check(s[i], t[i], t[j])){
                    ///printf("%d before %d\n",i,j);
                    G2[j].push_back(i);
                }
            }
        }
        bool can = true;
        fill(vis,vis+m, 0);
        for (int i = 0; i < m; i++){
            if (vis[i] == 0){
                can &= dfs2(i);
            }
        }
        if (can){
            printf("Yes\n");
        }
        else printf("No\n");

    }

}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
jail.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
jail.cpp:61:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |             scanf("%d%d",&a,&b);
      |             ~~~~~^~~~~~~~~~~~~~
jail.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         scanf("%d",&m);
      |         ~~~~~^~~~~~~~~
jail.cpp:74:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |             scanf("%d%d",&s[i],&t[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 5952 KB Output is correct
4 Correct 16 ms 6220 KB Output is correct
5 Correct 36 ms 6348 KB Output is correct
6 Correct 5 ms 5968 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 66 ms 6100 KB Output is correct
9 Correct 1957 ms 9224 KB Output is correct
10 Correct 155 ms 27724 KB Output is correct
11 Correct 29 ms 6108 KB Output is correct
12 Correct 745 ms 6448 KB Output is correct
13 Execution timed out 5010 ms 26980 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5844 KB Output is correct
2 Correct 5 ms 5844 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 6 ms 5972 KB Output is correct
6 Correct 6 ms 5968 KB Output is correct
7 Correct 5 ms 5968 KB Output is correct
8 Correct 6 ms 5956 KB Output is correct
9 Correct 5 ms 5956 KB Output is correct
10 Correct 5 ms 6028 KB Output is correct
11 Correct 5 ms 5972 KB Output is correct
12 Correct 5 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5844 KB Output is correct
2 Correct 5 ms 5844 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 6 ms 5972 KB Output is correct
6 Correct 6 ms 5968 KB Output is correct
7 Correct 5 ms 5968 KB Output is correct
8 Correct 6 ms 5956 KB Output is correct
9 Correct 5 ms 5956 KB Output is correct
10 Correct 5 ms 6028 KB Output is correct
11 Correct 5 ms 5972 KB Output is correct
12 Correct 5 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5972 KB Output is correct
16 Correct 5 ms 6040 KB Output is correct
17 Correct 5 ms 5972 KB Output is correct
18 Correct 8 ms 5996 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 7 ms 5980 KB Output is correct
21 Correct 8 ms 6036 KB Output is correct
22 Correct 6 ms 5972 KB Output is correct
23 Correct 5 ms 5972 KB Output is correct
24 Correct 4 ms 5952 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 8 ms 5952 KB Output is correct
28 Correct 4 ms 5956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5844 KB Output is correct
2 Correct 5 ms 5844 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 6 ms 5972 KB Output is correct
6 Correct 6 ms 5968 KB Output is correct
7 Correct 5 ms 5968 KB Output is correct
8 Correct 6 ms 5956 KB Output is correct
9 Correct 5 ms 5956 KB Output is correct
10 Correct 5 ms 6028 KB Output is correct
11 Correct 5 ms 5972 KB Output is correct
12 Correct 5 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5972 KB Output is correct
16 Correct 5 ms 6040 KB Output is correct
17 Correct 5 ms 5972 KB Output is correct
18 Correct 8 ms 5996 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 7 ms 5980 KB Output is correct
21 Correct 8 ms 6036 KB Output is correct
22 Correct 6 ms 5972 KB Output is correct
23 Correct 5 ms 5972 KB Output is correct
24 Correct 4 ms 5952 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 8 ms 5952 KB Output is correct
28 Correct 4 ms 5956 KB Output is correct
29 Correct 69 ms 6040 KB Output is correct
30 Correct 58 ms 6044 KB Output is correct
31 Correct 39 ms 6060 KB Output is correct
32 Correct 28 ms 5972 KB Output is correct
33 Correct 7 ms 6032 KB Output is correct
34 Correct 104 ms 6032 KB Output is correct
35 Correct 133 ms 6208 KB Output is correct
36 Correct 76 ms 5984 KB Output is correct
37 Correct 48 ms 5968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5844 KB Output is correct
2 Correct 5 ms 5844 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 6 ms 5972 KB Output is correct
6 Correct 6 ms 5968 KB Output is correct
7 Correct 5 ms 5968 KB Output is correct
8 Correct 6 ms 5956 KB Output is correct
9 Correct 5 ms 5956 KB Output is correct
10 Correct 5 ms 6028 KB Output is correct
11 Correct 5 ms 5972 KB Output is correct
12 Correct 5 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5972 KB Output is correct
16 Correct 5 ms 6040 KB Output is correct
17 Correct 5 ms 5972 KB Output is correct
18 Correct 8 ms 5996 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 7 ms 5980 KB Output is correct
21 Correct 8 ms 6036 KB Output is correct
22 Correct 6 ms 5972 KB Output is correct
23 Correct 5 ms 5972 KB Output is correct
24 Correct 4 ms 5952 KB Output is correct
25 Correct 5 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 8 ms 5952 KB Output is correct
28 Correct 4 ms 5956 KB Output is correct
29 Correct 69 ms 6040 KB Output is correct
30 Correct 58 ms 6044 KB Output is correct
31 Correct 39 ms 6060 KB Output is correct
32 Correct 28 ms 5972 KB Output is correct
33 Correct 7 ms 6032 KB Output is correct
34 Correct 104 ms 6032 KB Output is correct
35 Correct 133 ms 6208 KB Output is correct
36 Correct 76 ms 5984 KB Output is correct
37 Correct 48 ms 5968 KB Output is correct
38 Correct 2099 ms 9160 KB Output is correct
39 Correct 178 ms 27776 KB Output is correct
40 Correct 2809 ms 8132 KB Output is correct
41 Correct 2380 ms 6976 KB Output is correct
42 Correct 2053 ms 8388 KB Output is correct
43 Correct 59 ms 7068 KB Output is correct
44 Correct 2279 ms 6532 KB Output is correct
45 Correct 258 ms 19052 KB Output is correct
46 Correct 268 ms 19048 KB Output is correct
47 Correct 306 ms 22756 KB Output is correct
48 Correct 310 ms 22768 KB Output is correct
49 Correct 273 ms 19212 KB Output is correct
50 Correct 259 ms 19112 KB Output is correct
51 Correct 81 ms 20300 KB Output is correct
52 Correct 86 ms 20168 KB Output is correct
53 Correct 1520 ms 6944 KB Output is correct
54 Correct 262 ms 18904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5968 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 26 ms 5972 KB Output is correct
6 Correct 4 ms 5972 KB Output is correct
7 Correct 4 ms 5912 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 4 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5952 KB Output is correct
12 Correct 72 ms 6016 KB Output is correct
13 Correct 863 ms 6412 KB Output is correct
14 Correct 361 ms 6316 KB Output is correct
15 Correct 673 ms 6324 KB Output is correct
16 Execution timed out 5068 ms 19288 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 5952 KB Output is correct
4 Correct 16 ms 6220 KB Output is correct
5 Correct 36 ms 6348 KB Output is correct
6 Correct 5 ms 5968 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 66 ms 6100 KB Output is correct
9 Correct 1957 ms 9224 KB Output is correct
10 Correct 155 ms 27724 KB Output is correct
11 Correct 29 ms 6108 KB Output is correct
12 Correct 745 ms 6448 KB Output is correct
13 Execution timed out 5010 ms 26980 KB Time limit exceeded
14 Halted 0 ms 0 KB -