답안 #1069136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1069136 2024-08-21T16:17:27 Z Jarif_Rahman Amusement Park (JOI17_amusement_park) C++17
0 / 100
11 ms 5100 KB
#include "Joi.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

namespace {
    const int N = 10000;
    const int k = 60;
    int n, m;
    vector<int> graph[N];
    vector<int> tree[N];
    bitset<N> bl;
    int in[N], out[N];

    int msg[N];
    vector<int> nodes[N];

    int _p = 0;
    void spanning_tree(int nd, int ss){
        if(bl[nd]) return;
        bl[nd] = 1;
        if(ss != -1) tree[ss].push_back(nd);
        in[nd] = _p++;
        for(int x: graph[nd]) spanning_tree(x, nd);
        out[nd] = _p;
    }

    void create_initial_group(int nd){
        if(nodes[0].size() == k) return;
        nodes[0].push_back(nd);
        for(int x: tree[nd]) create_initial_group(x);
    }

    bool contains(int x, int y){
        return in[x] <= in[y] && out[x] >= out[y];
    }

    void dfs(int nd, int ss){
        if(!nodes[nd].empty()){
            for(int x: tree[nd]) dfs(x, ss);
            return;
        }
        nodes[nd] = nodes[ss];
        nodes[nd].push_back(nd);
        sort(nodes[nd].begin(), nodes[nd].end(), [&](int a, int b){
            return in[a] < in[b];
        });

        int rem = -1;
        bool ok = contains(nodes[nd][0], nodes[nd][1]);
        for(int x: nodes[nd]){
            if(x == nodes[nd][0] || x == nodes[nd][1]) continue;
            ok&=contains(nodes[nd][1], x);
        }
        if(ok) rem = nodes[nd][0];
        else{
            for(int x: nodes[nd]) if(x != nd){
                bool ok = 1;
                for(int y: nodes[nd]) if(y != x)
                    ok&=!contains(x, y);
                if(ok){
                    rem = x;
                    break;
                }
            }
        }
        msg[nd] = msg[rem];
        nodes[nd].erase(find(nodes[nd].begin(), nodes[nd].end(), rem));

        for(int x: tree[nd]) dfs(x, ss);
    }
}

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++){
        graph[A[i]].push_back(B[i]);
        graph[B[i]].push_back(A[i]);
    }

    spanning_tree(0, -1);
    create_initial_group(0);
    for(int x: nodes[0]) if(x) nodes[x] = nodes[0];
    for(int i = 0; i < k; i++) msg[nodes[0][i]] = i;
    dfs(0, -1);

    vector<int> bin;
    for(int i = 0; i < k; i++){
        bin.push_back(X%2);
        X/=2;
    }

    for(int i = 0; i < n; i++) MessageBoard(i, bin[msg[i]]);
}
#include "Ioi.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;


namespace {
    const int N = 10000;
    const int k = 60;
    int n, m;
    vector<int> graph[N];
    vector<int> tree[N];
    bitset<N> bl;
    int in[N], out[N];

    int msg[N];
    vector<int> nodes[N];

    int _p = 0;
    void spanning_tree(int nd, int ss){
        if(bl[nd]) return;
        bl[nd] = 1;
        if(ss != -1) tree[ss].push_back(nd);
        in[nd] = _p++;
        for(int x: graph[nd]) spanning_tree(x, nd);
        out[nd] = _p;
    }

    void create_initial_group(int nd){
        if(nodes[0].size() == k) return;
        nodes[0].push_back(nd);
        for(int x: tree[nd]) create_initial_group(x);
    }

    bool contains(int x, int y){
        return in[x] <= in[y] && out[x] >= out[y];
    }

    void dfs(int nd, int ss){
        if(!nodes[nd].empty()){
            for(int x: tree[nd]) dfs(x, ss);
            return;
        }
        nodes[nd] = nodes[ss];
        nodes[nd].push_back(nd);
        sort(nodes[nd].begin(), nodes[nd].end(), [&](int a, int b){
            return in[a] < in[b];
        });

        int rem = -1;
        bool ok = contains(nodes[nd][0], nodes[nd][1]);
        for(int x: nodes[nd]){
            if(x == nodes[nd][0] || x == nodes[nd][1]) continue;
            ok&=contains(nodes[nd][1], x);
        }
        if(ok) rem = nodes[nd][0];
        else{
            for(int x: nodes[nd]) if(x != nd){
                bool ok = 1;
                for(int y: nodes[nd]) if(y != x)
                    ok&=!contains(x, y);
                if(ok){
                    rem = x;
                    break;
                }
            }
        }
        msg[nd] = msg[rem];
        nodes[nd].erase(find(nodes[nd].begin(), nodes[nd].end(), rem));

        for(int x: tree[nd]) dfs(x, ss);
    }

    vector<int> bin(k);
    vector<int> tree2[N];

    void dfs2(int nd, int ss){
        for(int x: tree2[nd]) if(x != ss){
            bin[msg[x]] = Move(x);
            dfs2(x, nd);
            Move(nd);
        }
    }
}


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++){
        graph[A[i]].push_back(B[i]);
        graph[B[i]].push_back(A[i]);
    }

    spanning_tree(0, -1);
    create_initial_group(0);
    for(int x: nodes[0]) if(x) nodes[x] = nodes[0];
    for(int i = 0; i < k; i++) msg[nodes[0][i]] = i;
    dfs(0, -1);

    vector<bool> in_subtree(n, 0);
    for(int x: nodes[p]) in_subtree[x] = 1;
    for(int i = 0; i < m; i++) if(in_subtree[A[i]] && in_subtree[B[i]]){
        tree2[A[i]].push_back(B[i]);
        tree2[B[i]].push_back(A[i]);
    }

    bin[msg[p]] = v;
    dfs2(p, -1);

    ll X = 0;
    for(int i = 0; i < k; i++) if(bin[i]) X+=(1LL<<i);
    return X;
}

Compilation message

Joi.cpp: In function 'void {anonymous}::dfs(int, int)':
Joi.cpp:47:29: warning: array subscript -1 is below array bounds of 'std::vector<int> [10000]' [-Warray-bounds]
   47 |         nodes[nd] = nodes[ss];
      |                     ~~~~~~~~^
Joi.cpp:20:17: note: while referencing '{anonymous}::nodes'
   20 |     vector<int> nodes[N];
      |                 ^~~~~

Ioi.cpp: In function 'void {anonymous}::dfs(int, int)':
Ioi.cpp:48:29: warning: array subscript -1 is below array bounds of 'std::vector<int> [10000]' [-Warray-bounds]
   48 |         nodes[nd] = nodes[ss];
      |                     ~~~~~~~~^
Ioi.cpp:21:17: note: while referencing '{anonymous}::nodes'
   21 |     vector<int> nodes[N];
      |                 ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 2392 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 4952 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 2136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 8 ms 4952 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 11 ms 5100 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -