This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
Compilation message (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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |