이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int hubDistance(int N, int sub) {
map<int,int> d[N];
auto get = [&](int a,int b){
if(a > b)
swap(a,b);
if(d[a].count(b))
return d[a][b];
if(a == b)
return 0;
return d[a][b] = getDistance(a,b);
};
int v = 0;
for(int i = 0;i<N;i++){
if(get(0,i) > get(0,v)){
v = i;
}
}
int x = 0;
for(int i = 0;i<N;i++){
if(get(v,i) > get(v,x)){
x = i;
}
}
int len = get(v,x);
// cout << v << ' ' << x << ' ' << len << endl;
map<int,vector<int>> mp;
for(int i = 0;i<N;i++){
int val = (get(0,i) + get(0,v) - get(v,i))/2;
mp[val].push_back(i);
}
int val1 = 0;
int val2 = 2e9;
int ans;
for(auto u:mp){
int tmp = get(0,v) - u.first;
// cout << tmp << endl;
if(tmp <= len/2){
val1 = max(val1,tmp);
}
if((len + 1)/2 <= tmp){
val2 = min(val2,tmp);
}
}
// cout << val1 << ' ' << val2 << endl;
ans = len - val1;
if(val2 != len - val1){
if(val2 < len - val1){
ans = val2;
val1 = -1;
}
else val2 = -1;
}
int cnt = 0;
for(auto u:mp){
int tmp = get(0,v) - u.first;
if(tmp == val1){
if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
val1 = -1;
}
}
if(tmp == val2){
if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
val2 = -1;
}
}
cnt += u.second.size();
}
if(val1 == -1 && val2 == -1)
return -ans;
if(val1 == -1)
val1 = val2;
vector<int> a = mp[-(val1 - get(0,v))];
vector<int> v1,v2;
for(auto u:a){
if(v1.empty() || get(u,v1.back()) == get(v,u) + get(v,v1.back()) - 2 * val1){
v1.push_back(u);
if(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
}
else v2.push_back(u);
}
if(v2.empty())
return -ans;
cnt = 0;
for(auto u:a){
if(get(u,v2[0]) != get(v,u) + get(v,v2[0]) - 2 * val1){
cnt++;
}
}
// return ans;
if(cnt <= N/2)
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:60:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:65:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
65 | if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:69:24: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
69 | cnt += u.second.size();
| ^
towns.cpp:4:28: warning: unused parameter 'sub' [-Wunused-parameter]
4 | int hubDistance(int N, int sub) {
| ~~~~^~~
towns.cpp:98:1: warning: control reaches end of non-void function [-Wreturn-type]
98 | }
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |