이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include<vector>
#include<algorithm>
#include<utility>
#include<map>
#include<iostream>
using namespace std;
namespace{
struct DSU{
vector<int> dsu, sz;
DSU(int n){
dsu.resize(n + 1);
sz.resize(n + 1);
for(int i = 1; i <= n; i++) dsu[i] = i, sz[i] = 1;
}
int query(int x){
if(x == dsu[x]) return x;
dsu[x] = query(dsu[x]);
return dsu[x];
}
void unite(int a, int b){
a = query(a);
b = query(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a, b);
dsu[b] = a;
sz[a] += sz[b];
}
};
}
int hubDistance(int N, int sub) {
int a = -1, b = -1;
int maxi = 0, n = N;
vector<vector<int>> dist(n, vector<int>(n));
if(sub == 3){
for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++) {dist[i][j] = getDistance(i, j); dist[j][i] = dist[i][j];}
for(int i = 1; i < n; i++){
int len = dist[0][i];
if(len > maxi){
maxi = len;
a = i;
}
}
vector<int> dista(n), distb(n);
maxi = 0;
for(int i = 0; i < n; i++){
dista[i] = dist[a][i];
if(dista[i] > maxi){
maxi = dista[i];
b = i;
}
}
for(int i = 0; i < n; i++){
distb[i] = dist[b][i];
}
int d = dista[b];
int ans = 1e9;
vector<int> dda(n), ddb(n);
for(int i = 0; i < n; i++){
int da = (d + dista[i] + distb[i]) / 2 - distb[i], db = (d + dista[i] + distb[i]) / 2 - dista[i];
ans = min(ans, max(da, db));
dda[i] = dista[i] - da;
ddb[i] = distb[i] - db;
}
map<int, vector<int>> m;
for(int i = 0; i < n; i++){
m[(d + distb[i] + dista[i]) / 2 - distb[i]].push_back(i);
}
int sum = 0, maxsz = 0;
bool flag = 0;
for(auto &[len, vec]: m){
if(max(len, d - len) == ans){
int sz = vec.size();
DSU dsu(sz);
for(int i = 0; i < sz; i++){
for(int j = i + 1; j < sz; j++){
if(dda[vec[i]] + dda[vec[j]] > dist[vec[i]][vec[j]]){
dsu.unite(i, j);
}
}
}
maxsz = 0;
for(int i = 0; i < sz; i++){
maxsz = max(maxsz, dsu.sz[i]);
}
if(max(maxsz, max(sum, n - sz - sum)) <= n / 2) flag = 1;
}
sum += (int)vec.size();
}
if(flag) return ans;
else return -ans;
}
for(int i = 1; i < n; i++){
int len = getDistance(0, i);
if(len > maxi){
maxi = len;
a = i;
}
}
vector<int> dista(n), distb(n);
maxi = 0;
for(int i = 0; i < n; i++){
dista[i] = getDistance(a, i);
if(dista[i] > maxi){
maxi = dista[i];
b = i;
}
}
for(int i = 0; i < n; i++){
distb[i] = getDistance(b, i);
}
int d = dista[b];
int ans = 1e9;
for(int i = 0; i < n; i++){
int da = (d + dista[i] + distb[i]) / 2 - distb[i], db = (d + dista[i] + distb[i]) / 2 - dista[i];
ans = min(ans, max(da, db));
}
if(sub == 4){
map<int, int> m;
for(int i = 0; i < n; i++){
m[(d + distb[i] + dista[i]) / 2 - distb[i]]++;
}
int sum = 0;
bool flag = 0;
for(auto &[len, val]: m){
if(max(len, d - len) == ans){
if(max(val, max(sum, n - sum - val)) <= n / 2) flag = 1;
}
sum += val;
}
if(flag) return ans;
else return -ans;
}
return ans;
}
// g++ -std=c++17 -Wall -Wextra -fsanitize=undefined -fsanitize=address -Wshadow -o run towns.cpp grader.cpp
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:73:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
73 | int sz = vec.size();
| ~~~~~~~~^~
# | 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... |