답안 #973659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973659 2024-05-02T09:10:40 Z steveonalex Speedrun (RMI21_speedrun) C++17
100 / 100
89 ms 2192 KB
#include <bits/stdc++.h>
#include "speedrun.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 N = 1001;

namespace Sub1{
    void assignHints (int subtask, int n, int A[], int B[]){
        vector<vector<int>> graph(n+1);
        for(int i = 1; i<=n-1; ++i){
            int u = A[i], v = B[i];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
     
        setHintLen(n);
        for(int i = 1; i<=n; ++i){
            for(int j: graph[i]){
                setHint(i, j, 1);
            }
        }
    }


    int hint_length;
    bool visited[N];


    void dfs(int u, int p){
        visited[u] = true;
        for(int j = 1; j <= hint_length; ++j) if (!visited[j] && getHint(j)){
            goTo(j);
            dfs(j, u);
        }
        if (p != u) goTo(p);
    }

    void speedrun(int subtask , int N, int start ){
        hint_length = getLength();
        dfs(start, start);
    }

}

namespace Sub2{
    void assignHints (int subtask, int n, int A[], int B[]){
        vector<vector<int>> graph(n+1);
        for(int i = 1; i<=n-1; ++i){
            int u = A[i], v = B[i];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
     
        pair<int, int> max_deg = {0, 0};
        for(int i = 1; i<=n; ++i) maximize(max_deg, make_pair((int)graph[i].size(), i));

        int centroid = max_deg.second;
        setHintLen(10);

        for(int i = 1; i<=n; ++i) {
            for(int j = 0; j < 10; ++j) if (GETBIT(centroid, j)) 
                setHint(i, j + 1, 1);
        }
    }


    int hint_length;
    vector<int> graph[N];

    void dfs(int u, int p){
        for(int v: graph[u]) if (v != p){
            goTo(v);
            dfs(v, u);
        }
        if (p != u) goTo(p);
    }

    void speedrun(int subtask , int n, int start ){
        hint_length = getLength();

        int centroid = 0;
        for(int j = 1; j <= hint_length; ++j) if (getHint(j)) centroid += MASK(j - 1);

        for(int i = 1; i <= n; ++i){
            if (i != centroid){
                graph[i].push_back(centroid);
                graph[centroid].push_back(i);
            }
        }

        dfs(start, start);
    }

}

namespace Sub3{
    void assignHints (int subtask, int n, int A[], int B[]){
        vector<vector<int>> graph(n+1);
        for(int i = 1; i<=n-1; ++i){
            int u = A[i], v = B[i];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }

        setHintLen(20);

        for(int i = 1; i<=n; ++i){
            int cur = 0;
            for(int j: graph[i]) cur = (cur << 10) + j;
            for(int j = 0; j < 20; ++j) if (GETBIT(cur, j)) setHint(i, j + 1, 1);
        }
    }


    int hint_length;


    void dfs(int u, int p){
        int cur = 0;
        for(int j = 1; j <= 20; ++j) if (getHint(j)) cur += MASK(j-1);
        vector<int> graph = {cur / 1024, cur % 1024};

        for(int v: graph) if (v != p && v != 0){
            goTo(v);
            dfs(v, u);
        }

        if (p != u) goTo(p);
    }

    void speedrun(int subtask , int N, int start ){
        hint_length = getLength();
        dfs(start, start);
    }

}


namespace Sub4{
    void assignHints (int subtask, int n, int A[], int B[]){
        vector<vector<int>> graph(n+1);
        for(int i = 1; i<=n-1; ++i){
            int u = A[i], v = B[i];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }

        setHintLen(310);

        for(int i = 1; i<=n; ++i) if (graph[i].size() <= 31) {
            string bruh;
            for(int j: graph[i]){
                for(int bit = 0; bit < 10; ++bit){
                    bruh.push_back('0' + GETBIT(j, bit));
                }
            }
            for(int j = 0; j< bruh.size(); ++j) if (bruh[j] == '1')
                setHint(i, j + 1, 1);
        }
    }


    int hint_length;
    int visited[N];
    int n;


