# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
74232 |
2018-08-30T13:50:15 Z |
funcsr |
Towns (IOI15_towns) |
C++17 |
|
1000 ms |
748 KB |
#include "towns.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <random>
#define rep(i, N) for (int i=0; i<(N); i++)
#define pb push_back
#define _1 first
#define _2 second
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define INF 1145141919
using namespace std;
typedef pair<int, int> P;
struct UF {
int U[111];
int R[111];
bool seen[111];
UF(int N) {
rep(i, N) U[i] = i, R[i] = 1,seen[i] = false;
}
int find(int x) {
if (U[x]==x)return x;
return U[x]=find(U[x]);
}
void unite(int x, int y) {
x=find(x), y=find(y);
if(x==y)return;
U[y]=x;
R[x]+=R[y];
R[y]=0;
seen[x] |=seen[y];
}
};
mt19937 mt_rand(time(NULL));
int N;
int memo[111][111];
int Rank[111];
int Q = 0;
int dist(int a, int b) {
if (memo[a][b] != -1) return memo[a][b];
if (a == b) return 0;
Q++;
return memo[a][b] = memo[b][a] = getDistance(a, b);
}
int saien(int x) {
P mp = P(-1, -1);
rep(t, N) mp = max(mp, P(dist(x, t), t));
return mp._2;
}
bool same(int x, int y) {
if (x==y)return true;
return dist(x,y)-Rank[x]-Rank[y]!=0;
}
int hubDistance(int NN, int sub) {
N=NN;
rep(i, N) rep(j, N) memo[i][j] = -1;
rep(i, N) Rank[i] = -1;
int u = saien(0);
int v = saien(u);
vector<int> pos;
int L = dist(u, v);
pos.pb(0);
pos.pb(L);
map<int, vector<int> > G;
rep(x, N) if (x != u && x != v) {
Rank[x] = (dist(u, x)+dist(v, x)-L)/2;
int p = dist(u, x)-Rank[x];
//cout<<"x="<<x<<"->"<<p<<"\n";
pos.pb(p);
G[p].pb(x);
}
sort(all(pos)); uniq(pos);
//for (int x : pos)cout<<x<<",";cout<<"\n";
int R = INF;
for (int x : pos) R = min(R, max(x, L-x));
vector<int> gs;
for (int x : pos) if (R == max(x, L-x)) gs.pb(x);
assert(gs.size()==1||gs.size()==2);
bool ok = false;
random_shuffle(all(gs));
for (int g : gs) {
vector<int> list = G[g];
int left = 1;
for (int x : pos) if (x<g) left += G[x].size();
int right = 1;
for (int x : pos) if (x>g) right += G[x].size();
int maxsize = max(left, right);
assert(left+right+list.size()==N);
if (maxsize <= N/2 && list.size() > N/2) {
if (sub == 4) {
maxsize = max(maxsize, (int)list.size());
}
else if (sub >= 3) {
int Qlimit = INF;
if (sub == 5) Qlimit = 5*N;
else if (sub == 6) Qlimit = (7*N+1)/2;
UF uf(N);
while (true) {
vector<int> cand;
for (int i : list)if (i == uf.find(i) && !uf.seen[i]) cand.pb(i);
if (cand.empty()) continue;
int x = cand[mt_rand()%cand.size()];
int ctr = 0;
int rest = list.size();
for (int y : list) {
if (ctr > N/2) break;
if (ctr+rest <= N/2) break;
if (uf.seen[uf.find(x)]) break;
rest--;
if (uf.find(x) == uf.find(y)) {
ctr++;
continue;
}
//if (uf.seen[uf.find(y)]) continue;
if (Q<Qlimit && same(x, y)) {
ctr++;
uf.unite(x, y);
}
}
uf.seen[uf.find(x)]=true;
maxsize = max(maxsize, ctr);
if (maxsize > N/2) break;
}
}
}
//cout<<"R="<<R<<", size="<<maxsize<<"\n";
if (maxsize <= N/2) {
ok = true;
break;
}
}
if (!ok) R *= -1;
return R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:91:50: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
for (int x : pos) if (x<g) left += G[x].size();
^
towns.cpp:93:51: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
for (int x : pos) if (x>g) right += G[x].size();
^
In file included from /usr/include/c++/7/cassert:44:0,
from towns.cpp:6:
towns.cpp:95:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(left+right+list.size()==N);
~~~~~~~~~~~~~~~~~~~~~~^~~
towns.cpp:97:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (maxsize <= N/2 && list.size() > N/2) {
~~~~~~~~~~~~^~~~~
towns.cpp:113:31: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
int rest = list.size();
~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
376 KB |
Output is correct |
2 |
Correct |
17 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
21 ms |
544 KB |
Output is correct |
5 |
Correct |
23 ms |
584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
584 KB |
Output is correct |
2 |
Correct |
17 ms |
644 KB |
Output is correct |
3 |
Correct |
22 ms |
676 KB |
Output is correct |
4 |
Correct |
21 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1078 ms |
736 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1086 ms |
748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |