이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int shift = 8;
vector<int> labels;
vector<vector<int>> graph;
vector<int> sz;
void dfs1(int n,int p){//calc sizes
sz[n] = 1;
for(int adj : graph[n])
{
if(adj == p) continue;
dfs1(adj, n);
sz[n] += sz[adj];
}
}
vector<bool> vis;
int getcentroid(int n){
dfs1(n, -1);
vis[n] = true;
for(int adj : graph[n]){
if(!vis[adj] and sz[adj] >= sz.size()/2){
//cout << adj<<": "<<sz[adj]<<endl;
return getcentroid(adj);
}
}
return n;
}
int counter;
void name_subtree(int n, int p){
labels[n] =sz[n] + (counter << shift) ;
if(p == -1) labels[n] = 0;
counter++;
for(int adj : graph[n]){
if(adj == p)
continue;
name_subtree(adj, n);
}
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
counter = 0;
sz.clear();
labels.clear();
graph.clear();
vis.clear();
sz.assign(n, 0);
vis.assign(n, 0);
labels.assign(n, 0);
graph.assign( n, vector<int>());
for (int i = 0; i < n - 1; i++) {
graph[u[i]].pb(v[i]);
graph[v[i]].pb(u[i]);
}
//dfs1(0,-1);
int root = getcentroid(0);
//for(int i =0; i<n; i++) cout <<i<<": "<< sz[i]<<endl;
name_subtree(root, -1);
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
int startind = ( s>> shift);
int targetind = (t>>shift);
for(int adj : c){
int adjind = (adj >> shift);
if(adjind < startind) continue;
//cout << s <<"->"<<t<<": "<<adj<<", "<<sz[adj]<<endl;
if(adjind<= targetind and targetind <= adjind + adj - (adjind<<shift) -1){
//cout << startind<<"->"<<targetind<<": "<<adjind<<", "<<adj - (adjind<<10) -1 <<endl;
return adj;
}
}
return c[0];
}
/************/
컴파일 시 표준 에러 (stderr) 메시지
stations.cpp: In function 'int getcentroid(int)':
stations.cpp:29:28: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(!vis[adj] and sz[adj] >= sz.size()/2){
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |