이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 120;
/*
int ds[MAXN][MAXN];
int getDistance(int a, int b) {
return ds[a][b];
}
*/
int dist[MAXN][MAXN];
int gd(int a, int b) {
if(dist[a][b] == 0) {
dist[a][b] = getDistance(a, b);
dist[b][a] = dist[a][b];
}
return dist[a][b];
}
int pai[MAXN], sz[MAXN];
int find(int v) {
if(v == pai[v]) return v;
return pai[v] = find(pai[v]);
}
void une(int a, int b) {
a = find(a); b = find(b);
if(a == b) return;
if(sz[a] > sz[b]) swap(a, b);
pai[a] = b;
sz[b] += sz[a];
}
int hubDistance(int n, int sub) {
//int resp = 0;
for(int i = 0; i < MAXN; i++) {
for(int j = 0; j < MAXN; j++) {
dist[i][j] = 0;
}
}
int mx1 = 1, mx = 0;
int a, b;
for(int i = 1; i < n; i++) {
int d = gd(1, i);
if(d > mx) {
mx = d; mx1 = i;
}
}
a = mx1; mx = 0;
for(int i = 0; i < n; i++) {
if(i == a) continue;
int d = gd(a, i);
if(d > mx) {
mx = d; mx1 = i;
}
}
b = mx1;
int dim = mx;
//set <int> di;
int resp = dim;
vector <int> ns;
for(int i = 0; i < n; i++) {
if(i == a || i == b) continue;
int d = gd(a, i) - (gd(a, i) + gd(b, i) - dim) / 2;
resp = min(resp, max(d, dim - d));
ns.push_back(d);
}
resp = resp * -1;
int esq1 = -1, esq2 = -1;
for(int i = 0; i < n; i++) {
int d = gd(a, i) - (gd(a, i) + gd(b, i) - dim)/ 2;
//printf("%d %d %d\n", i, d, dim - d);
if(max(d, dim - d) != -resp) continue;
//printf("oi\n");
if(d == esq1 || d == esq2) continue;
//printf("%d\n", i);
if(esq1 == -1) esq1 = d;
else esq2 = d;
vector <int> cur;
int ce = 0, cd = 0;
for(int j = 0; j < n; j++) {
int dt = gd(a, j) - (gd(a, j) + gd(b, j) - dim) / 2;
if(dt < d) {
ce++;
}
else if(dt > d) {
cd++;
}
else cur.push_back(j);
}
if(ce > n / 2 || cd > n / 2) continue;
//return -resp;
if(cur.size() <= n / 2) return -resp;
else continue;
//printf("%d\n", cur.size());
stack <int> pilha;
for(int j = 0; j < cur.size(); j++) {
int id = cur[j];
if(pilha.empty()) {
pilha.push(id);
}
else {
int k = pilha.top(), l = cur[j];
int d1 = gd(a, k) + gd(b, k) - gd(a, b), d2 = gd(a, l) + gd(b, l) - gd(a, b), db = gd(k, l);
if(d1 + d2 != 2 * db) {
une(l, pilha.top());
pilha.push(l);
}
else pilha.pop();
}
}
if(pilha.empty()) return -resp;
int bs = pilha.top();
int qt = 1;
for(int j = 0; j < cur.size(); j++) {
int id = cur[j];
if(id == bs) continue;
int k = find(bs), l = find(id);
int d1 = gd(a, k) + gd(b, k) - gd(a, b), d2 = gd(a, l) + gd(b, l) - gd(a, b), db = gd(k, l);
if(d1 + d2 != 2 * db) qt++;
}
if(qt <= n / 2) return -resp;
}
return resp;
}
/*
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &ds[i][j]);
}
}
printf("%d\n", hubDistance(n, 0));
return 0;
}
*/
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:92:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | if(cur.size() <= n / 2) return -resp;
| ~~~~~~~~~~~^~~~~~~~
towns.cpp:96:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int j = 0; j < cur.size(); j++) {
| ~~^~~~~~~~~~~~
towns.cpp:114:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int j = 0; j < cur.size(); j++) {
| ~~^~~~~~~~~~~~
towns.cpp:33:28: warning: unused parameter 'sub' [-Wunused-parameter]
33 | 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... |