# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
795506 |
2023-07-27T10:36:52 Z |
Josia |
Towns (IOI15_towns) |
C++17 |
|
60 ms |
1132 KB |
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> queries;
int getd(int a, int b) {
if (a>b) swap(a,b);
if (a==b) return 0;
if (queries.count({a,b})) return queries[{a,b}];
return queries[{a,b}] = getDistance(a, b);
}
struct UF {
map<int, int> chefs;
UF(vector<int> elems) {
for (int i: elems) chefs[i] = i;
}
int find(int x) {
if (chefs[x] == x) return x;
chefs[x] = find(chefs[x]);
return chefs[x];
}
void unite (int a, int b) {
a = find(a);
b = find(b);
chefs[a] = b;
}
};
int hubDistance(int N, int sub) {
queries.clear();
int d1=0, v1 = 0;
for (int i = 1; i<N; i++) {
int d = getd(0, i);
if (d>d1) {
d1 = d;
v1 = i;
}
}
vector<int> dists1(N);
int d2=0, v2=v1;
for (int i = 0; i<N; i++) {
int d = getd(v1, i);
dists1[i] = d;
if (d>d2) {
d2 = d;
v2 = i;
}
}
vector<int> dists2(N);
for (int i = 0; i<N; i++) {
int d = getd(v2, i);
dists2[i] = d;
}
map<int, vector<int>> joinedDiam;
int R = 1e9;
for (int i = 0; i<N; i++) {
int rem = (dists1[i]+dists2[i]-d2)/2;
joinedDiam[dists1[i]-rem].push_back(i);
R = min(R, max(dists1[i]-rem, dists2[i]-rem));
}
vector<int> possHub;
for (int i = 0; i<N; i++) {
int rem = (dists1[i]+dists2[i]-d2)/2;
if (max(dists1[i]-rem, dists2[i]-rem) == R) possHub.push_back(dists1[i]-rem);
}
sort(possHub.begin(), possHub.end());
possHub.erase(unique(possHub.begin(), possHub.end()), possHub.end());
int mult = -1;
for (int hub: possHub) {
int h1=0, h2=0;
bool poss = 1;
for (auto i: joinedDiam) {
if (i.first < hub) h1+=i.second.size();
if (i.first > hub) h2+=i.second.size();
if (i.first == hub) {
if ((int)i.second.size() > N/2) {
if (sub!=3) {
poss=0;
continue;
}
UF u(i.second);
for (int a: i.second) {
for (int b: i.second) {
// cerr << a << " " << b << "\n";
int da = (dists1[a]+dists2[a]-d2)/2;
int db = (dists1[b]+dists2[b]-d2)/2;
// cerr << a << " " << b << "\n";
int d = getd(a,b);
if (d < da+db) u.unite(a,b);
}
}
map<int, int> count;
for (auto j: u.chefs) count[j.second]++;
int Max = 0;
for (auto j: count) Max = max(Max, j.second);
if (Max > N/2) poss = 0;
}
}
}
if (h1 > N/2 || h2 > N/2) poss = 0;
if (poss) mult = 1;
}
return R*mult;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:103:41: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
103 | if (i.first < hub) h1+=i.second.size();
| ^
towns.cpp:104:41: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
104 | if (i.first > hub) h2+=i.second.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
340 KB |
Output is correct |
2 |
Correct |
12 ms |
376 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
13 ms |
360 KB |
Output is correct |
5 |
Correct |
13 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
368 KB |
Output is correct |
2 |
Correct |
9 ms |
340 KB |
Output is correct |
3 |
Correct |
16 ms |
340 KB |
Output is correct |
4 |
Correct |
13 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
596 KB |
Output is correct |
2 |
Correct |
60 ms |
1132 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
60 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |