#include "towns.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#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];
UF(int N) {
rep(i, N) U[i] = i, R[i] = 1;
}
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;
}
};
int N;
int memo[111][111];
int Rank[111];
int dist(int a, int b) {
if (memo[a][b] != -1) return memo[a][b];
if (a == b) return 0;
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;
}
/*
P solve(vector<int> list) {
if (list.size() == 1) return P(1, list[0]);
vector<int> l, r;
rep(i, list.size()) if (i%2==0)l.pb(list[i]); else r.pb(list[i]);
P a = solve(l);
P b = solve(r);
if (same(a._2, b._2)) {
cout<<"solve({";for(int x:list)cout<<x<<",";cout<<"}) = "<<a._1+b._1<<" (repr="<<a._2<<")\n";
}
else {
cout<<"solve({";for(int x:list)cout<<x<<",";cout<<"}) = "<<max(a,b)._1<<" (repr="<<max(a,b)._2<<")\n";
}
if (same(a._2, b._2)) return P(a._1+b._1, a._2);
else return max(a, b);
}
*/
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);
P mp = P(INF, -1);
//cout<<"L="<<L<<"\n";
//for (int x : pos)cout<<x<<",";cout<<"\n";
for (int x : pos) mp = min(mp, P(max(x, L-x), x));
int R = mp._1, g = mp._2;
vector<int> list = G[g];
//for (int x : list)cout<<x<<",";cout<<"\n";
// (size, repr)
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 = 0;
maxsize = max(maxsize, left);
maxsize = max(maxsize, right);
//cout<<"Left="<<left<<", right="<<right<<"\n";
assert(left+right+list.size()==N);
//cout<<"sub="<<sub<<"\n";
if (sub == 4) {
maxsize = max(maxsize, (int)list.size());
}
else if (sub >= 3) {
/*
UF uf(N);
for (int x : list) for (int y :list) if (x!=y&&same(x, y)) uf.unite(x, y);
for (int x : list) maxsize = max(maxsize, uf.R[uf.find(x)]);
*/
//maxsize = solve(list)._1;
}
//cout<<"R="<<R<<", size="<<maxsize<<"\n";
if (maxsize > N/2) R *= -1;
return R;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:100:48: 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:102:49: 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:107:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(left+right+list.size()==N);
~~~~~~~~~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
376 KB |
Output is correct |
2 |
Correct |
16 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
22 ms |
612 KB |
Output is correct |
5 |
Correct |
23 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
612 KB |
Output is correct |
2 |
Correct |
23 ms |
612 KB |
Output is correct |
3 |
Correct |
24 ms |
612 KB |
Output is correct |
4 |
Correct |
25 ms |
688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |