답안 #725004

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725004 2023-04-16T12:46:12 Z berr Jail (JOI22_jail) C++17
61 / 100
5000 ms 31060 KB
#include <bits/stdc++.h>
using namespace std; 
const int N = 1e5 + 2e4 + 37;
vector<int> adj[N], adj2[N], adj3[N], s(N), e(N), ti(N), to(N), vis(N), de(N), qe(N, -1), qs(N, -1);
int par[N][18], cnt, flag;


bool ia(int x, int y){
    return ti[x] <= ti[y] && to[x] >= to[y];
}

int d(int x, int y){
    if(x == y || x==-1 || y==-1) return 0;

    int sum = 0;

    if(ia(x, y)) swap(x, y);

    for (int i = 17; i >= 0; i--){
        if(!ia(par[x][i], y)){
            x = par[x][i];
            sum += 1<<i;
        }
    }

    sum++;
    x=par[x][0];



    swap(x, y);

    sum+=de[x]-de[y];
    return sum;
}

bool c(int x, int y){
    if(x==y || x==-1 || y==-1) return 0;

    if( (d(s[x], e[x]) == (d(s[x], e[y])+ d(e[y], e[x])))) return 1;
    swap(x, y);
    return (d(s[x], e[x]) == (d(s[x], s[y])+ d(s[y], e[x])));
}
bool c2(int x, int y){
    if(x==y) return 0;

    if(d(s[x], e[x]) == d(s[x], s[y])+ d(s[y], e[y]) + d(e[y], e[x])) return 1;
    if(d(s[x], e[x]) == d(s[x], e[y])+ d(e[y], s[y]) + d(s[y], e[x])) return 1;
    return 0;
}

void dfs(int v, int p){
    par[v][0] = p;
    de[v]=de[p]+1;
    ti[v] = cnt++;
    
    for (auto i: adj[v]){
        if (i == p) continue;
        dfs(i, v);
    }

    to[v] = cnt++;
}

void dfs2(int v, int z){
    vis[v] = z;

    for (auto i: adj2[v]){
        if (vis[i]==z) flag = 0;
        else if(vis[i]<z) dfs2(i, z);
    }

    vis[v]=z+1;
}

void ssolve(int n, int m){
    for(int i=0;i<m; i++){
        vector<int> path;

        int x=s[i], y=e[i];


        while(!ia(x, y)){
            path.push_back(x);
            x=par[x][0];
        }

        path.push_back(x);

        while(!ia(y, x)){
            path.push_back(y);
            y=par[y][0];
        }

        for(auto i: path){
            int l=qs[i];

            if (c(i, l)) adj2[i].push_back(l);


            if(c2(i, l)) flag = 0;

            l=qe[i];

            if (c(i, l)) adj2[i].push_back(l);


            if(c2(i, l)) flag = 0;  

        }
    }


    for(int i = 0; i < n; i++){
        if(vis[i]==0) dfs2(i, n*i+1);
    }


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

    for (int i = 0; i < n; i++){
        adj[i].clear();
        adj2[i].clear();
        vis[i] = 0;
        qs[i]=qe[i]=-1;
    }
}

