# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
897322 | alexander707070 | Flights (JOI22_flights) | C++17 | 354 ms | 5496 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "Ali.h"
#define MAXN 20007
using namespace std;
namespace{
int n,xx,yy,k,root;
const int bucket=7;
vector<int> v[MAXN];
int dist[MAXN],where[MAXN],num[MAXN],cnt[MAXN];
int l,r,mins,currdist;
vector<int> code[MAXN],nodes[MAXN];
void reset(){
k=0; mins=n+1;
for(int i=0;i<n;i++){
v[i].clear(); code[i].clear();
nodes[i].clear();
cnt[i]=0;
}
}
vector<int> dfs(int x,int p){
vector<int> comp={x},curr;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
curr=dfs(v[x][i],x);
for(int f:curr)comp.push_back(f);
}
if(comp.size()>=bucket or p==-1){
for(int f:comp)where[f]=k;
nodes[k]=comp;
k++; comp.clear();
}
return comp;
}
void dfs2(int x,int p){
num[x]=where[x]*(2*bucket-1)+cnt[where[x]];
SetID(x,num[x]); cnt[where[x]]++;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
if(where[x]==where[v[x][i]]){
code[where[x]].push_back(0);
}
dfs2(v[x][i],x);
}
if(p!=-1 and where[x]==where[p]){
code[where[x]].push_back(1);
}
}
void dfs3(int x,int p,int d){
dist[x]=d;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
dfs3(v[x][i],x,d+1);
}
}
}
vector<int> perm;
void Init(int N, vector<int> U, vector<int> V){
srand(10);
n=N;
reset();
perm={};
for(int i=0;i<n;i++)perm.push_back(i);
for(int i=0;i<n-1;i++){
v[U[i]].push_back(V[i]);
v[V[i]].push_back(U[i]);
}
random_shuffle(perm.begin(),perm.end());
for(int i:perm){
if(v[i].size()<3)root=i;
}
dfs(root,-1);
dfs2(root,-1);
}
string SendA(string S){
xx=yy=0;
for(int i=0;i<20;i++){
xx*=2;
if(S[i]=='1')xx++;
}
int z=0;
for(int i=0;i<=1430;i++){
for(int f=i;f<=1430;f++){
if(z==xx){
xx=i; yy=f;
i=f=2000;
}
z++;
}
}
for(int i:nodes[xx]){
dfs3(i,-1,0);
currdist=n;
for(int f:nodes[yy]){
currdist=min(currdist,dist[f]);
}
if(currdist<mins){
mins=currdist;
l=i;
for(int f:nodes[yy]){
if(dist[f]==mins)r=f;
}
}
}
l=num[l]%(2*bucket-1); r=num[r]%(2*bucket-1);
string res="";
for(int i=0;i<26;i++){
if(i<code[xx].size())res.push_back(code[xx][i]+'0');
else res+="1";
}
for(int i=3;i>=0;i--){
if(((1<<i)&l)==0)res.push_back('0');
else res.push_back('1');
}
for(int i=0;i<26;i++){
if(i<code[yy].size())res.push_back(code[yy][i]+'0');
else res.push_back('1');
}
for(int i=3;i>=0;i--){
if(((1<<i)&r)==0)res.push_back('0');
else res.push_back('1');
}
for(int i=13;i>=0;i--){
if(((1<<i)&mins)==0)res.push_back('0');
else res.push_back('1');
}
return res;
}
#include<bits/stdc++.h>
#include "Benjamin.h"
#define MAXN 20007
using namespace std;
namespace{
int nn,from,to,s,pos,dis,ld,rd,pt,nd;
vector<int> codel,coder;
vector<int> treel[40],treer[40];
int dst[40],as,bs;
const int del=13;
void resets(){
codel.clear(); coder.clear();
for(int i=0;i<40;i++){
treel[i].clear();
treer[i].clear();
dst[i]=0;
}
}
void dfs4(int x){
while(pt<codel.size()){
if(codel[pt]==0){
nd++; pt++;
treel[x].push_back(nd);
treel[nd].push_back(x);
dfs4(nd);
}else{
pt++;
return;
}
}
}
void dfs5(int x){
while(pt<coder.size()){
if(coder[pt]==0){
nd++; pt++;
treer[x].push_back(nd);
treer[nd].push_back(x);
dfs5(nd);
}else{
pt++;
return;
}
}
}
void dfs6(int x,int p,int d){
dst[x]=d;
for(int i=0;i<treel[x].size();i++){
if(treel[x][i]==p)continue;
dfs6(treel[x][i],x,d+1);
}
}
void dfs7(int x,int p,int d){
dst[x]=d;
for(int i=0;i<treer[x].size();i++){
if(treer[x][i]==p)continue;
dfs7(treer[x][i],x,d+1);
}
}
}
string SendB(int N, int X, int Y){
resets();
nn=N; from=X; to=Y;
if(from>to)swap(from,to);
int z=0;
for(int i=0;i<=1430;i++){
for(int f=i;f<=1430;f++){
if(i==from/del and to/del==f){
i=f=2000;
break;
}
z++;
}
}
string res="";
for(int i=19;i>=0;i--){
if((z&(1<<i))==0)res.push_back('0');
else res.push_back('1');
}
return res;
}
int Answer(string T){
pos=ld=rd=0;
for(int i=pos;i<pos+26;i++){
codel.push_back(T[i]-'0');
}
pos+=26;
for(int i=pos;i<pos+4;i++){
ld*=2;
if(T[i]=='1')ld++;
}
pos+=4;
nd=pt=0;
dfs4(0);
if(from/del==to/del){
dfs6(to%del,-1,0);
return dst[from%del];
}
dfs6(ld,-1,0);
as=dst[from%del];
for(int i=pos;i<pos+26;i++){
coder.push_back(T[i]-'0');
}
pos+=26;
for(int i=pos;i<pos+4;i++){
rd*=2;
if(T[i]=='1')rd++;
}
pos+=4;
nd=pt=0;
dfs5(0);
dfs7(rd,-1,0);
bs=dst[to%del];
dis=0;
for(int i=pos;i<pos+14;i++){
dis*=2;
if(T[i]=='1')dis++;
}
return as+dis+bs;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |