답안 #97334

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
97334 2019-02-15T09:41:42 Z E869120 통행료 (IOI18_highway) C++14
0 / 100
1500 ms 24704 KB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> G[1<<18]; int depth[1<<18], par[1<<18], BB[1<<18]; bool anti[1<<18];
map<pair<int, int>, int>M;

void dfs(int pos, int dep) {
	depth[pos] = dep;
	for (int i = 0; i < G[pos].size(); i++) {
		if(depth[G[pos][i]]!=-1) continue;
		par[G[pos][i]] = i;
		BB[G[pos][i]] = M[make_pair(G[pos][i], pos)];
		dfs(G[pos][i], dep + 1);
	}
}

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
	for (int i = 0; i < U.size(); i++) {
		G[U[i]].push_back(V[i]);
		G[V[i]].push_back(U[i]);
		M[make_pair(U[i], V[i])] = i; M[make_pair(V[i], U[i])] = i;
	}
	for (int i = 0; i < N; i++) depth[i] = -1;
	dfs(0, 0);
	
	vector<int> W(N - 1, 0);
	for (int i = 0; i < N - 1; i++) W[i] = 0; long long c1 = ask(W);
	for (int i = 0; i < N - 1; i++) W[i] = 1; long long c2 = ask(W);
	
	vector<pair<int, int>> T;
	for (int i = 1; i < N; i++) T.push_back(make_pair(depth[i], i));
	sort(T.begin(), T.end());
	
	// Part 1. Find the lowest
	int L1 = 0, R1 = N - 1, M1, ret1 = 0;
	for (int i = 0; i < 19; i++) {
		M1 = (L1 + R1) / 2;
		for (int j = 0; j < T.size(); j++) { if (j < M1) W[BB[T[j].second]] = 0; else W[BB[T[j].second]] = 1; }
		long long c3 = ask(W);
		if (c1 == c3) { R1 = M1; }
		else { L1 = M1; ret1 = max(ret1, M1); }
	}
	
	int cx = T[ret1].second;
	while (true) {
		anti[cx] = true; if (cx == 0) break;
		cx = par[cx];
	}
	
	// Part 2. Find the highest
	int L2 = 0, R2 = N - 1, M2, ret2 = N - 2;
	for (int i = 0; i < 19; i++) {
		M2 = (L2 + R2) / 2;
		for (int j = 0; j < T.size(); j++) { if (j <= M2) W[BB[T[j].second]] = 1; else W[BB[T[j].second]] = 0; }
		long long c3 = ask(W);
		if (c1 == c3) { L2 = M2; }
		else { R2 = M2; ret2 = min(ret2, M2);  }
	}
	
	// Part 3. Find the second
	long long P1 = T[ret1].first, P2 = T[ret2].first - 1, Leng = (c2 - c1) / (1LL * B - 1LL * A);
	long long P3 = P2 + (Leng - (P1 - P2));
	
	if (P3 == P2) {
		answer(par[T[ret2].second], T[ret1].second);
		return;
	}
	
	vector<int>I;
	for (int i = 0; i < N; i++) {
		if (depth[i] == P3 && anti[i] == false) { I.push_back(i); }
	}
	
	int L3 = 0, R3 = I.size(), M3, ret3 = I.size() - 1;
	for (int i = 0; i < 19; i++) {
		M3 = (L3 + R3) / 2;
		for (int j = 0; j < N; j++) W[j] = 0;
		for (int j = 0; j <= M3; j++) W[BB[I[j]]] = 1;
		
		long long c3 = ask(W);
		if (c1 == c3) { L3 = M3; }
		else { R3 = M3; ret3 = min(ret3, M3); }
	}
	
	answer(I[ret3], T[ret1].second);
	return;
}

Compilation message

highway.cpp: In function 'void dfs(int, int)':
highway.cpp:10:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:19:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < U.size(); i++) {
                  ~~^~~~~~~~~~
highway.cpp:28:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (int i = 0; i < N - 1; i++) W[i] = 0; long long c1 = ask(W);
  ^~~
highway.cpp:28:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (int i = 0; i < N - 1; i++) W[i] = 0; long long c1 = ask(W);
                                            ^~~~
highway.cpp:29:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (int i = 0; i < N - 1; i++) W[i] = 1; long long c2 = ask(W);
  ^~~
highway.cpp:29:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (int i = 0; i < N - 1; i++) W[i] = 1; long long c2 = ask(W);
                                            ^~~~
highway.cpp:39:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < T.size(); j++) { if (j < M1) W[BB[T[j].second]] = 0; else W[BB[T[j].second]] = 1; }
                   ~~^~~~~~~~~~
highway.cpp:55:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < T.size(); j++) { if (j <= M2) W[BB[T[j].second]] = 1; else W[BB[T[j].second]] = 0; }
                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3095 ms 6392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 6696 KB Output is correct
2 Correct 43 ms 8608 KB Output is correct
3 Incorrect 504 ms 24704 KB Output is incorrect: {s, t} is wrong.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 30 ms 9376 KB Output is incorrect: {s, t} is wrong.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3094 ms 6648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 8568 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 8692 KB Incorrect
2 Halted 0 ms 0 KB -