# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
115639 |
2019-06-08T12:10:08 Z |
E869120 |
Towns (IOI15_towns) |
C++14 |
|
33 ms |
1024 KB |
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int N, dist[183][179];
int getdist(int u, int v) {
if (u == v) return 0;
if (dist[u][v] >= 1) return dist[u][v] - 1;
int R = getDistance(u, v);
dist[u][v] = R + 1; dist[v][u] = R + 1;
return R;
}
pair<int, int> Furthest(int pos) {
int maxn = -1, maxid = -1;
for (int i = 0; i < N; i++) {
int D = getdist(pos, i);
if (D > maxn) { maxn = D; maxid = i; }
}
return make_pair(maxn, maxid);
}
bool isdifferent(int root, int dst, int u1, int u2) {
int V1 = getdist(root, u1); V1 -= dst;
int V2 = getdist(root, u2); V2 -= dst;
int V3 = getdist(u1, u2);
if (V1 + V2 == V3) return true;
return false;
}
vector<int> G[119]; bool used[119]; int cntv = 0;
void dfs(int pos) {
if (used[pos] == true) return;
used[pos] = true; cntv++;
for (int i = 0; i < (int)G[pos].size(); i++) dfs(G[pos][i]);
}
int exact_size(int root, int dst, vector<int> vec) {
for (int i = 0; i < 119; i++) { G[i].clear(); used[i] = false; }
cntv = 0;
for (int i = 0; i < (int)vec.size(); i++) {
for (int j = i + 1; j < (int)vec.size(); j++) {
bool I = isdifferent(root, dst, vec[i], vec[j]);
if (I == false) { G[i].push_back(j); G[j].push_back(i); }
}
}
int maxn = 0;
for (int i = 0; i < (int)vec.size(); i++) {
if (used[i] == true) continue;
cntv = 0;
dfs(i);
maxn = max(maxn, cntv);
}
return maxn;
}
int hubDistance(int NN, int sub) {
for (int i = 0; i < NN * NN; i++) dist[i / NN][i % NN] = 0;
N = NN;
pair<int, int> E1 = Furthest(0);
pair<int, int> E2 = Furthest(E1.second);
int diameter = E2.first, u1 = E1.second, u2 = E2.second, minx = (1 << 30);
for (int i = 0; i < N; i++) {
minx = min(minx, abs(getdist(u1, i) - getdist(u2, i)));
}
vector<int> t[4];
for (int i = 0; i < N; i++) {
int V1 = getdist(u1, i);
int V2 = getdist(u2, i);
if (V1 < V2) {
if (abs(V1 - V2) == minx) t[1].push_back(i);
else t[0].push_back(i);
}
if (V1 >= V2) {
if (abs(V1 - V2) == minx) t[2].push_back(i);
else t[3].push_back(i);
}
}
int E[4] = {(int)t[0].size(), (int)t[1].size(), (int)t[2].size(), (int)t[3].size()};
if (t[1].size() > N / 2) { int s = (getdist(u1, u2) + getdist(u1, t[1][0]) - getdist(u2, t[1][0])) / 2; E[1] = exact_size(u1, s, t[1]); }
if (t[2].size() > N / 2) { int s = (getdist(u1, u2) + getdist(u1, t[2][0]) - getdist(u2, t[2][0])) / 2; E[2] = exact_size(u1, s, t[2]); }
bool flag = false;
if (E[1] >= 1 && max({E[0], E[1], (int)t[2].size() + E[3]}) <= N / 2) flag = true;
if (E[2] >= 1 && max({E[0] + (int)t[1].size(), E[2], E[3]}) <= N / 2) flag = true;
if (flag == true) return (diameter + minx) / 2;
return -1 * ((diameter + minx) / 2);
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (t[1].size() > N / 2) { int s = (getdist(u1, u2) + getdist(u1, t[1][0]) - getdist(u2, t[1][0])) / 2; E[1] = exact_size(u1, s, t[1]); }
~~~~~~~~~~~~^~~~~~~
towns.cpp:89:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (t[2].size() > N / 2) { int s = (getdist(u1, u2) + getdist(u1, t[2][0]) - getdist(u2, t[2][0])) / 2; E[2] = exact_size(u1, s, t[2]); }
~~~~~~~~~~~~^~~~~~~
towns.cpp:62:29: warning: unused parameter 'sub' [-Wunused-parameter]
int hubDistance(int NN, int sub) {
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
512 KB |
Output is correct |
2 |
Correct |
33 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
20 ms |
640 KB |
Output is correct |
5 |
Correct |
20 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
640 KB |
Output is correct |
2 |
Correct |
21 ms |
1024 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
20 ms |
1020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |