Submission #1069145

# Submission time Handle Problem Language Result Execution time Memory
1069145 2024-08-21T16:23:07 Z Jarif_Rahman Amusement Park (JOI17_amusement_park) C++17
100 / 100
41 ms 16432 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];
    vector<pair<int, int>> tree_edges;
    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);
            tree_edges.push_back({ss, 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, nd);
            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, nd);
    }
}

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];
    vector<pair<int, int>> tree_edges;
    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);
            tree_edges.push_back({ss, 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, nd);
            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, nd);
    }

    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(auto ab: tree_edges) if(in_subtree[ab.first] && in_subtree[ab.second]){
        tree2[ab.first].push_back(ab.second);
        tree2[ab.second].push_back(ab.first);
    }

    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;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2588 KB Output is correct
2 Correct 2 ms 2588 KB Output is correct
3 Correct 2 ms 2580 KB Output is correct
4 Correct 2 ms 2588 KB Output is correct
5 Correct 2 ms 2584 KB Output is correct
6 Correct 2 ms 2588 KB Output is correct
7 Correct 3 ms 2592 KB Output is correct
8 Correct 2 ms 2588 KB Output is correct
9 Correct 2 ms 2592 KB Output is correct
10 Correct 2 ms 2572 KB Output is correct
11 Correct 4 ms 2920 KB Output is correct
12 Correct 1 ms 2580 KB Output is correct
13 Correct 2 ms 2588 KB Output is correct
14 Correct 2 ms 2608 KB Output is correct
15 Correct 2 ms 2588 KB Output is correct
16 Correct 2 ms 2600 KB Output is correct
17 Correct 2 ms 2596 KB Output is correct
18 Correct 2 ms 2596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16160 KB Output is correct
2 Correct 31 ms 16184 KB Output is correct
3 Correct 31 ms 16420 KB Output is correct
4 Correct 27 ms 14412 KB Output is correct
5 Correct 23 ms 15612 KB Output is correct
6 Correct 24 ms 14928 KB Output is correct
7 Correct 22 ms 15096 KB Output is correct
8 Correct 27 ms 15344 KB Output is correct
9 Correct 25 ms 15436 KB Output is correct
10 Correct 19 ms 14424 KB Output is correct
11 Correct 26 ms 14396 KB Output is correct
12 Correct 24 ms 13364 KB Output is correct
13 Correct 27 ms 13344 KB Output is correct
14 Correct 27 ms 13868 KB Output is correct
15 Correct 30 ms 14700 KB Output is correct
16 Correct 31 ms 14664 KB Output is correct
17 Correct 26 ms 14312 KB Output is correct
18 Correct 41 ms 14388 KB Output is correct
19 Correct 25 ms 14476 KB Output is correct
20 Correct 18 ms 15444 KB Output is correct
21 Correct 19 ms 15176 KB Output is correct
22 Correct 25 ms 14956 KB Output is correct
23 Correct 20 ms 15156 KB Output is correct
24 Correct 23 ms 14864 KB Output is correct
25 Correct 24 ms 14932 KB Output is correct
26 Correct 25 ms 15052 KB Output is correct
27 Correct 22 ms 15176 KB Output is correct
28 Correct 23 ms 15176 KB Output is correct
29 Correct 20 ms 13872 KB Output is correct
30 Correct 19 ms 14460 KB Output is correct
31 Correct 2 ms 2580 KB Output is correct
32 Correct 2 ms 2580 KB Output is correct
33 Correct 2 ms 2592 KB Output is correct
34 Correct 2 ms 2580 KB Output is correct
35 Correct 2 ms 2588 KB Output is correct
36 Correct 2 ms 2588 KB Output is correct
37 Correct 2 ms 2580 KB Output is correct
38 Correct 0 ms 2616 KB Output is correct
39 Correct 1 ms 2592 KB Output is correct
40 Correct 2 ms 2580 KB Output is correct
41 Correct 2 ms 2592 KB Output is correct
42 Correct 2 ms 2592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2572 KB Output is correct
2 Correct 2 ms 2584 KB Output is correct
3 Correct 2 ms 2588 KB Output is correct
4 Correct 6 ms 4660 KB Output is correct
5 Correct 4 ms 4656 KB Output is correct
6 Correct 4 ms 4668 KB Output is correct
7 Correct 4 ms 4660 KB Output is correct
8 Correct 4 ms 4668 KB Output is correct
9 Correct 18 ms 16324 KB Output is correct
10 Correct 20 ms 16356 KB Output is correct
11 Correct 20 ms 16368 KB Output is correct
12 Correct 2 ms 2588 KB Output is correct
13 Correct 2 ms 2584 KB Output is correct
14 Correct 2 ms 2588 KB Output is correct
15 Correct 2 ms 2588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16232 KB Output is correct
2 Correct 30 ms 16216 KB Output is correct
3 Correct 32 ms 16252 KB Output is correct
4 Correct 27 ms 14400 KB Output is correct
5 Correct 24 ms 15856 KB Output is correct
6 Correct 33 ms 15328 KB Output is correct
7 Correct 26 ms 14928 KB Output is correct
8 Correct 23 ms 14820 KB Output is correct
9 Correct 25 ms 14832 KB Output is correct
10 Correct 20 ms 14292 KB Output is correct
11 Correct 24 ms 14328 KB Output is correct
12 Correct 25 ms 13272 KB Output is correct
13 Correct 23 ms 13272 KB Output is correct
14 Correct 26 ms 13692 KB Output is correct
15 Correct 20 ms 14452 KB Output is correct
16 Correct 28 ms 14304 KB Output is correct
17 Correct 27 ms 14328 KB Output is correct
18 Correct 28 ms 14340 KB Output is correct
19 Correct 28 ms 14252 KB Output is correct
20 Correct 19 ms 15340 KB Output is correct
21 Correct 18 ms 14828 KB Output is correct
22 Correct 25 ms 14832 KB Output is correct
23 Correct 25 ms 14840 KB Output is correct
24 Correct 24 ms 14824 KB Output is correct
25 Correct 23 ms 14832 KB Output is correct
26 Correct 24 ms 14820 KB Output is correct
27 Correct 22 ms 15080 KB Output is correct
28 Correct 23 ms 14832 KB Output is correct
29 Correct 24 ms 13764 KB Output is correct
30 Correct 19 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16244 KB Output is correct
2 Correct 27 ms 16432 KB Output is correct
3 Correct 28 ms 16412 KB Output is correct
4 Correct 27 ms 14384 KB Output is correct
5 Correct 36 ms 16200 KB Output is correct
6 Correct 26 ms 14928 KB Output is correct
7 Correct 26 ms 15088 KB Output is correct
8 Correct 22 ms 15300 KB Output is correct
9 Correct 22 ms 15160 KB Output is correct
10 Correct 20 ms 14412 KB Output is correct
11 Correct 24 ms 14332 KB Output is correct
12 Correct 25 ms 13428 KB Output is correct
13 Correct 25 ms 13336 KB Output is correct
14 Correct 27 ms 13920 KB Output is correct
15 Correct 26 ms 14676 KB Output is correct
16 Correct 25 ms 14488 KB Output is correct
17 Correct 32 ms 14444 KB Output is correct
18 Correct 28 ms 14436 KB Output is correct
19 Correct 28 ms 14272 KB Output is correct
20 Correct 19 ms 15340 KB Output is correct
21 Correct 23 ms 15240 KB Output is correct
22 Correct 29 ms 14916 KB Output is correct
23 Correct 23 ms 14924 KB Output is correct
24 Correct 23 ms 15152 KB Output is correct
25 Correct 26 ms 15156 KB Output is correct
26 Correct 23 ms 14916 KB Output is correct
27 Correct 25 ms 15384 KB Output is correct
28 Correct 28 ms 15340 KB Output is correct
29 Correct 27 ms 14280 KB Output is correct
30 Correct 23 ms 14408 KB Output is correct
31 Correct 2 ms 2576 KB Output is correct
32 Correct 2 ms 2592 KB Output is correct
33 Correct 2 ms 2596 KB Output is correct
34 Correct 2 ms 2580 KB Output is correct
35 Correct 2 ms 2580 KB Output is correct
36 Correct 2 ms 2580 KB Output is correct
37 Correct 2 ms 2592 KB Output is correct
38 Correct 2 ms 2588 KB Output is correct
39 Correct 2 ms 2580 KB Output is correct
40 Correct 2 ms 2588 KB Output is correct
41 Correct 2 ms 2592 KB Output is correct
42 Correct 2 ms 2592 KB Output is correct