Submission #974162

# Submission time Handle Problem Language Result Execution time Memory
974162 2024-05-03T02:32:15 Z steveonalex Amusement Park (JOI17_amusement_park) C++17
100 / 100
32 ms 9792 KB
#include <bits/stdc++.h>
#include "Joi.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
 
// mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T& container, string separator = " ", string finish = "\n"){
        for(auto item: container) cout << item << separator;
        cout << finish;
    }
 
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


// void MessageBoard(int i, int j){cout << j << ",";}

struct DSU{
    int n;
    vector<int> parent;

    DSU(int _n){
        n = _n;
        parent.resize(n);
        for(int i = 0; i<n; ++i) parent[i] = i;
    }

    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool same_set(int u, int v){return find_set(u) == find_set(v);}
    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            parent[v] = u;
            return true;
        }
        return false;
    }
};

const int N = 1e4 + 69;
vector<int> graph[N];
vector<int> ver[N];
int color[N];
int untouched_cnt;

void dfs(int u, int p){
    ver[u].push_back(u);
    for(int v: graph[u]) if (v != p){
        dfs(v, u);
        for(int i: ver[v]) ver[u].push_back(i);
        ver[v].clear();
    }
    if (ver[u].size() >= 60 && (untouched_cnt - ver[u].size() >= 60 || u == p)){
        untouched_cnt -= ver[u].size();
        for(int i = 0; i< ver[u].size(); ++i){
            color[ver[u][i]] = i % 60;
        }
        ver[u].clear();
    }
}


void Joi(int n, int m, int A[], int B[], ll X, int T) {
    vector<pair<int, int>> edging(m);
    for(int i = 0; i<m; ++i){
        edging[i] = {A[i], B[i]};
        if (edging[i].first > edging[i].second) swap(edging[i].first, edging[i].second); 
    }
    sort(ALL(edging));

    DSU mst(n);
    for(pair<int, int> i: edging){
        if (mst.join_set(i.first, i.second)){
            int u = i.first, v = i.second;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
    }

    untouched_cnt = n;
    dfs(0, 0);

    for(int i = 0; i<n; ++i) {
        MessageBoard(i, GETBIT(X, color[i]));
    }
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     int n = 150, m = 149;
//     int A[m], B[m];
//     for(int i = 0; i<m; ++i) {
//         A[i] = i;
//         B[i] = i + 1;
//     }

//     Joi(n, m, A, B, 69, 1);
 
//     return 0;
// }
#include <bits/stdc++.h>
#include "Ioi.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
 
// mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T& container, string separator = " ", string finish = "\n"){
        for(auto item: container) cout << item << separator;
        cout << finish;
    }
 
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
 
const int INF = 1e9 + 69;
// int tape[] = {1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
 
// int Move(int v){
//     return tape[v];
// }
 
struct DSU{
    int n;
    vector<int> parent;
 
    DSU(int _n){
        n = _n;
        parent.resize(n);
        for(int i = 0; i<n; ++i) parent[i] = i;
    }
 
    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool same_set(int u, int v){return find_set(u) == find_set(v);}
    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            parent[v] = u;
            return true;
        }
        return false;
    }
};
 
const int N = 1e4 + 69;
vector<int> graph[N];
vector<int> ver[N], shit[N];
int color[N];
int untouched_cnt;
 
void dfs(int u, int p){
    ver[u].push_back(u);
    for(int v: graph[u]) if (v != p){
        dfs(v, u);
        for(int i: ver[v]) ver[u].push_back(i);
        ver[v].clear();
    }
    if (ver[u].size() >= 60 && (untouched_cnt - ver[u].size() >= 60 || u == p)){
        untouched_cnt -= ver[u].size();
        for(int i = 0; i< ver[u].size(); ++i){
            color[ver[u][i]] = i % 60;
        }
        shit[u] = ver[u];
        ver[u].clear();
    }
}
 
bool dp[N];
int sum[N], longest_path[N];
bool bit[60];
 