void solve(){
    int n; cin >> n;
    cnt = 0;

    for (int i = 1; i < n; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    de[0]=-1;
    dfs(0, 0);

    for (int i = 1; i < 18; i++){
        for(int l = 0; l < n; l++){
            par[l][i] = par[par[l][i-1]][i-1];
        }
    }

    flag = 1;

    int m; cin >> m;


    for (int i = 0; i < m; i++){
        cin >> s[i] >> e[i];
        s[i]--; e[i]--;
        qs[s[i]]=i;
        qe[e[i]]=i;

    }
  /*  if(m>500){
        ssolve(n, m);
        return ;
    }*/

    for (int i = 0; i < m; i++){
        for (int l = 0; l < m; l++){
            if (c(i, l)) adj2[i].push_back(l);


            if(c2(i, l)) flag = 0;
            

        }
    }

    for(int i = 0; i < n; i++){
        if(vis[i]==0) dfs2(i, n*i+1);
    }


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

    for (int i = 0; i < n; i++){
        adj[i].clear();
        adj2[i].clear();
        vis[i] = 0;
        qs[i]=qe[i]=-1;
    }
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
        
    int t; cin>>t;

    while(t--){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12500 KB Output is correct
2 Correct 8 ms 12500 KB Output is correct
3 Correct 6 ms 12500 KB Output is correct
4 Correct 15 ms 12500 KB Output is correct
5 Correct 27 ms 12500 KB Output is correct
6 Correct 7 ms 12500 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 147 ms 12688 KB Output is correct
9 Correct 4961 ms 14728 KB Output is correct
10 Correct 246 ms 31044 KB Output is correct
11 Correct 49 ms 12452 KB Output is correct
12 Correct 1564 ms 12644 KB Output is correct
13 Execution timed out 5028 ms 30284 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 7 ms 12500 KB Output is correct
3 Correct 8 ms 12500 KB Output is correct
4 Correct 8 ms 12500 KB Output is correct
5 Correct 7 ms 12500 KB Output is correct
6 Correct 8 ms 12608 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 7 ms 12500 KB Output is correct
9 Correct 8 ms 12580 KB Output is correct
10 Correct 7 ms 12500 KB Output is correct
11 Correct 7 ms 12500 KB Output is correct
12 Correct 6 ms 12500 KB Output is correct
13 Correct 7 ms 12500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 7 ms 12500 KB Output is correct
3 Correct 8 ms 12500 KB Output is correct
4 Correct 8 ms 12500 KB Output is correct
5 Correct 7 ms 12500 KB Output is correct
6 Correct 8 ms 12608 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 7 ms 12500 KB Output is correct
9 Correct 8 ms 12580 KB Output is correct
10 Correct 7 ms 12500 KB Output is correct
11 Correct 7 ms 12500 KB Output is correct
12 Correct 6 ms 12500 KB Output is correct
13 Correct 7 ms 12500 KB Output is correct
14 Correct 6 ms 12500 KB Output is correct
15 Correct 6 ms 12500 KB Output is correct
16 Correct 7 ms 12500 KB Output is correct
17 Correct 7 ms 12500 KB Output is correct
18 Correct 8 ms 12500 KB Output is correct
19 Correct 6 ms 12500 KB Output is correct
20 Correct 8 ms 12500 KB Output is correct
21 Correct 8 ms 12500 KB Output is correct
22 Correct 8 ms 12500 KB Output is correct
23 Correct 7 ms 12500 KB Output is correct
24 Correct 7 ms 12500 KB Output is correct
25 Correct 7 ms 12500 KB Output is correct
26 Correct 8 ms 12500 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 7 ms 12500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 7 ms 12500 KB Output is correct
3 Correct 8 ms 12500 KB Output is correct
4 Correct 8 ms 12500 KB Output is correct
5 Correct 7 ms 12500 KB Output is correct
6 Correct 8 ms 12608 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 7 ms 12500 KB Output is correct
9 Correct 8 ms 12580 KB Output is correct
10 Correct 7 ms 12500 KB Output is correct
11 Correct 7 ms 12500 KB Output is correct
12 Correct 6 ms 12500 KB Output is correct
13 Correct 7 ms 12500 KB Output is correct
14 Correct 6 ms 12500 KB Output is correct
15 Correct 6 ms 12500 KB Output is correct
16 Correct 7 ms 12500 KB Output is correct
17 Correct 7 ms 12500 KB Output is correct
18 Correct 8 ms 12500 KB Output is correct
19 Correct 6 ms 12500 KB Output is correct
20 Correct 8 ms 12500 KB Output is correct
21 Correct 8 ms 12500 KB Output is correct
22 Correct 8 ms 12500 KB Output is correct
23 Correct 7 ms 12500 KB Output is correct
24 Correct 7 ms 12500 KB Output is correct
25 Correct 7 ms 12500 KB Output is correct
26 Correct 8 ms 12500 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 7 ms 12500 KB Output is correct
29 Correct 143 ms 12612 KB Output is correct
30 Correct 60 ms 12608 KB Output is correct
31 Correct 58 ms 12612 KB Output is correct
32 Correct 30 ms 12500 KB Output is correct
33 Correct 11 ms 12500 KB Output is correct
34 Correct 95 ms 12588 KB Output is correct
35 Correct 114 ms 12628 KB Output is correct
36 Correct 82 ms 12532 KB Output is correct
37 Correct 62 ms 12604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12500 KB Output is correct
2 Correct 7 ms 12500 KB Output is correct
3 Correct 8 ms 12500 KB Output is correct
4 Correct 8 ms 12500 KB Output is correct
5 Correct 7 ms 12500 KB Output is correct
6 Correct 8 ms 12608 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 7 ms 12500 KB Output is correct
9 Correct 8 ms 12580 KB Output is correct
10 Correct 7 ms 12500 KB Output is correct
11 Correct 7 ms 12500 KB Output is correct
12 Correct 6 ms 12500 KB Output is correct
13 Correct 7 ms 12500 KB Output is correct
14 Correct 6 ms 12500 KB Output is correct
15 Correct 6 ms 12500 KB Output is correct
16 Correct 7 ms 12500 KB Output is correct
17 Correct 7 ms 12500 KB Output is correct
18 Correct 8 ms 12500 KB Output is correct
19 Correct 6 ms 12500 KB Output is correct
20 Correct 8 ms 12500 KB Output is correct
21 Correct 8 ms 12500 KB Output is correct
22 Correct 8 ms 12500 KB Output is correct
23 Correct 7 ms 12500 KB Output is correct
24 Correct 7 ms 12500 KB Output is correct
25 Correct 7 ms 12500 KB Output is correct
26 Correct 8 ms 12500 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 7 ms 12500 KB Output is correct
29 Correct 143 ms 12612 KB Output is correct
30 Correct 60 ms 12608 KB Output is correct
31 Correct 58 ms 12612 KB Output is correct
32 Correct 30 ms 12500 KB Output is correct
33 Correct 11 ms 12500 KB Output is correct
34 Correct 95 ms 12588 KB Output is correct
35 Correct 114 ms 12628 KB Output is correct
36 Correct 82 ms 12532 KB Output is correct
37 Correct 62 ms 12604 KB Output is correct
38 Correct 4910 ms 14596 KB Output is correct
39 Correct 245 ms 31060 KB Output is correct
40 Correct 3815 ms 14392 KB Output is correct
41 Correct 2908 ms 13448 KB Output is correct
42 Correct 2746 ms 14716 KB Output is correct
43 Correct 65 ms 13300 KB Output is correct
44 Correct 2204 ms 12956 KB Output is correct
45 Correct 316 ms 24820 KB Output is correct
46 Correct 282 ms 24820 KB Output is correct
47 Correct 462 ms 27584 KB Output is correct
48 Correct 444 ms 27468 KB Output is correct
49 Correct 377 ms 24928 KB Output is correct
50 Correct 401 ms 24920 KB Output is correct
51 Correct 109 ms 25656 KB Output is correct
52 Correct 122 ms 25736 KB Output is correct
53 Correct 1961 ms 13444 KB Output is correct
54 Correct 272 ms 24764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12500 KB Output is correct
2 Correct 7 ms 12500 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 6 ms 12556 KB Output is correct
5 Correct 46 ms 12576 KB Output is correct
6 Correct 7 ms 12500 KB Output is correct
7 Correct 7 ms 12500 KB Output is correct
8 Correct 6 ms 12500 KB Output is correct
9 Correct 8 ms 12512 KB Output is correct
10 Correct 8 ms 12500 KB Output is correct
11 Correct 6 ms 12628 KB Output is correct
12 Correct 100 ms 12600 KB Output is correct
13 Correct 968 ms 12840 KB Output is correct
14 Correct 434 ms 12584 KB Output is correct
15 Correct 700 ms 12584 KB Output is correct
16 Execution timed out 5074 ms 25036 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12500 KB Output is correct
2 Correct 8 ms 12500 KB Output is correct
3 Correct 6 ms 12500 KB Output is correct
4 Correct 15 ms 12500 KB Output is correct
5 Correct 27 ms 12500 KB Output is correct
6 Correct 7 ms 12500 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 147 ms 12688 KB Output is correct
9 Correct 4961 ms 14728 KB Output is correct
10 Correct 246 ms 31044 KB Output is correct
11 Correct 49 ms 12452 KB Output is correct
12 Correct 1564 ms 12644 KB Output is correct
13 Execution timed out 5028 ms 30284 KB Time limit exceeded
14 Halted 0 ms 0 KB -