# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1040112 |
2024-07-31T16:27:08 Z |
c2zi6 |
Towns (IOI15_towns) |
C++14 |
|
10 ms |
860 KB |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "towns.h"
int maxind(VL& a) {
int ret;
ll mx = -1e18;
rep(i, a.size()) if (a[i] > mx) {
mx = a[i];
ret = i;
}
return ret;
}
map<PII, int> distances;
int getdist(int u, int v) {
if (u == v) return 0;
if (u > v) swap(u, v);
if (!distances.count({u, v})) return distances[{u, v}] = getDistance(u, v);
return distances[{u, v}];
}
int hubDistance(int N, int SUBTASK) {
int n = N;
/*rep(u, n) rep(v, n) getdist(u, v);*/
VL dist0, distd1, distd2, distl, distr, disth;
dist0 = distd1 = distd2 = distl = distr = disth = VL(n);
rep(u, n) dist0[u] = getdist(0, u);
int d1 = maxind(dist0);
rep(u, n) distd1[u] = getdist(d1, u);
int d2 = maxind(distd1);
rep(u, n) distd2[u] = getdist(d2, u);
ll R = 1e18;
rep(u, n) {
disth[u] = (distd1[u] + distd2[u] - distd1[d2])/2;
distl[u] = distd1[u]-disth[u];
distr[u] = distd2[u]-disth[u];
setmin(R, max(distl[u], distr[u]));
}
VI anc(n);
/* 0, on the left
* 1, first hub
* 2, second hub
* 3, on the right
*/
rep(u, n) {
if (max(distl[u], distr[u]) == R) {
if (distl[u] <= distr[u]) anc[u] = 1;
else anc[u] = 2;
} else {
if (distl[u] <= distr[u]) anc[u] = 0;
else anc[u] = 3;
}
}
return R;
int cnt[4]{};
for (int x : anc) cnt[x]++;
if (SUBTASK == 4) {
if (cnt[0] <= N/2 && cnt[1] <= N/2 && cnt[2] + cnt[3] <= N/2) return +R;
if (cnt[0] + cnt[1] <= N/2 && cnt[2] <= N/2 && cnt[3] <= N/2) return +R;
return -R;
} else if (SUBTASK == 3) {
auto check = [&](int u, int v) {
return disth[u] + disth[v] != getdist(u, v);
};
VI a;
replr(HUBIND, 1, 2) {
a = VI();
rep(i, n) if (anc[i] == HUBIND) a.pb(i);
if (a.size() == 0) continue;
VI subtrees;
subtrees.pb(cnt[0]);
subtrees.pb(cnt[3]);
VI group(n);
int g = 1;
for (int i : a) if (group[i] == 0) {
group[i] = g;
subtrees.pb(1);
for (int j : a) if (group[j] == 0) {
if (check(i, j)) {
group[j] = g;
subtrees.back()++;
}
}
g++;
}
bool good = true;
for (int x : subtrees) {
if (x > N/2) good = false;
}
if (good) {
return +R;
}
}
return -R;
}
return -R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:86:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
86 | return R;
| ^
towns.cpp:90:78: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
90 | if (cnt[0] <= N/2 && cnt[1] <= N/2 && cnt[2] + cnt[3] <= N/2) return +R;
| ^~
towns.cpp:91:78: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
91 | if (cnt[0] + cnt[1] <= N/2 && cnt[2] <= N/2 && cnt[3] <= N/2) return +R;
| ^~
towns.cpp:92:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
92 | return -R;
| ^~
towns.cpp:126:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
126 | return +R;
| ^~
towns.cpp:129:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
129 | return -R;
| ^~
towns.cpp:131:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
131 | return -R;
| ^~
towns.cpp: In function 'int maxind(VL&)':
towns.cpp:41:12: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | return ret;
| ^~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:61:34: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | rep(u, n) distd2[u] = getdist(d2, u);
| ~~~~~~~^~~~~~~
towns.cpp:59:34: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | rep(u, n) distd1[u] = getdist(d1, u);
| ~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |