#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
vector<int>par;
void init(int sz) { par.resize(sz, -1); }
int root(int pos) {
if (par[pos] == -1) return pos;
par[pos] = root(par[pos]);
return par[pos];
}
void unite(int u, int v) { u = root(u); v = root(v); par[u] = v; }
};
int N, M, A[1 << 18], B[1 << 18], id[509][509], col[509], ming[509], represent[509], cnts; bool forced[509];
vector<int>G[509], ans; int rr[509][509];
vector<pair<int, int>> R[509];
void dfs(int root, int pos) {
if (col[pos] >= 1) return;
col[pos] = cnts;
if (id[root][pos] >= 0) {
represent[cnts] = pos;
if (root > pos && rr[root][pos] == 1) ming[cnts] = pos;
}
for (int t : G[pos]) dfs(root, t);
}
void solve(int pos) {
UnionFind UF; UF.init(N); vector<int>v;
for (int i = 0; i < N; i++) { G[i].clear(); R[i].clear(); col[i] = 0; ming[i] = -1; represent[i] = 0; cnts = 0; forced[i] = false; }
for (int i = 0; i < M; i++) {
if (A[i] == pos || B[i] == pos) continue;
if (UF.root(A[i]) == UF.root(B[i])) continue;
UF.unite(A[i], B[i]); v.push_back(i);
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
}
for (int i = 0; i < N; i++) {
if (i == pos || col[i] >= 1) continue;
cnts++; dfs(pos, i);
}
for (int i = 1; i <= cnts; i++) { if (ming[i] >= 0) forced[ming[i]] = true; }
for (int i = 0; i < N; i++) {
if (id[pos][i] == -1) continue;
if (pos > i && forced[i] == false) continue;
vector<int>vec = v;
for (int j = 1; j <= cnts; j++) { if (j == col[i]) continue; vec.push_back(id[pos][represent[j]]); }
vec.push_back(id[pos][i]);
int E = count_common_roads(vec);
R[col[i]].push_back(make_pair(E, i));
}
for (int i = 1; i <= cnts; i++) {
sort(R[i].begin(), R[i].end());
reverse(R[i].begin(), R[i].end());
for (int j = 0; j < R[i].size(); j++) { if (R[i][j].first == R[i][0].first) { ans.push_back(id[pos][R[i][j].second]); rr[pos][R[i][j].second] = 1; rr[R[i][j].second][pos] = 1; } }
}
}
void find_array(int pos) {
int cx = pos + 1, cnt = 0;
for (int i = 0; i < pos; i++) { cnt += max(rr[pos][i], rr[i][pos]); }
while (true) {
int cl = cx, cr = N, cm, minx = (1 << 30);
for (int i = 0; i < 10; i++) {
cm = (cl + cr) / 2;
vector<int> vec; for (int j = 0; j < N; j++) { if (j == pos) continue; if (j <= cm) vec.push_back(id[pos][j]); else vec.push_back(id[0][j]); }
int E = count_common_roads(vec);
for (int j = cm + 1; j < N; j++) { if (j == pos) continue; E -= rr[0][j]; }
//cout << "pos = " << pos << ", cm = " << cm << ", E = " << E << ", cnt = " << cnt << endl;
if (E >= cnt + 1) { minx = min(minx, cm); cr = cm; }
else { cl = cm; }
}
if (minx == (1 << 30)) break;
ans.push_back(id[pos][minx]); rr[pos][minx] = 1; rr[minx][pos] = 1; //cout << "# " << pos << " " << minx << endl;
cx = minx + 1; cnt++;
}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
N = n; M = u.size();
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) id[i][j] = -1; }
for (int i = 0; i < M; i++) { id[u[i]][v[i]] = i; id[v[i]][u[i]] = i; A[i] = u[i]; B[i] = v[i]; }
if (N * (N - 1) / 2 != M) {
for (int i = 0; i < N; i++) solve(i);
}
else {
solve(0);
for (int j = 1; j < N; j++) find_array(j);
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
//cout << "#answer = "; for (int i = 0; i < ans.size(); i++) cout << ans[i] << ", "; cout << endl;
return ans;
}
Compilation message
simurgh.cpp: In function 'void solve(int)':
simurgh.cpp:59:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < R[i].size(); j++) { if (R[i][j].first == R[i][0].first) { ans.push_back(id[pos][R[i][j].second]); rr[pos][R[i][j].second] = 1; rr[R[i][j].second][pos] = 1; } }
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
correct |
2 |
Correct |
2 ms |
384 KB |
correct |
3 |
Correct |
2 ms |
384 KB |
correct |
4 |
Correct |
2 ms |
384 KB |
correct |
5 |
Correct |
2 ms |
384 KB |
correct |
6 |
Correct |
2 ms |
384 KB |
correct |
7 |
Correct |
2 ms |
384 KB |
correct |
8 |
Correct |
2 ms |
384 KB |
correct |
9 |
Correct |
2 ms |
384 KB |
correct |
10 |
Correct |
2 ms |
384 KB |
correct |
11 |
Correct |
2 ms |
384 KB |
correct |
12 |
Correct |
2 ms |
384 KB |
correct |
13 |
Correct |
2 ms |
384 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
correct |
2 |
Correct |
2 ms |
384 KB |
correct |
3 |
Correct |
2 ms |
384 KB |
correct |
4 |
Correct |
2 ms |
384 KB |
correct |
5 |
Correct |
2 ms |
384 KB |
correct |
6 |
Correct |
2 ms |
384 KB |
correct |
7 |
Correct |
2 ms |
384 KB |
correct |
8 |
Correct |
2 ms |
384 KB |
correct |
9 |
Correct |
2 ms |
384 KB |
correct |
10 |
Correct |
2 ms |
384 KB |
correct |
11 |
Correct |
2 ms |
384 KB |
correct |
12 |
Correct |
2 ms |
384 KB |
correct |
13 |
Correct |
2 ms |
384 KB |
correct |
14 |
Correct |
3 ms |
640 KB |
correct |
15 |
Correct |
4 ms |
640 KB |
correct |
16 |
Correct |
4 ms |
640 KB |
correct |
17 |
Correct |
4 ms |
640 KB |
correct |
18 |
Correct |
3 ms |
640 KB |
correct |
19 |
Correct |
4 ms |
512 KB |
correct |
20 |
Correct |
3 ms |
640 KB |
correct |
21 |
Correct |
4 ms |
640 KB |
correct |
22 |
Correct |
3 ms |
640 KB |
correct |
23 |
Correct |
3 ms |
640 KB |
correct |
24 |
Correct |
4 ms |
640 KB |
correct |
25 |
Correct |
3 ms |
640 KB |
correct |
26 |
Correct |
3 ms |
640 KB |
correct |
27 |
Correct |
3 ms |
512 KB |
correct |
28 |
Correct |
3 ms |
640 KB |
correct |
29 |
Correct |
3 ms |
640 KB |
correct |
30 |
Correct |
3 ms |
640 KB |
correct |
31 |
Correct |
3 ms |
640 KB |
correct |
32 |
Correct |
4 ms |
540 KB |
correct |
33 |
Correct |
3 ms |
640 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
correct |
2 |
Correct |
2 ms |
384 KB |
correct |
3 |
Correct |
2 ms |
384 KB |
correct |
4 |
Correct |
2 ms |
384 KB |
correct |
5 |
Correct |
2 ms |
384 KB |
correct |
6 |
Correct |
2 ms |
384 KB |
correct |
7 |
Correct |
2 ms |
384 KB |
correct |
8 |
Correct |
2 ms |
384 KB |
correct |
9 |
Correct |
2 ms |
384 KB |
correct |
10 |
Correct |
2 ms |
384 KB |
correct |
11 |
Correct |
2 ms |
384 KB |
correct |
12 |
Correct |
2 ms |
384 KB |
correct |
13 |
Correct |
2 ms |
384 KB |
correct |
14 |
Correct |
3 ms |
640 KB |
correct |
15 |
Correct |
4 ms |
640 KB |
correct |
16 |
Correct |
4 ms |
640 KB |
correct |
17 |
Correct |
4 ms |
640 KB |
correct |
18 |
Correct |
3 ms |
640 KB |
correct |
19 |
Correct |
4 ms |
512 KB |
correct |
20 |
Correct |
3 ms |
640 KB |
correct |
21 |
Correct |
4 ms |
640 KB |
correct |
22 |
Correct |
3 ms |
640 KB |
correct |
23 |
Correct |
3 ms |
640 KB |
correct |
24 |
Correct |
4 ms |
640 KB |
correct |
25 |
Correct |
3 ms |
640 KB |
correct |
26 |
Correct |
3 ms |
640 KB |
correct |
27 |
Correct |
3 ms |
512 KB |
correct |
28 |
Correct |
3 ms |
640 KB |
correct |
29 |
Correct |
3 ms |
640 KB |
correct |
30 |
Correct |
3 ms |
640 KB |
correct |
31 |
Correct |
3 ms |
640 KB |
correct |
32 |
Correct |
4 ms |
540 KB |
correct |
33 |
Correct |
3 ms |
640 KB |
correct |
34 |
Correct |
29 ms |
2056 KB |
correct |
35 |
Correct |
138 ms |
2064 KB |
correct |
36 |
Correct |
100 ms |
1804 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
152 ms |
2072 KB |
correct |
39 |
Correct |
137 ms |
1912 KB |
correct |
40 |
Correct |
95 ms |
2012 KB |
correct |
41 |
Correct |
29 ms |
2040 KB |
correct |
42 |
Correct |
145 ms |
1960 KB |
correct |
43 |
Correct |
81 ms |
1656 KB |
correct |
44 |
Correct |
72 ms |
1656 KB |
correct |
45 |
Correct |
81 ms |
1628 KB |
correct |
46 |
Correct |
66 ms |
1620 KB |
correct |
47 |
Correct |
32 ms |
1408 KB |
correct |
48 |
Correct |
7 ms |
1408 KB |
correct |
49 |
Correct |
14 ms |
1408 KB |
correct |
50 |
Correct |
32 ms |
1408 KB |
correct |
51 |
Correct |
79 ms |
1656 KB |
correct |
52 |
Correct |
70 ms |
1656 KB |
correct |
53 |
Correct |
70 ms |
1732 KB |
correct |
54 |
Correct |
98 ms |
1656 KB |
correct |
55 |
Correct |
66 ms |
1784 KB |
correct |
56 |
Correct |
76 ms |
1660 KB |
correct |
57 |
Correct |
75 ms |
1656 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
correct |
2 |
Correct |
2 ms |
384 KB |
correct |
3 |
Correct |
81 ms |
4016 KB |
correct |
4 |
Correct |
126 ms |
5368 KB |
correct |
5 |
Correct |
127 ms |
6268 KB |
correct |
6 |
Correct |
132 ms |
6220 KB |
correct |
7 |
Correct |
127 ms |
6316 KB |
correct |
8 |
Correct |
128 ms |
6348 KB |
correct |
9 |
Correct |
129 ms |
6264 KB |
correct |
10 |
Correct |
138 ms |
6448 KB |
correct |
11 |
Correct |
151 ms |
6392 KB |
correct |
12 |
Correct |
133 ms |
6392 KB |
correct |
13 |
Correct |
2 ms |
384 KB |
correct |
14 |
Correct |
129 ms |
6264 KB |
correct |
15 |
Correct |
128 ms |
6264 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
correct |
2 |
Correct |
2 ms |
384 KB |
correct |
3 |
Correct |
2 ms |
384 KB |
correct |
4 |
Correct |
2 ms |
384 KB |
correct |
5 |
Correct |
2 ms |
384 KB |
correct |
6 |
Correct |
2 ms |
384 KB |
correct |
7 |
Correct |
2 ms |
384 KB |
correct |
8 |
Correct |
2 ms |
384 KB |
correct |
9 |
Correct |
2 ms |
384 KB |
correct |
10 |
Correct |
2 ms |
384 KB |
correct |
11 |
Correct |
2 ms |
384 KB |
correct |
12 |
Correct |
2 ms |
384 KB |
correct |
13 |
Correct |
2 ms |
384 KB |
correct |
14 |
Correct |
3 ms |
640 KB |
correct |
15 |
Correct |
4 ms |
640 KB |
correct |
16 |
Correct |
4 ms |
640 KB |
correct |
17 |
Correct |
4 ms |
640 KB |
correct |
18 |
Correct |
3 ms |
640 KB |
correct |
19 |
Correct |
4 ms |
512 KB |
correct |
20 |
Correct |
3 ms |
640 KB |
correct |
21 |
Correct |
4 ms |
640 KB |
correct |
22 |
Correct |
3 ms |
640 KB |
correct |
23 |
Correct |
3 ms |
640 KB |
correct |
24 |
Correct |
4 ms |
640 KB |
correct |
25 |
Correct |
3 ms |
640 KB |
correct |
26 |
Correct |
3 ms |
640 KB |
correct |
27 |
Correct |
3 ms |
512 KB |
correct |
28 |
Correct |
3 ms |
640 KB |
correct |
29 |
Correct |
3 ms |
640 KB |
correct |
30 |
Correct |
3 ms |
640 KB |
correct |
31 |
Correct |
3 ms |
640 KB |
correct |
32 |
Correct |
4 ms |
540 KB |
correct |
33 |
Correct |
3 ms |
640 KB |
correct |
34 |
Correct |
29 ms |
2056 KB |
correct |
35 |
Correct |
138 ms |
2064 KB |
correct |
36 |
Correct |
100 ms |
1804 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
152 ms |
2072 KB |
correct |
39 |
Correct |
137 ms |
1912 KB |
correct |
40 |
Correct |
95 ms |
2012 KB |
correct |
41 |
Correct |
29 ms |
2040 KB |
correct |
42 |
Correct |
145 ms |
1960 KB |
correct |
43 |
Correct |
81 ms |
1656 KB |
correct |
44 |
Correct |
72 ms |
1656 KB |
correct |
45 |
Correct |
81 ms |
1628 KB |
correct |
46 |
Correct |
66 ms |
1620 KB |
correct |
47 |
Correct |
32 ms |
1408 KB |
correct |
48 |
Correct |
7 ms |
1408 KB |
correct |
49 |
Correct |
14 ms |
1408 KB |
correct |
50 |
Correct |
32 ms |
1408 KB |
correct |
51 |
Correct |
79 ms |
1656 KB |
correct |
52 |
Correct |
70 ms |
1656 KB |
correct |
53 |
Correct |
70 ms |
1732 KB |
correct |
54 |
Correct |
98 ms |
1656 KB |
correct |
55 |
Correct |
66 ms |
1784 KB |
correct |
56 |
Correct |
76 ms |
1660 KB |
correct |
57 |
Correct |
75 ms |
1656 KB |
correct |
58 |
Correct |
2 ms |
384 KB |
correct |
59 |
Correct |
2 ms |
384 KB |
correct |
60 |
Correct |
81 ms |
4016 KB |
correct |
61 |
Correct |
126 ms |
5368 KB |
correct |
62 |
Correct |
127 ms |
6268 KB |
correct |
63 |
Correct |
132 ms |
6220 KB |
correct |
64 |
Correct |
127 ms |
6316 KB |
correct |
65 |
Correct |
128 ms |
6348 KB |
correct |
66 |
Correct |
129 ms |
6264 KB |
correct |
67 |
Correct |
138 ms |
6448 KB |
correct |
68 |
Correct |
151 ms |
6392 KB |
correct |
69 |
Correct |
133 ms |
6392 KB |
correct |
70 |
Correct |
2 ms |
384 KB |
correct |
71 |
Correct |
129 ms |
6264 KB |
correct |
72 |
Correct |
128 ms |
6264 KB |
correct |
73 |
Correct |
2 ms |
384 KB |
correct |
74 |
Incorrect |
110 ms |
6264 KB |
WA in grader: NO |
75 |
Halted |
0 ms |
0 KB |
- |