이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include<bits/stdc++.h>
using namespace std;
vector<int> dist0;
int a=-1;
vector<int> dista;
int b=-1;
vector<int> distb;
int Distance(int i, int j){
if(j<i) swap(i, j);
if(i==0) return dist0[j];
if(i==a) return dista[j];
if(i==b) return distb[j];
if(j==a) return dista[i];
if(j==b) return distb[i];
return getDistance(i, j);
}
bool balancedSlow(int n, int r, int diameter, map<int, vector<int> >& group){
bool balanced_hub=false;
int cntleft=1;
for(auto [x, g]: group){
if(max(x, diameter-x)!=r){ //no hub
continue;
}
int cntright = n - cntleft - (int)g.size();
if(cntleft>n/2 || cntright>n/2) continue;
vector<bool> vis(n, false);
int open = (int)g.size();
bool balanced=true;
while(open > n/2){
int v=g.back(); g.pop_back();
if(vis[v]){
continue;
}
vis[v]=true;
int size=1;
for(auto u: g){
int d = Distance(v, u);
if(d < dista[v]-x + dista[u]-x){ //same component
vis[u]=true;
size++;
}
}
if(size > n/2){
balanced = false;
break;
}
open -= size;
}
if(balanced){
balanced_hub = true;
break;
}
cntleft += (int)group.size();
}
return balanced_hub;
}
int hubDistance(int n, int sub) {
//find diameter
dist0.assign(n, 0);
a=1;
for(int i=1; i<n; i++){
dist0[i] = getDistance(0, i);
if(dist0[i]>dist0[a]){
a=i;
}
}
b=0;
dista.assign(n, 0);
dista[0] = dist0[a];
for(int i=1; i<n; i++){
if(i==a) continue;
dista[i] = getDistance(a, i);
if(dista[i] > dista[b]){
b=i;
}
}
distb.assign(n, 0);
distb[0] = dist0[b];
distb[a] = dista[b];
for(int i=1; i<n; i++){
if(i==a || i==b) continue;
distb[i] = getDistance(b, i);
}
int diameter = dista[b];
//group the leafes
vector<int> dleft(n); dleft[a]=0; dleft[b]=diameter;
vector<int> ddiam(n); ddiam[a]=ddiam[b]=0;
map<int, vector<int> > group;
for(int i=0; i<n; i++){
if(i==a || i==b) continue;
ddiam[i] = (dista[i]+distb[i] - diameter)/2;
dleft[i] = dista[i]-ddiam[i];
group[dleft[i]].push_back(i);
}
//compute r
int r=diameter;
bool balanced = false;
int left=1;
for(auto [x, g]: group){
r = min(r, max(x, diameter-x));
int gsize=g.size();
int right=n-left-gsize;
if(left<=n/2 && right<=n/2 && g.size()<=n/2){
balanced=true;
}
left += gsize;
}
if(balanced){
return r;
}
return -r;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:143:25: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
143 | int gsize=g.size();
| ~~~~~~^~
towns.cpp:146:47: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
146 | if(left<=n/2 && right<=n/2 && g.size()<=n/2){
| ~~~~~~~~^~~~~
towns.cpp:79:28: warning: unused parameter 'sub' [-Wunused-parameter]
79 | int hubDistance(int n, int sub) {
| ~~~~^~~
# | 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... |