Submission #151214

# Submission time Handle Problem Language Result Execution time Memory
151214 2019-09-02T08:28:03 Z dotorya Trip to the Galapagos Islands (FXCUP4_island) C++17
31 / 100
5000 ms 119488 KB
#include "island.h"

#include <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  

#pragma warning(disable:4996)  
#pragma comment(linker, "/STACK:336777216")  
using namespace std;

#pragma gcc optimize("Ofast")

#define mp make_pair  
#define Fi first  
#define Se second  
#define pb(x) push_back(x)  
#define szz(x) ((int)(x).size())  
#define rep(i, n) for(int i=0;i<n;i++)  
#define all(x) (x).begin(), (x).end()  
#define ldb ldouble  

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 19;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;

int r[100050];
int sz[100050][2];
int root(int x) {
	return (x == r[x]) ? x : (r[x] = root(r[x]));
}

vector <pair<pii, int>> Ve;
vector <pii> conn[100050];
vector <pii> son[100050];
int par[32][100050][2];
int dep[100050];
int col[100050];

int tus;
int lr[100050][2];
bool dchk[100050];
void DFS(int n) {
	lr[n][0] = ++tus;
	dchk[n] = true;
	for (auto it : conn[n]) {
		if (dchk[it.second]) continue;
		int nn = it.second;
		par[0][nn][0] = n;
		par[0][nn][1] = it.first;
		for (int i = 1; i < 17; i++) {
			int p = par[i-1][nn][0];
			par[i][nn][0] = par[i-1][p][0];
			par[i][nn][1] = min(par[i-1][p][1], par[i-1][nn][1]);
		}
		son[n].push_back(it);
		dep[nn] = dep[n] + 1;
		DFS(nn);
	}

	lr[n][1] = tus;
}

pii upnode(int a, int c) {
	int mn = INF;
	for (int i = 16; i >= 0; i--) {
		if (c & (1 << i)) {
			mn = min(mn, par[i][a][1]);
			a = par[i][a][0];
		}
	}
	return pii(a, mn);
}
int lca(int a, int b) {
	if (dep[a] > dep[b]) swap(a, b);

	int c = dep[b] - dep[a];
	b = upnode(b, c).first;

	if (a == b) return a;

	for (int i = 16; i >= 0; i--) {
		if (par[i][a][0] != par[i][b][0]) {
			a = par[i][a][0];
			b = par[i][b][0];
		}
	}
	return par[0][a][0];
}

int dis[100050];
void DFS2(int n, int c) {
	dis[n] = -INF;
	if (col[n] == c) dis[n] = INF;
	for (auto it : son[n]) {
		DFS2(it.second, c);
		dis[n] = max(dis[n], min(dis[it.second], it.first));
	}
}
void DFS3(int n) {
	for (auto it : son[n]) {
		dis[it.second] = max(dis[it.second], min(dis[n], it.first));
		DFS3(it.second);
	}
}

int ch[100050]; // answer array
int ans[505][100050];
vector <int> Vc[100050];

int u[100050];

void Init(int K, std::vector<int> F, std::vector<int> S, std::vector<int> E) {
	int N = F.size(), M = S.size(), i, j;

	for (i = 0; i < N; i++) col[i] = F[i];
	for (i = 0; i < N; i++) Vc[F[i]].push_back(i);

	for (i = 0; i < N; i++) r[i] = i;
	for (i = M - 1; i >= 0; i--) {
		int t1 = S[i], t2 = E[i];
		if (root(t1) == root(t2)) continue;
		int r1 = root(t1), r2 = root(t2);
		r[r1] = r2;
		Ve.emplace_back(pii(t1, t2), i);
		conn[t1].emplace_back(i, t2);
		conn[t2].emplace_back(i, t1);
	}
	for (i = 0; i < 17; i++) {
		par[i][0][0] = 0;
		par[i][0][1] = INF;
	}
	DFS(0);

	for (i = 0; i < K; i++) u[i] = i;
	sort(u, u + K, [](int a, int b) {
		if (Vc[a].size() != Vc[b].size()) return Vc[a].size() > Vc[b].size();
		return a > b;
	});

	for (i = 0; i < K; i++) ch[i] = -1;

	for (i = 0; i < K && i < 200; i++) {
		int c = u[i];
		ch[c] = i;
		for (j = 0; j < N; j++) dis[j] = -1;
		DFS2(0, c);
		DFS3(0);

		for (j = 0; j < K; j++) ans[i][j] = -1;
		for (j = 0; j < N; j++) ans[i][F[j]] = max(ans[i][F[j]], dis[j]);
	}

	for (i = 0; i < N; i++) {
		r[i] = i;
		sz[i][0] = sz[i][1] = 0;
	}
}

