Submission #590678

# Submission time Handle Problem Language Result Execution time Memory
590678 2022-07-06T08:36:16 Z 반딧불(#8412) Amusement Park (JOI17_amusement_park) C++14
100 / 100
33 ms 5708 KB
#include "Joi.h"
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

namespace{
    int n, m;
    vector<int> link[10002];

    int depth[10002], par[10002], maxDepth[10002];
    bool visited[10002];
    bool existPath;
    int in[10002], inCnt;

    void dfs(int x, int dp=0){
        visited[x] = 1;
        depth[x] = dp;
        vector<int> newLink;
        for(auto y: link[x]){
            if(visited[y]) continue;
            newLink.push_back(y);
            par[y] = x;
            dfs(y, dp+1);
            maxDepth[x] = max(maxDepth[x], maxDepth[y]+1);
        }
        link[x].swap(newLink);
    }

    void dfs2(int x){
        visited[x] = 1;
        in[x] = inCnt++;
        for(auto y: link[x]){
            if(visited[y]) continue;
            dfs2(y);
        }
    }
}

void Joi(int N, int M, int A[], int B[], ll X, int T) {
    n = N, m = M;
    for(int i=0; i<m; i++){
        link[A[i]].push_back(B[i]);
        link[B[i]].push_back(A[i]);
    }
    dfs(0);
    existPath = (maxDepth[0] >= 59);
    if(existPath){
        for(int i=0; i<n; i++){
            MessageBoard(i, (X>>(depth[i]%60))&1);
        }
    }
    else{
        memset(visited, 0, sizeof(visited));
        dfs2(0);
        for(int i=0; i<n; i++){
            MessageBoard(i, (X>>(in[i])%60)&1);
        }
    }
}
#include "Ioi.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

namespace{
    const ll INF = (1LL<<60)-1;
    int n, m;
    vector<int> link[10002];

    int depth[10002], par[10002], maxDepth[10002];
    int LCADB[10002][16];
    bool visited[10002];
    bool existPath;
    int in[10002], inCnt, idx[10002];
    int minDist = 10000;
    vector<int> runway;

    int DP[10002];
    int L, R;

    void dfs(int x, int dp=0){
        visited[x] = 1;
        depth[x] = dp;
        vector<int> newLink;
        for(auto y: link[x]){
            if(visited[y]) continue;
            newLink.push_back(y);
            par[y] = x;
            dfs(y, dp+1);
            maxDepth[x] = max(maxDepth[x], maxDepth[y]+1);
        }
        link[x].swap(newLink);
    }

    void dfs2(int x){
        visited[x] = 1;
        idx[inCnt] = x;
        in[x] = inCnt++;
        for(auto y: link[x]){
            if(visited[y]) continue;
            dfs2(y);
        }
    }

    int getLCA(int x, int y){
        if(depth[x] > depth[y]) swap(x, y);
        for(int d=0; d<16; d++) if((depth[y]-depth[x]) & (1<<d)) y = LCADB[y][d];
        if(x==y) return x;
        for(int d=15; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
        return par[x];
    }

    int dist(int x, int y){
        return depth[x] + depth[y] - 2*depth[getLCA(x, y)];
    }

    void dfs3(int x){
        DP[x] = (L <= in[x] && in[x] <= R) ? 1 : 0;
        for(auto y: link[x]){
            dfs3(y);
            DP[x] += DP[y];
        }
    }

    bool dfs4(int x, int avoid, int p=-1){ /// üũ�� ������ ��� �湮�ϵ�, avoid�� ���� �������� �湮�Ѵ�.
        runway.push_back(x);
        if(x == avoid) return true;

        vector<int> linked;
        int dir = -1;
        for(auto y: link[x]) if(y!=p && DP[y]){
            if(dist(x, avoid) == dist(y, avoid)+1) dir = y;
            else linked.push_back(y);
        }
        if(x && par[x]!=p && DP[x]!=60){
            if(dist(x, avoid) == dist(par[x], avoid)+1) dir = par[x];
            else linked.push_back(par[x]);
        }
        if(dir!=-1) linked.push_back(dir);

        bool ret = 0;
        for(auto y: linked){
            if(dfs4(y, avoid, x)) ret = true;
            else runway.push_back(x);
        }
        return ret;
    }

    void trySolution(int l, int r, int x){ /// ���� in, �� in, ������ ��ȣ
        L = l, R = r;
        dfs3(0);
        int validPoints = 1;
        for(int i=0; i<n; i++) if(DP[i] && DP[i] < 60) validPoints++;
        int Farthest = 0, FarDist = -1;
        for(int i=l; i<=r; i++){
            int y = idx[i];
            if(dist(x, y) > FarDist) Farthest = y, FarDist = dist(x, y);
        }
        int len = 2*(validPoints-1) - FarDist;
        if(minDist <= len) return;

        minDist = len;
        runway.clear();
        bool tmp = dfs4(x, Farthest);
//        assert(tmp);
    }
}

ll Ioi(int N, int M, int A[], int B[], int P, int V, int T){
    n = N, m = M;
    for(int i=0; i<m; i++){
        link[A[i]].push_back(B[i]);
        link[B[i]].push_back(A[i]);
    }
    dfs(0);
    existPath = (maxDepth[0] >= 59);

    if(existPath){ /// �ö󰬴� �������� ��
        int x = P;
        ll ans = 0;

        if(depth[x] >= 59){ /// �ö󰡸� ��
            for(int cnt=0; cnt<59; cnt++){
                if(V) ans += (1LL << (depth[x] % 60));
                V = Move(x = par[x]);
            }
            if(V) ans += (1LL << (depth[x] % 60));
            return ans;
        }

        while(x){
            V = Move(x = par[x]);
        }
        for(int cnt=0; cnt<59; cnt++){
            if(V) ans += (1LL << cnt);
            int nxt = -1;
            for(auto y: link[x]){
                if(cnt + maxDepth[y] >= 58){
                    nxt = y;
                    break;
                }
            }
            assert(nxt != -1);
            V = Move(x = nxt);
        }
        if(V) ans += (1LL << 59);
        return ans;
    }

    int x = P;
    memset(visited, 0, sizeof(visited));
    dfs2(0);
    for(int i=0; i<n; i++) LCADB[i][0] = par[i];
    for(int d=1; d<16; d++) for(int i=0; i<n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];

    for(int a=max(0, in[x]-59); a<=in[x] && a+59<n; a++){
        int b = a+59;
        trySolution(a, b, x);
    }

    ll ans = 0;
    runway.erase(runway.begin());
    assert((int)runway.size() == minDist);
    for(auto nxt: runway){
        if(V) ans |= (1LL << (in[x]%60));
        V = Move(x = nxt);
    }
    if(V) ans |= (1LL << in[x]%60);
    return ans;
}

Compilation message

Ioi.cpp: In function 'void {anonymous}::trySolution(int, int, int)':
Ioi.cpp:107:14: warning: unused variable 'tmp' [-Wunused-variable]
  107 |         bool tmp = dfs4(x, Farthest);
      |              ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1160 KB Output is correct
3 Correct 3 ms 1164 KB Output is correct
4 Correct 0 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1152 KB Output is correct
7 Correct 2 ms 1284 KB Output is correct
8 Correct 2 ms 1292 KB Output is correct
9 Correct 2 ms 1164 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 4 ms 1464 KB Output is correct
12 Correct 2 ms 1164 KB Output is correct
13 Correct 2 ms 1164 KB Output is correct
14 Correct 2 ms 1284 KB Output is correct
15 Correct 2 ms 1156 KB Output is correct
16 Correct 2 ms 1156 KB Output is correct
17 Correct 2 ms 1156 KB Output is correct
18 Correct 2 ms 1156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5244 KB Output is correct
2 Correct 24 ms 5200 KB Output is correct
3 Correct 24 ms 5212 KB Output is correct
4 Correct 31 ms 3536 KB Output is correct
5 Correct 14 ms 4172 KB Output is correct
6 Correct 12 ms 3652 KB Output is correct
7 Correct 13 ms 3660 KB Output is correct
8 Correct 16 ms 3888 KB Output is correct
9 Correct 16 ms 3892 KB Output is correct
10 Correct 12 ms 3784 KB Output is correct
11 Correct 20 ms 3776 KB Output is correct
12 Correct 20 ms 3332 KB Output is correct
13 Correct 20 ms 3252 KB Output is correct
14 Correct 23 ms 3324 KB Output is correct
15 Correct 30 ms 3508 KB Output is correct
16 Correct 28 ms 3580 KB Output is correct
17 Correct 24 ms 3536 KB Output is correct
18 Correct 28 ms 3484 KB Output is correct
19 Correct 27 ms 3528 KB Output is correct
20 Correct 10 ms 3920 KB Output is correct
21 Correct 10 ms 3916 KB Output is correct
22 Correct 12 ms 3396 KB Output is correct
23 Correct 12 ms 3704 KB Output is correct
24 Correct 13 ms 3404 KB Output is correct
25 Correct 13 ms 3652 KB Output is correct
26 Correct 14 ms 3860 KB Output is correct
27 Correct 16 ms 3912 KB Output is correct
28 Correct 15 ms 3912 KB Output is correct
29 Correct 12 ms 3392 KB Output is correct
30 Correct 13 ms 3524 KB Output is correct
31 Correct 1 ms 1160 KB Output is correct
32 Correct 2 ms 1164 KB Output is correct
33 Correct 2 ms 1296 KB Output is correct
34 Correct 2 ms 1152 KB Output is correct
35 Correct 1 ms 1152 KB Output is correct
36 Correct 1 ms 1152 KB Output is correct
37 Correct 2 ms 1148 KB Output is correct
38 Correct 2 ms 1152 KB Output is correct
39 Correct 2 ms 1152 KB Output is correct
40 Correct 1 ms 1152 KB Output is correct
41 Correct 1 ms 1024 KB Output is correct
42 Correct 1 ms 1148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1032 KB Output is correct
2 Correct 0 ms 1160 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1820 KB Output is correct
5 Correct 3 ms 1828 KB Output is correct
6 Correct 2 ms 1824 KB Output is correct
7 Correct 2 ms 1828 KB Output is correct
8 Correct 2 ms 1816 KB Output is correct
9 Correct 11 ms 5708 KB Output is correct
10 Correct 12 ms 5684 KB Output is correct
11 Correct 16 ms 5700 KB Output is correct
12 Correct 1 ms 1024 KB Output is correct
13 Correct 1 ms 1152 KB Output is correct
14 Correct 1 ms 1024 KB Output is correct
15 Correct 1 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5188 KB Output is correct
2 Correct 20 ms 5148 KB Output is correct
3 Correct 23 ms 5216 KB Output is correct
4 Correct 27 ms 3620 KB Output is correct
5 Correct 15 ms 4952 KB Output is correct
6 Correct 15 ms 3936 KB Output is correct
7 Correct 13 ms 3652 KB Output is correct
8 Correct 13 ms 3396 KB Output is correct
9 Correct 15 ms 3528 KB Output is correct
10 Correct 13 ms 3728 KB Output is correct
11 Correct 25 ms 3788 KB Output is correct
12 Correct 20 ms 3252 KB Output is correct
13 Correct 20 ms 3312 KB Output is correct
14 Correct 23 ms 3368 KB Output is correct
15 Correct 28 ms 3480 KB Output is correct
16 Correct 28 ms 3520 KB Output is correct
17 Correct 28 ms 3504 KB Output is correct
18 Correct 28 ms 3552 KB Output is correct
19 Correct 30 ms 3508 KB Output is correct
20 Correct 10 ms 3908 KB Output is correct
21 Correct 10 ms 3908 KB Output is correct
22 Correct 13 ms 3656 KB Output is correct
23 Correct 12 ms 3656 KB Output is correct
24 Correct 12 ms 3656 KB Output is correct
25 Correct 16 ms 3908 KB Output is correct
26 Correct 14 ms 3752 KB Output is correct
27 Correct 17 ms 3980 KB Output is correct
28 Correct 13 ms 3496 KB Output is correct
29 Correct 11 ms 3380 KB Output is correct
30 Correct 13 ms 3640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5192 KB Output is correct
2 Correct 22 ms 5284 KB Output is correct
3 Correct 23 ms 5200 KB Output is correct
4 Correct 26 ms 3564 KB Output is correct
5 Correct 15 ms 5436 KB Output is correct
6 Correct 12 ms 3884 KB Output is correct
7 Correct 12 ms 3608 KB Output is correct
8 Correct 12 ms 4096 KB Output is correct
9 Correct 13 ms 3800 KB Output is correct
10 Correct 13 ms 3772 KB Output is correct
11 Correct 19 ms 4040 KB Output is correct
12 Correct 24 ms 3376 KB Output is correct
13 Correct 20 ms 3400 KB Output is correct
14 Correct 22 ms 3676 KB Output is correct
15 Correct 30 ms 3756 KB Output is correct
16 Correct 26 ms 3764 KB Output is correct
17 Correct 26 ms 3700 KB Output is correct
18 Correct 31 ms 3752 KB Output is correct
19 Correct 33 ms 3736 KB Output is correct
20 Correct 13 ms 4160 KB Output is correct
21 Correct 12 ms 4092 KB Output is correct
22 Correct 15 ms 3880 KB Output is correct
23 Correct 13 ms 3596 KB Output is correct
24 Correct 13 ms 3644 KB Output is correct
25 Correct 16 ms 3868 KB Output is correct
26 Correct 16 ms 3620 KB Output is correct
27 Correct 13 ms 4136 KB Output is correct
28 Correct 13 ms 4136 KB Output is correct
29 Correct 14 ms 3960 KB Output is correct
30 Correct 19 ms 3868 KB Output is correct
31 Correct 2 ms 1228 KB Output is correct
32 Correct 2 ms 1232 KB Output is correct
33 Correct 2 ms 1300 KB Output is correct
34 Correct 2 ms 1172 KB Output is correct
35 Correct 1 ms 1164 KB Output is correct
36 Correct 2 ms 1168 KB Output is correct
37 Correct 1 ms 1156 KB Output is correct
38 Correct 1 ms 1172 KB Output is correct
39 Correct 1 ms 1180 KB Output is correct
40 Correct 2 ms 1152 KB Output is correct
41 Correct 1 ms 1160 KB Output is correct
42 Correct 2 ms 1240 KB Output is correct