# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
645533 | TimDee | Speedrun (RMI21_speedrun) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Input format:
*
* N -- number of nodes
* a1 b1 -- edge 1
* ...
* a(N-1) b(N-1) -- edge N - 1
* x -- start node
*/
#include <iostream>
#include <map>
#include <set>
//#include "speedrun.h"
using namespace std;
static map<int, map<int, bool>> mp;
static int length = -1;
static int queries = 0;
static bool length_set = false;
static int current_node = 0;
static set<int> viz;
static map<int, set<int>> neighbours;
void setHintLen(int l) {
if (length_set) {
cerr << "Cannot call setHintLen twice" << endl;
exit(0);
}
length = l;
length_set = true;
}
void setHint(int i, int j, bool b) {
if (!length_set) {
cerr << "Must call setHintLen before setHint" << endl;
exit(0);
}
mp[i][j] = b;
}
int getLength() { return length; }
bool getHint(int j) { return mp[current_node][j]; }
bool goTo(int x) {
//cout<<"now "<<x<<'\n';
if (neighbours[current_node].find(x) == end(neighbours[current_node])) {
++queries;
return false;
} else {
viz.insert(current_node = x);
return true;
}
}
//#include "speedrun.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> p(1e3+1);
vector<int> r(1e3+1,0);
int get(int a) {
return (a==p[a])?a:get(a);
}
void uni(int a, int b) {
a=get(a), b=get(b);
if (a==b) return;
if (r[a]==r[b]) ++r[a];
if (r[a]<r[b]) swap(a,b);
p[b]=a;
}
void assignHints(int id,int n, int a[], int b[]) {
if (id==1) {
setHintLen(n);
for(int i=1;i<=n-1;++i) {
int u=a[i],v=b[i];
setHint(u,v,1);
setHint(v,u,1);
}
}
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))));
}
}
}
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))));
}
}
}
if (id==4) {
setHintLen(316);
vector<vector<int>> adj(n+1);
for (int i=1; i<n; ++i) {
adj[a[i]].push_back(b[i]);
adj[b[i]].push_back(a[i]);
}
vector<pair<int,int>> toadd;
for (int i=1; i<=n; ++i) {
int s=adj[i].size();
for (int j=10; j; --j) setHint(i,j,!!(s&(1<<(j-1))));
int pt=10;
for (auto x:adj[i]) {
if (pt<306) {
for (int j=10; j; --j) setHint(i,j+pt,!!(x&(1<<(j-1))));
pt+=10;
} else {
toadd.push_back({i,x});
}
}
for (; pt<296; pt+=20) {
if (toadd.empty()) break;
auto p=toadd.back(); toadd.pop_back();
for (int j=10; j; --j) {
setHint(i,j+pt,!!(p.first&(1<<(j-1))));
setHint(i,j+pt+10,!!(p.second&(1<<(j-1))));
}
}
}
}
}
int n;
vector<vector<int>> adj(1e3+1,vector<int>(1e3+1,0));
vector<int> vis(1e3+1,0);
void dfs(int u, int p) {
for (int j=1; j<=n; ++j) {
adj[u][j]=getHint(j);
}
for (int i=1; i<=n; ++i) {
if (i==p) continue;
if (adj[u][i]) {
goTo(i);
dfs(i,u);
}
}
if (p==-1) return;
goTo(p);
}
void dfs2(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);
}
adj[u][l]=1, adj[u][r]=1;
for (int i=1; i<=n; ++i) {
if (i==p) continue;
if (adj[u][i]) {
goTo(i);
dfs2(i,u);
}
}
if (p==-1) return;
goTo(p);
}
void scuza(int u, int p) {
vis[u]=1;
int sz=0;
for (int j=10; j>0; --j) {
sz+=getHint(j)<<(j-1);
}
int pt=10;
for (;pt<=min(sz*10,306);pt+=10) {
int x=0;
for (int j=10; j; --j) x+=getHint(j+pt)<<(j-1);
adj[u][x]=1;
adj[x][u]=1;
}
for (int j=1; j<=n; ++j) {
if (j==p) continue;
if (vis[j]) continue;
if (adj[u][j]) {
goTo(j);
scuza(j,u);
}
}
if (p==-1) return;
if (sz<=30) {
goTo(p);
return;
}
for (int j=1; j<=n; ++j) {
if (j==p) continue;
if (vis[j]) continue;
if (adj[u][j]) {
goTo(j);
scuza(j,u);
}
}
for (int j=1; j<=n; ++j) {
if (j==p) continue;
if (vis[j]) continue;
int x=goTo(j);
if (x) scuza(j,u);
}
}
int center=0;
void speedrun(int id,int N,int start) {
for (int i=1; i<=1e3; ++i) p[i]=i;
n=N;
if (id==1) {
dfs(start,-1);
}
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);
}
}
if (id==3) {
dfs2(start,-1);
}
if (id==4) {
scuza(start,-1);
}
}
int main() {
int N;
cin >> N;
int a[N],b[N];
for (int i = 1; i < N; ++i) {
cin >> a[i] >> b[i];
neighbours[a[i]].insert(b[i]);
neighbours[b[i]].insert(a[i]);
}
assignHints(4, N, a, b);
if (!length_set) {
cerr << "Must call setHintLen at least once" << endl;
exit(0);
}
cin >> current_node;
viz.insert(current_node);
speedrun(4, N, current_node);
if (viz.size() < N) {
cerr << "Haven't seen all nodes" << endl;
exit(0);
}
cerr << "OK; " << queries << " incorrect goto's" << endl;
return 0;
}