void go(int u, int p){
    for(int v: graph[u]) if (v != p){
        go(v, u);
        if (dp[v]) {
            dp[u] = 1;
            sum[u] += sum[v] + 1;
            maximize(longest_path[u], longest_path[v] + 1);
        }
    }
}
 
 
void bobby(int u, int p, bool flag){
    if (!flag){
        for(int v: graph[u]) if (v != p && dp[v]){
            bit[color[v]] = Move(v);
            bobby(v, u, 0);
        }
        if (u != p) Move(p);
    }
    else{
        pair<int, int> max_path = {-1, -1};
        for(int v: graph[u]) if (v != p && dp[v]) maximize(max_path, make_pair(longest_path[v], v));
        for(int v: graph[u]) if (v != p && dp[v] && v != max_path.second) {
            bit[color[v]] = Move(v);
            bobby(v, u, 0);
        }
        if (max_path.second != -1) {
            bit[color[max_path.second]] = Move(max_path.second);
            bobby(max_path.second, u, 1);
        }

    }
}
 
 
ll Ioi(int n, int m, int A[], int B[], int P, int V, int T) {
    vector<pair<int, int>> edging(m);
    for(int i = 0; i<m; ++i){
        edging[i] = {A[i], B[i]};
        if (edging[i].first > edging[i].second) swap(edging[i].first, edging[i].second); 
    }
    sort(ALL(edging));
 
    DSU mst(n);
    for(pair<int, int> i: edging){
        if (mst.join_set(i.first, i.second)){
            int u = i.first, v = i.second;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
    }
 
    untouched_cnt = n;
    dfs(0, 0);
 
    bit[color[P]] = V;
    for(int i = 0; i<n; ++i) {
        int pos = -1;
        for(int j = 0; j < shit[i].size(); ++j) if (shit[i][j] == P) pos = j;
 
        if (pos == -1) continue;
        pair<int, int> best = {INF, INF};
 
        for(int j = 0; j + 59 < shit[i].size();++j)if (j <= pos && pos <= j + 59){
            memset(dp, 0, sizeof dp); memset(sum, 0, sizeof sum); memset(longest_path, 0, sizeof longest_path);
            for(int k = j; k <= j+59; ++k) dp[shit[i][k]] = 1;
            go(P, P);
 
            minimize(best, make_pair(sum[P] - longest_path[P], j));
        }
        memset(dp, 0, sizeof dp); memset(sum, 0, sizeof sum);memset(longest_path, 0, sizeof longest_path);
        if (best.first > 120) assert(false);
        for(int k = best.second; k <= best.second+59; ++k) dp[shit[i][k]] = 1;
        go(P, P);
        bobby(P, P, 1);
 
        break;
    }
 
    ll X = 0;
    for(int i = 0; i<60; ++i) if (bit[i]) X += MASK(i);
 
    return X;
}
 
 
// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
//     int n = 150, m = 149;
//     int A[m], B[m];
//     for(int i = 0; i<m; ++i) {
//         A[i] = i;
//         B[i] = i + 1;
//     }
 
//     cout << Ioi(n, m, A, B, 69, tape[69], 1) << "\n";
 
 
//     return 0;
// }

Compilation message

Joi.cpp: In function 'void dfs(int, int)':
Joi.cpp:90:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int i = 0; i< ver[u].size(); ++i){
      |                        ~^~~~~~~~~~~~~~~

Ioi.cpp: In function 'void dfs(int, int)':
Ioi.cpp:95:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for(int i = 0; i< ver[u].size(); ++i){
      |                        ~^~~~~~~~~~~~~~~
Ioi.cpp: In function 'll Ioi(int, int, int*, int*, int, int, int)':
Ioi.cpp:166:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |         for(int j = 0; j < shit[i].size(); ++j) if (shit[i][j] == P) pos = j;
      |                        ~~^~~~~~~~~~~~~~~~
Ioi.cpp:171:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for(int j = 0; j + 59 < shit[i].size();++j)if (j <= pos && pos <= j + 59){
      |                        ~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2068 KB Output is correct
2 Correct 2 ms 2068 KB Output is correct
3 Correct 2 ms 2084 KB Output is correct
4 Correct 2 ms 2072 KB Output is correct
5 Correct 2 ms 2080 KB Output is correct
6 Correct 2 ms 2064 KB Output is correct
7 Correct 2 ms 2076 KB Output is correct
8 Correct 2 ms 2076 KB Output is correct
9 Correct 2 ms 2076 KB Output is correct
10 Correct 2 ms 2068 KB Output is correct
11 Correct 4 ms 2660 KB Output is correct
12 Correct 2 ms 2068 KB Output is correct
13 Correct 2 ms 2076 KB Output is correct
14 Correct 2 ms 2080 KB Output is correct
15 Correct 2 ms 2076 KB Output is correct
16 Correct 2 ms 2080 KB Output is correct
17 Correct 2 ms 2084 KB Output is correct
18 Correct 2 ms 2088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 5508 KB Output is correct
2 Correct 22 ms 5764 KB Output is correct
3 Correct 23 ms 5948 KB Output is correct
4 Correct 24 ms 4924 KB Output is correct
5 Correct 18 ms 8504 KB Output is correct
6 Correct 30 ms 6716 KB Output is correct
7 Correct 16 ms 6712 KB Output is correct
8 Correct 17 ms 6724 KB Output is correct
9 Correct 17 ms 6788 KB Output is correct
10 Correct 12 ms 5184 KB Output is correct
11 Correct 17 ms 4808 KB Output is correct
12 Correct 14 ms 5144 KB Output is correct
13 Correct 13 ms 4640 KB Output is correct
14 Correct 13 ms 4664 KB Output is correct
15 Correct 28 ms 5432 KB Output is correct
16 Correct 28 ms 5700 KB Output is correct
17 Correct 15 ms 4928 KB Output is correct
18 Correct 26 ms 4912 KB Output is correct
19 Correct 32 ms 5264 KB Output is correct
20 Correct 13 ms 7700 KB Output is correct
21 Correct 13 ms 7268 KB Output is correct
22 Correct 22 ms 6704 KB Output is correct
23 Correct 17 ms 7048 KB Output is correct
24 Correct 17 ms 6744 KB Output is correct
25 Correct 17 ms 7452 KB Output is correct
26 Correct 16 ms 7432 KB Output is correct
27 Correct 17 ms 6972 KB Output is correct
28 Correct 16 ms 6972 KB Output is correct
29 Correct 15 ms 6448 KB Output is correct
30 Correct 18 ms 6696 KB Output is correct
31 Correct 2 ms 2076 KB Output is correct
32 Correct 2 ms 2068 KB Output is correct
33 Correct 2 ms 2068 KB Output is correct
34 Correct 2 ms 2080 KB Output is correct
35 Correct 2 ms 2080 KB Output is correct
36 Correct 2 ms 2064 KB Output is correct
37 Correct 2 ms 2064 KB Output is correct
38 Correct 2 ms 2080 KB Output is correct
39 Correct 2 ms 2072 KB Output is correct
40 Correct 2 ms 2080 KB Output is correct
41 Correct 2 ms 2520 KB Output is correct
42 Correct 2 ms 2076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2064 KB Output is correct
2 Correct 2 ms 2132 KB Output is correct
3 Correct 2 ms 2080 KB Output is correct
4 Correct 4 ms 3144 KB Output is correct
5 Correct 3 ms 3136 KB Output is correct
6 Correct 4 ms 3140 KB Output is correct
7 Correct 4 ms 3232 KB Output is correct
8 Correct 4 ms 3148 KB Output is correct
9 Correct 18 ms 9684 KB Output is correct
10 Correct 18 ms 9792 KB Output is correct
11 Correct 15 ms 9788 KB Output is correct
12 Correct 2 ms 2080 KB Output is correct
13 Correct 1 ms 2072 KB Output is correct
14 Correct 2 ms 2068 KB Output is correct
15 Correct 2 ms 2064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 5560 KB Output is correct
2 Correct 22 ms 5908 KB Output is correct
3 Correct 20 ms 5752 KB Output is correct
4 Correct 20 ms 5060 KB Output is correct
5 Correct 24 ms 8820 KB Output is correct
6 Correct 25 ms 6884 KB Output is correct
7 Correct 19 ms 6780 KB Output is correct
8 Correct 15 ms 6212 KB Output is correct
9 Correct 17 ms 6452 KB Output is correct
10 Correct 12 ms 5184 KB Output is correct
11 Correct 15 ms 5184 KB Output is correct
12 Correct 14 ms 4640 KB Output is correct
13 Correct 16 ms 4636 KB Output is correct
14 Correct 13 ms 4648 KB Output is correct
15 Correct 29 ms 6212 KB Output is correct
16 Correct 16 ms 5512 KB Output is correct
17 Correct 15 ms 4924 KB Output is correct
18 Correct 27 ms 4984 KB Output is correct
19 Correct 20 ms 4920 KB Output is correct
20 Correct 13 ms 7492 KB Output is correct
21 Correct 16 ms 7136 KB Output is correct
22 Correct 16 ms 6972 KB Output is correct
23 Correct 17 ms 6704 KB Output is correct
24 Correct 17 ms 6708 KB Output is correct
25 Correct 18 ms 6972 KB Output is correct
26 Correct 18 ms 6844 KB Output is correct
27 Correct 19 ms 7204 KB Output is correct
28 Correct 17 ms 6708 KB Output is correct
29 Correct 15 ms 6432 KB Output is correct
30 Correct 16 ms 6700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5580 KB Output is correct
2 Correct 22 ms 5780 KB Output is correct
3 Correct 22 ms 5816 KB Output is correct
4 Correct 17 ms 5044 KB Output is correct
5 Correct 19 ms 9032 KB Output is correct
6 Correct 16 ms 6712 KB Output is correct
7 Correct 16 ms 6460 KB Output is correct
8 Correct 19 ms 6860 KB Output is correct
9 Correct 17 ms 6832 KB Output is correct
10 Correct 12 ms 5192 KB Output is correct
11 Correct 15 ms 5468 KB Output is correct
12 Correct 13 ms 4636 KB Output is correct
13 Correct 14 ms 4644 KB Output is correct
14 Correct 16 ms 4652 KB Output is correct
15 Correct 28 ms 5176 KB Output is correct
16 Correct 28 ms 6204 KB Output is correct
17 Correct 15 ms 4920 KB Output is correct
18 Correct 29 ms 4936 KB Output is correct
19 Correct 17 ms 5144 KB Output is correct
20 Correct 13 ms 7480 KB Output is correct
21 Correct 13 ms 7220 KB Output is correct
22 Correct 17 ms 6780 KB Output is correct
23 Correct 16 ms 6696 KB Output is correct
24 Correct 17 ms 6724 KB Output is correct
25 Correct 16 ms 6724 KB Output is correct
26 Correct 18 ms 6716 KB Output is correct
27 Correct 17 ms 7196 KB Output is correct
28 Correct 17 ms 7164 KB Output is correct
29 Correct 15 ms 6680 KB Output is correct
30 Correct 16 ms 6704 KB Output is correct
31 Correct 2 ms 2080 KB Output is correct
32 Correct 2 ms 2080 KB Output is correct
33 Correct 2 ms 2084 KB Output is correct
34 Correct 2 ms 2060 KB Output is correct
35 Correct 2 ms 2068 KB Output is correct
36 Correct 2 ms 2064 KB Output is correct
37 Correct 2 ms 2068 KB Output is correct
38 Correct 1 ms 2068 KB Output is correct
39 Correct 2 ms 2080 KB Output is correct
40 Correct 2 ms 2080 KB Output is correct
41 Correct 2 ms 2080 KB Output is correct
42 Correct 2 ms 2068 KB Output is correct