Submission #58301

# Submission time Handle Problem Language Result Execution time Memory
58301 2018-07-17T12:35:04 Z E869120 Towns (IOI15_towns) C++14
35 / 100
30 ms 700 KB
#include "towns.h"
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

int Weight[111][111], Dist[111][111], NN, TT, SB, cu[111], cv[111], hub[111], CNT = 0; bool used[111];
vector<int>X[111];

//int getDistance(int p1, int p2) { return Weight[p1][p2]; }

int getDist(int p1, int p2) {
	if (p1 == p2) return 0;
	if (p1 > p2) swap(p1, p2);
	if (Dist[p1][p2] == 0) {
		Dist[p1][p2] = getDistance(p1, p2) + 1;
	}
	return Dist[p1][p2] - 1;
}

void dfs(int pos) {
	if (used[pos] == true) return;
	CNT++; used[pos] = true;
	for (int i = 0; i < X[pos].size(); i++) dfs(X[pos][i]);
}

int hubDistance(int N, int sub) {
	for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) Dist[i][j] = 0; X[i].clear(); used[i] = false; }

	int maxn1 = 0, u = 0;
	for (int i = 0; i < N; i++) { int V = getDist(0, i); if (V > maxn1) { maxn1 = V; u = i; } }
	int maxn2 = 0, v = 0;
	for (int i = 0; i < N; i++) { int V = getDist(u, i); if (V > maxn2) { maxn2 = V; v = i; } cu[i] = V; }

	for (int i = 0; i < N; i++) cv[i] = getDist(v, i);

	int ret = (1 << 30), maxid = 0;
	for (int i = 0; i < N; i++) { if (abs(cu[i] - cv[i]) < ret) { ret = abs(cu[i] - cv[i]); maxid = i; } }

	int p[3] = { 0,0,0 }, R = cu[maxid] - cv[maxid], S = (maxn2 + ret) / 2; vector<int>vec;
	for (int i = 0; i < N; i++) {
		if (cu[i] - cv[i] < R) p[0]++;
		if (cu[i] - cv[i] == R) { p[1]++; vec.push_back(i); }
		if (cu[i] - cv[i] > R) p[2]++;
	}
	if (sub == 1 || sub == 2 || sub == 4) {
		if (p[0] > (N / 2) || p[1] > (N / 2) || p[2] > (N / 2)) return -S;
		return S;
	}
	if (sub == 3) {
		if (p[0] > (N / 2) || p[2] > (N / 2)) return -S;
		if (p[1] <= (N / 2)) return -S;

		for (int i = 0; i < vec.size(); i++) hub[vec[i]] = max(cu[vec[i]], cv[vec[i]]) - S;

		// vec は同じやつのリスト
		for (int i = 0; i < vec.size(); i++) {
			for (int j = 0; j < vec.size(); j++) {
				int Z = getDist(vec[i], vec[j]);
				if (Z == hub[vec[i]] + hub[vec[j]] || Z == 0) continue;
				X[vec[i]].push_back(vec[j]);
			}
		}
		for (int i = 0; i < vec.size(); i++) {
			if (used[vec[i]] == true) continue;
			CNT = 0;
			dfs(vec[i]);
			if (CNT > N / 2) return -S;
		}
		return S;
	}
	return 0;
}

Compilation message

towns.cpp: In function 'void dfs(int)':
towns.cpp:24:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < X[pos].size(); i++) dfs(X[pos][i]);
                  ~~^~~~~~~~~~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:54:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < vec.size(); i++) hub[vec[i]] = max(cu[vec[i]], cv[vec[i]]) - S;
                   ~~^~~~~~~~~~~~
towns.cpp:57:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < vec.size(); i++) {
                   ~~^~~~~~~~~~~~
towns.cpp:58:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j = 0; j < vec.size(); j++) {
                    ~~^~~~~~~~~~~~
towns.cpp:64:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < vec.size(); i++) {
                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 416 KB Output is correct
2 Correct 19 ms 416 KB Output is correct
3 Correct 2 ms 416 KB Output is correct
4 Correct 24 ms 428 KB Output is correct
5 Correct 23 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 596 KB Output is correct
2 Correct 20 ms 596 KB Output is correct
3 Correct 22 ms 596 KB Output is correct
4 Correct 24 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 700 KB Output isn't correct
2 Halted 0 ms 0 KB -