제출 #149492

#제출 시각아이디문제언어결과실행 시간메모리
149492お前はもう死んでいる (#200)갈라파고스 여행 (FXCUP4_island)C++17
100 / 100
3085 ms20600 KiB
#include "island.h"

#include <vector>
#include <algorithm>
#include <map>
#include <iostream>

const int ms = 100100;
int par[ms], when[ms];
std::vector<int> freq[ms];
std::vector< std::pair<int, int> > times[ms];
int getPar(int x) { return par[x] == x ? x : getPar(par[x]); }

int wtf;
void Init(int K, std::vector<int> F, std::vector<int> S, std::vector<int> E){
	int N = F.size(), M = S.size();
	for(int i = 0; i < N; i++) {
		par[i] = i;
		when[i] = 1e9;
		freq[F[i]].push_back(i);
		times[i].emplace_back(F[i], 0);
	}
	wtf = M;
	for(int i = 0; i < M; i++) {
		int u = getPar(S[M-i-1]), v = getPar(E[M-i-1]);
		if(u == v) continue;
		if(times[u].size() > times[v].size()) {
			std::swap(u, v);
		}
		for(int j = 0; j < times[u].size(); j++) {
			times[v].emplace_back(times[u][j].first, i);
		}
		when[u] = i;
		par[u] = v;
	}
	for(int i = 0; i < N; i++) {
		std::sort(times[i].begin(), times[i].end());
	}
}

std::map<std::pair<int, int>, int> memo;


int Separate(int A, int B){
	if(freq[A].size() > freq[B].size()) {
		std::swap(A, B);
	}
	if(memo.count(std::pair<int, int>(A, B))) return memo[std::pair<int, int>(A, B)];
	int ans = 1e9;
	for(int i = 0; i < freq[A].size(); i++) {
		int u = freq[A][i];
		int cost = 0;
		while(1) {
			int id = std::lower_bound(times[u].begin(), times[u].end(), std::pair<int, int>(B, -1)) - times[u].begin();
			if(id < times[u].size() && times[u][id].first == B) {
				ans = std::min(ans, std::max(times[u][id].second, cost));
			}
			if(par[u] == u) break;
			cost = when[u];
			u = par[u];
		}
	}
	ans = wtf - ans;
	return memo[std::pair<int, int>(A, B)] = ans;
}

컴파일 시 표준 에러 (stderr) 메시지

island.cpp: In function 'void Init(int, std::vector<int>, std::vector<int>, std::vector<int>)':
island.cpp:30:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < times[u].size(); j++) {
                  ~~^~~~~~~~~~~~~~~~~
island.cpp: In function 'int Separate(int, int)':
island.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < freq[A].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~
island.cpp:55:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if(id < times[u].size() && times[u][id].first == B) {
       ~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...