#include "island.h"
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define pii pair<int,int>
#define N_ 101000
vector<int>G[N_], E[N_], L[N_];
int n, m, UF[N_][2], MM[N_];
int Find(int a, int pv) {
if (UF[a][pv] == a)return a;
return UF[a][pv] = Find(UF[a][pv], pv);
}
void Add_Edge(int a, int b, int c) {
E[a].push_back(b);
E[b].push_back(a);
L[a].push_back(c);
L[b].push_back(c);
}
int D[101000][20], Num[101000];
void DFS(int a, int pp, int cur) {
Num[a] = cur;
for (int i = 0; i < E[a].size(); i++) {
int x = E[a][i];
if (x == pp)continue;
D[cur][0] = L[a][i];
DFS(x, a, cur + 1);
}
}
void Init(int K, std::vector<int> F, std::vector<int> A, std::vector<int> B) {
int i, j;
n = F.size(), m = A.size();
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
for (i = 0; i < n; i++) {
UF[i][0] = i;
UF[i][1] = i;
}
for (i = 0; i < m; i++) {
int a = Find(A[i], 0), b = Find(B[i], 0);
if (a != b) {
int a2 = Find(A[i], 1), b2 = Find(B[i], 1);
Add_Edge(a2, b, i+1);
UF[b][0] = a;
UF[a2][1] = b2;
}
}
DFS(Find(0,0), -1, 1);
for (i = 0; i < n; i++) {
G[F[i]].push_back(Num[i]);
}
for (i = 0; i < K; i++) {
sort(G[i].begin(), G[i].end());
}
for (i = 0; i < 16; i++) {
for (j = 1; j < n; j++) {
if ((j + (1 << i)) >= n)D[j][i+1] = 1e9;
else D[j][i+1] = max(D[j][i], D[j + (1 << i)][i]);
}
}
for (i = 1; i <= n; i++) {
MM[i] = MM[i - 1];
if ((1 << (MM[i] + 1)) <= i)MM[i]++;
}
}
int LMQ(int b, int e) {
int t = MM[e - b];
return max(D[b][t], D[e - (1 << t)][t]);
}
map<pii, int>Map;
int Separate(int A, int B) {
if (G[A].size() > G[B].size())swap(A, B);
if (Map.count({ min(A,B),max(A,B) }))return Map[{ min(A, B), max(A, B) }];
int Mn = 1e9;
for (auto &t : G[A]) {
int pv = lower_bound(G[B].begin(), G[B].end(), t) - G[B].begin();
if (pv < G[B].size()) {
Mn = min(Mn, LMQ(t, G[B][pv]));
}
if (pv) {
Mn = min(Mn, LMQ(G[B][pv - 1], t));
}
}
return Map[{ min(A, B), max(A, B) }] = m - Mn + 1;
}
Compilation message
island.cpp: In function 'void DFS(int, int, int)':
island.cpp:25:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < E[a].size(); i++) {
~~^~~~~~~~~~~~~
island.cpp: In function 'int Separate(int, int)':
island.cpp:82:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pv < G[B].size()) {
~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
40388 KB |
Output is correct |
2 |
Correct |
341 ms |
44860 KB |
Output is correct |
3 |
Correct |
361 ms |
44860 KB |
Output is correct |
4 |
Correct |
383 ms |
44952 KB |
Output is correct |
5 |
Correct |
345 ms |
44860 KB |
Output is correct |
6 |
Correct |
352 ms |
44936 KB |
Output is correct |
7 |
Correct |
344 ms |
44908 KB |
Output is correct |
8 |
Correct |
350 ms |
44988 KB |
Output is correct |
9 |
Correct |
343 ms |
44900 KB |
Output is correct |
10 |
Correct |
334 ms |
44976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
310 ms |
40916 KB |
Output is correct |
4 |
Correct |
319 ms |
40944 KB |
Output is correct |
5 |
Correct |
496 ms |
40676 KB |
Output is correct |
6 |
Correct |
514 ms |
40784 KB |
Output is correct |
7 |
Correct |
732 ms |
40928 KB |
Output is correct |
8 |
Correct |
1129 ms |
40932 KB |
Output is correct |
9 |
Correct |
985 ms |
41592 KB |
Output is correct |
10 |
Correct |
730 ms |
41844 KB |
Output is correct |
11 |
Correct |
724 ms |
41924 KB |
Output is correct |
12 |
Correct |
747 ms |
41980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
342 ms |
40388 KB |
Output is correct |
4 |
Correct |
341 ms |
44860 KB |
Output is correct |
5 |
Correct |
361 ms |
44860 KB |
Output is correct |
6 |
Correct |
383 ms |
44952 KB |
Output is correct |
7 |
Correct |
345 ms |
44860 KB |
Output is correct |
8 |
Correct |
352 ms |
44936 KB |
Output is correct |
9 |
Correct |
344 ms |
44908 KB |
Output is correct |
10 |
Correct |
350 ms |
44988 KB |
Output is correct |
11 |
Correct |
343 ms |
44900 KB |
Output is correct |
12 |
Correct |
334 ms |
44976 KB |
Output is correct |
13 |
Correct |
310 ms |
40916 KB |
Output is correct |
14 |
Correct |
319 ms |
40944 KB |
Output is correct |
15 |
Correct |
496 ms |
40676 KB |
Output is correct |
16 |
Correct |
514 ms |
40784 KB |
Output is correct |
17 |
Correct |
732 ms |
40928 KB |
Output is correct |
18 |
Correct |
1129 ms |
40932 KB |
Output is correct |
19 |
Correct |
985 ms |
41592 KB |
Output is correct |
20 |
Correct |
730 ms |
41844 KB |
Output is correct |
21 |
Correct |
724 ms |
41924 KB |
Output is correct |
22 |
Correct |
747 ms |
41980 KB |
Output is correct |
23 |
Correct |
680 ms |
41924 KB |
Output is correct |
24 |
Correct |
492 ms |
42168 KB |
Output is correct |
25 |
Correct |
439 ms |
42188 KB |
Output is correct |
26 |
Correct |
387 ms |
42240 KB |
Output is correct |
27 |
Correct |
373 ms |
42364 KB |
Output is correct |
28 |
Correct |
352 ms |
42788 KB |
Output is correct |
29 |
Correct |
354 ms |
43324 KB |
Output is correct |
30 |
Correct |
348 ms |
44220 KB |
Output is correct |
31 |
Correct |
344 ms |
44544 KB |
Output is correct |
32 |
Correct |
345 ms |
44860 KB |
Output is correct |