    void dfs(int u, int p){
        visited[u] = 1;
        string s;

        vector<int> graph;
        for(int i = 0; i<310; i += 10){
            int cur = 0;
            for(int j = 0; j < 10; ++j) if (getHint(i+j+1)) cur += MASK(j);
            if (cur == 0) break;
            graph.push_back(cur); 
        }
        if (graph.empty()){
            for(int i = 1; i<=n; ++i) if (!visited[i] && goTo(i)){
                dfs(i, u);
            }
        }
        else{
            for(int v: graph) if (v != p) {
                goTo(v);
                dfs(v, u);
            }
        }

        if (p != u) goTo(p);
    }

    void speedrun(int subtask , int _n, int start ){
        n = _n;
        hint_length = getLength();
        dfs(start, start);
    }

}


namespace Sub5{
    vector<int> graph[N];
    int parent[N], num[N];
    int dfs_cnt = 0;

    void init(int u, int p){
        num[++dfs_cnt] = u;
        parent[u] = p;
        for(int v: graph[u]) if (v != p){
            init(v, u);
        }
    }

    void assignHints (int subtask, int n, int A[], int B[]){
        for(int i = 1; i<=n-1; ++i){
            int u = A[i], v = B[i];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }

        setHintLen(20);
        init(1, 1);

        for(int i = 1; i<=n; ++i){
            for(int j = 0; j< 10; ++j) if (GETBIT(parent[i], j)) setHint(i, j + 1, 1);
        }
        for(int i = 1; i<n; ++i){
            int u = num[i], v = num[i+1];
            for(int j = 0; j < 10; ++j) if (GETBIT(v, j)) setHint(u, j + 11, 1);
        }
    }

    int n;
    int hint_length;

    void speedrun(int subtask , int _n, int start ){
        n = _n;
        hint_length = getLength();

        int u = start;
        while(u != 1){
            int par = 0;
            for(int j = 0; j < 10; ++j) if (getHint(j + 1)) par += MASK(j);
            goTo(par);
            u = par;
        }

        for(int i = 0; i<n-1; ++i){
            int v = 0;
            for(int j = 0; j< 10; ++j) if (getHint(j + 11)) v += MASK(j);

            while(!goTo(v)){
                int par = 0;
                for(int j = 0; j < 10; ++j) if (getHint(j + 1)) par += MASK(j);
                goTo(par);
                u = par;
            }
            goTo(v);
            u = v;
        }
    }

}

 
void assignHints (int subtask, int n, int A[], int B[]){
    // if (subtask == 1) Sub1::assignHints(subtask, n, A, B);
    // if (subtask == 2) Sub2::assignHints(subtask, n, A, B);
    // if (subtask == 3) Sub3::assignHints(subtask, n, A, B);
    // if (subtask == 4) Sub4::assignHints(subtask, n, A, B);
    Sub5::assignHints(subtask, n, A, B);
}
 
 
 
void speedrun(int subtask , int N, int start ){
    // if (subtask == 1) Sub1::speedrun(subtask, N, start);
    // if (subtask == 2) Sub2::speedrun(subtask, N, start);
    // if (subtask == 3) Sub3::speedrun(subtask, N, start);
    // if (subtask == 4) Sub4::speedrun(subtask, N, start);
    Sub5::speedrun(subtask, N, start);
}
 
// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
//     return 0;
// }

Compilation message

speedrun.cpp: In function 'void Sub4::assignHints(int, int, int*, int*)':
speedrun.cpp:203:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  203 |             for(int j = 0; j< bruh.size(); ++j) if (bruh[j] == '1')
      |                            ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 1428 KB Output is correct
2 Correct 87 ms 1436 KB Output is correct
3 Correct 60 ms 1864 KB Output is correct
4 Correct 89 ms 1440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 1628 KB Output is correct
2 Correct 50 ms 2192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 1684 KB Output is correct
2 Correct 57 ms 1376 KB Output is correct
3 Correct 72 ms 1672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 1880 KB Output is correct
2 Correct 59 ms 1176 KB Output is correct
3 Correct 56 ms 1704 KB Output is correct
4 Correct 64 ms 1644 KB Output is correct
5 Correct 62 ms 1632 KB Output is correct
6 Correct 58 ms 1696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 1620 KB Output is correct
2 Correct 65 ms 1608 KB Output is correct
3 Correct 60 ms 1432 KB Output is correct
4 Correct 85 ms 1680 KB Output is correct
5 Correct 71 ms 1376 KB Output is correct
6 Correct 65 ms 1376 KB Output is correct
7 Correct 63 ms 1112 KB Output is correct