답안 #645598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
645598 2022-09-27T12:26:48 Z TimDee Speedrun (RMI21_speedrun) C++17
100 / 100
236 ms 8860 KB
#include "speedrun.h"
#include <bits/stdc++.h>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<vector<int>> adj(1e3+1);
vector<int> p(1e3+1,0);
void dfs(int u, int P) {
    for (auto v:adj[u]) {
        if (v==P) continue;
        p[v]=u;
        dfs(v,u);
    }
}
vector<pair<int,int>> toadd;
vector<pair<int,int>> edgeto(1e3+1);
void dfs2(int u, int P) {
    int cnt=0;
    for (auto v:adj[u]) {
        if (v==P) continue;
        if (cnt==0) {
            for (int j=10; j; --j) {
                setHint(u,j+10,!!(v&(1<<(j-1))));
            }
            edgeto[u]={u,v};
            ++cnt;
        } else {
            toadd.push_back({u,v});
        }
    }
    if (cnt==0) {
        //cout<<u<<' '<<P<<": ";
        //for (auto x:toadd) cout<<x.first<<' '<<x.second<<"  ";
        //cout<<'\n';
        if (toadd.size()==0) return;
        auto k=toadd.back(); toadd.pop_back();
        for (int j=10; j; --j) {
            setHint(u,j+10,!!(k.second&(1<<(j-1))));
        }
        edgeto[u]={k.first,k.second};
    }
    dfs2(edgeto[u].second,edgeto[u].first);
}

void assignHints(int id,int n, int a[], int b[]) {
    if (id==2) {
        setHintLen(20);
        vector<int>deg(n+1,0);
        for(int i=0;i<n-1;++i) deg[a[i]]++;
        for(int i=0;i<n-1;++i) deg[b[i]]++;
        int mx=0;
        for(int i=1;i<=n;++i) if (deg[i]>deg[mx]) mx=i;
        for (int i=1; i<=n; ++i) {
            if (i==mx) setHint(i,20,1);
            for (int j=11; j>0; --j) {
                setHint(i,j,!!(mx&(1<<(j-1))));
            }
        }
        return;
    }
    if (id==3) {
        setHintLen(20);
        vector<pair<int,int>> adj(n+1,{0,0});
        for (int i=1; i<n; ++i) {
            if (adj[a[i]].first) adj[a[i]].second=b[i];
            else adj[a[i]].first=b[i];
            if (adj[b[i]].first) adj[b[i]].second=a[i];
            else adj[b[i]].first=a[i];
        }
        for (int i=1; i<=n; ++i) {
            for (int j=10; j; --j) {
                setHint(i,j,!!(adj[i].first&(1<<(j-1))));
            }
            for (int j=10; j; --j) {
                setHint(i,j+10,!!(adj[i].second&(1<<(j-1))));
            }
        }
        return;
    }
    for (int i=1; i<n; ++i) {
        adj[a[i]].push_back(b[i]);
        adj[b[i]].push_back(a[i]);
    }
    dfs(1,0);
    setHintLen(20);
    for (int i=1; i<=n; ++i) {
        for (int j=10; j; --j) {
            setHint(i,j,!!(p[i]&(1<<(j-1))));
        }
    }
    dfs2(1,0);
}

vector<int> par(1e3+1,0);
int n;

void climb(int u, int need) {
    int p=0;
    for (int j=10; j; --j) p+=getHint(j)<<(j-1);
    if (p==need) return;
    par[u]=p;
    goTo(p);
    climb(u,need);
}

void visit(int u) {
    int y=0;
    int p=0;
    for (int j=10; j; --j) p+=getHint(j)<<(j-1);
    par[u]=p;
    for (int j=10; j; --j) y+=getHint(j+10)<<(j-1);
    //cout<<u<<' '<<par[u]<<' '<<y<<'\n';
    //int k; cin>>k;
    //if (k==-1) exit(0);
    if (y==0) return;
    bool b=goTo(y);
    if (b==1) {
        visit(y);
    } else {
        do {
            goTo(par[u]);
            u=par[u];
            b=goTo(y);
        } while (b==0);
        visit(y);
    }
}
int center=0;
vector<vector<int>> adj2(1e3+1,vector<int>(1e3+1,0));
void dfs22(int u, int p) {
    int l=0, r=0;
    for (int j=10; j>0; --j) {
        l+=getHint(j)<<(j-1);
        r+=getHint(j+10)<<(j-1);
    }
    adj2[u][l]=1, adj2[u][r]=1;
    for (int i=1; i<=n; ++i) {
        if (i==p) continue;
        if (adj2[u][i]) {
            goTo(i);
            dfs22(i,u);
        }
    }
    if (p==-1) return;
    goTo(p);
}

void speedrun(int id,int N,int start) {
    n=N;

    if (id==2) {
        for (int j=11; j>0; --j) {
            center+=getHint(j)<<(j-1);
        }
        bool b=getHint(20);
        if (!b) {
            goTo(center);
        }
        for (int i=1; i<=n; ++i) {
            if (i==center) continue;
            goTo(i);
            goTo(center);
        }
        return;
    }
    if (id==3) {
        dfs22(start,-1);
        return;
    }

    climb(start,0);
    visit(1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 8664 KB Output is correct
2 Correct 177 ms 8848 KB Output is correct
3 Correct 128 ms 8612 KB Output is correct
4 Correct 188 ms 8680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 8612 KB Output is correct
2 Correct 131 ms 8608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 208 ms 8672 KB Output is correct
2 Correct 164 ms 8804 KB Output is correct
3 Correct 191 ms 8692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 8660 KB Output is correct
2 Correct 179 ms 8668 KB Output is correct
3 Correct 152 ms 8860 KB Output is correct
4 Correct 111 ms 8736 KB Output is correct
5 Correct 172 ms 8608 KB Output is correct
6 Correct 118 ms 8664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 8656 KB Output is correct
2 Correct 147 ms 8608 KB Output is correct
3 Correct 236 ms 8712 KB Output is correct
4 Correct 165 ms 8756 KB Output is correct
5 Correct 207 ms 8716 KB Output is correct
6 Correct 227 ms 8608 KB Output is correct
7 Correct 172 ms 8680 KB Output is correct