int ispar(int a, int b) {
	if (dep[a] <= dep[b]) return -INF;

	pii u = upnode(a, dep[a] - dep[b]);
	if (u.first == b) return u.second;
	else return -INF;
}

map <pii, int> Ma;
int Vl[10050];
int vlc;
int Separate(int A, int B) {
	if (A > B) swap(A, B);
	if (ch[A] >= 0) return ans[ch[A]][B] + 1;
	if (ch[B] >= 0) return ans[ch[B]][A] + 1;
	if (Ma.count(pii(A, B))) return Ma[pii(A, B)];

	int vlc = 0;
	vector <int> Vu;
	for (auto it : Vc[A]) Vl[vlc++] = it;
	for (auto it : Vc[B]) Vl[vlc++] = it;
	sort(Vl, Vl + vlc, [](int a, int b) {
		return lr[a][0] < lr[b][0];
	});
	vlc = unique(Vl, Vl + vlc) - Vl;

	int ovlc = vlc;
	for (int i = 0; i + 1 < ovlc; i++) Vl[vlc++] = lca(Vl[i], Vl[i + 1]);

	sort(Vl, Vl+vlc, [](int a, int b) {
		return lr[a][0] < lr[b][0];
	});
	vlc = unique(Vl, Vl + vlc) - Vl;

	Vu.clear();
	Vu.push_back(Vl[0]);

	vector <pair<int, pii>> Vet;
	for (int i = 1; i < vlc; i++) {
		int rv;

		while (1) {
			rv = ispar(Vl[i], Vu.back());
			if (rv != -INF) break;
			Vu.pop_back();
		}

		Vet.emplace_back(rv, pii(Vl[i], Vu.back()));
		Vu.push_back(Vl[i]);
	}
	sort(all(Vet));
	reverse(all(Vet));

	for (int i = 0; i < vlc; i++) {
		int it = Vl[i];
		r[it] = it;
		sz[it][0] = sz[it][1] = 0;
		if (col[it] == A) sz[it][0] = 1;
		if (col[it] == B) sz[it][1] = 1;
	}

	for (auto it : Vet) {
		int r1 = root(it.second.first), r2 = root(it.second.second);
		if (r1 == r2) continue;

		sz[r1][0] += sz[r2][0];
		sz[r1][1] += sz[r2][1];
		r[r2] = r1;
		if (sz[r1][0] && sz[r1][1]) return Ma[pii(A, B)] = it.first + 1;
	}
	return 0;
}

Compilation message

island.cpp:25:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable:4996)  
 
island.cpp:26:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/STACK:336777216")  
 
island.cpp:29:0: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
 #pragma gcc optimize("Ofast")
# Verdict Execution time Memory Grader output
1 Correct 2574 ms 119436 KB Output is correct
2 Correct 2421 ms 119404 KB Output is correct
3 Correct 2977 ms 119232 KB Output is correct
4 Correct 2549 ms 119488 KB Output is correct
5 Correct 2416 ms 119352 KB Output is correct
6 Correct 2781 ms 119408 KB Output is correct
7 Correct 2548 ms 119308 KB Output is correct
8 Correct 2576 ms 119464 KB Output is correct
9 Correct 2537 ms 119292 KB Output is correct
10 Correct 2512 ms 119320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 280 ms 36832 KB Output is correct
4 Correct 361 ms 36992 KB Output is correct
5 Correct 538 ms 36960 KB Output is correct
6 Correct 769 ms 37188 KB Output is correct
7 Correct 1409 ms 37340 KB Output is correct
8 Correct 2617 ms 37908 KB Output is correct
9 Execution timed out 5007 ms 38304 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 2574 ms 119436 KB Output is correct
4 Correct 2421 ms 119404 KB Output is correct
5 Correct 2977 ms 119232 KB Output is correct
6 Correct 2549 ms 119488 KB Output is correct
7 Correct 2416 ms 119352 KB Output is correct
8 Correct 2781 ms 119408 KB Output is correct
9 Correct 2548 ms 119308 KB Output is correct
10 Correct 2576 ms 119464 KB Output is correct
11 Correct 2537 ms 119292 KB Output is correct
12 Correct 2512 ms 119320 KB Output is correct
13 Correct 280 ms 36832 KB Output is correct
14 Correct 361 ms 36992 KB Output is correct
15 Correct 538 ms 36960 KB Output is correct
16 Correct 769 ms 37188 KB Output is correct
17 Correct 1409 ms 37340 KB Output is correct
18 Correct 2617 ms 37908 KB Output is correct
19 Execution timed out 5007 ms 38304 KB Time limit exceeded
20 Halted 0 ms 0 KB -