#include "simurgh.h"
#include <algorithm>
struct Edge
{
Edge(int u, int v, int i) : start(u), end(v), id(i) {}
int start, end, id;
};
const int MAXN = 500;
const int MAXM = MAXN * (MAXN - 1) / 2;
const int LOGN = 9;
std::vector<Edge> graph[MAXN];
int parent[MAXN];
int posInSpanning[MAXM];
std::vector<Edge> spanning;
int previous[MAXN], edgeToPrev[MAXN];
int depth[MAXN];
int ancester[MAXN][LOGN];
std::vector<int> golden;
std::vector<Edge> todo;
std::vector<int> order;
bool seen[MAXN];
bool isRoyal[MAXM];
std::vector<int> royal;
void disjointreset(int nodes)
{
for (int iNode = 0; iNode < nodes; iNode++)
parent[iNode] = iNode;
}
int find(int node)
{
if (node != parent[node])
parent[node] = find(parent[node]);
return parent[node];
}
bool merge(int start, int end)
{
int st = find(start), nd = find(end);
parent[nd] = st;
return (st != nd);
}
void root(int node)
{
for (Edge &edge : graph[node])
if (posInSpanning[edge.id] != -1 && previous[node] != edge.end)
{
depth[edge.end] = depth[node] + 1;
previous[edge.end] = node;
edgeToPrev[edge.end] = edge.id;
isRoyal[edge.id] = true;
root(edge.end);
}
order.emplace_back(node);
}
void setbinlift(int nodes)
{
for (int iNode = 0; iNode < nodes; iNode++)
{
ancester[iNode][0] = previous[iNode];
for (int iExp = 1; iExp < LOGN; iExp++)
ancester[iNode][iExp] = -1;
}
}
int findanc(int node, int exp)
{
if (ancester[node][exp] == -1)
ancester[node][exp] = findanc(findanc(node, exp - 1), exp - 1);
return ancester[node][exp];
}
int lift(int node, int dist)
{
if (dist == 0)
return node;
int exp = 31 - __builtin_clz(dist);
return lift(findanc(node, exp), dist - (1 << exp));
}
int lca(int u, int v)
{
if (depth[u] < depth[v])
std::swap(u, v);
u = lift(u, depth[u] - depth[v]);
if (u == v)
return u;
for (int iExp = LOGN - 1; iExp > -1; iExp--)
if (findanc(u, iExp) != findanc(v, iExp))
{
u = findanc(u, iExp);
v = findanc(v, iExp);
}
return findanc(u, 0);
}
int replace(int edge, int other)
{
golden[posInSpanning[edge]] = other;
int inTree = count_common_roads(golden);
golden[posInSpanning[edge]] = edge;
return inTree;
}
int findval(int node, int stop, int expVal, int edge, int &known)
{
if (depth[node] <= depth[stop])
return -1;
if (find(node) == node)
{
int inTree = replace(edgeToPrev[node], edge);
parent[node] = previous[node];
if (inTree == expVal)
{
todo.emplace_back(0, 0, edgeToPrev[node]);
return findval(previous[node], stop, expVal, edge, known);
}
todo.emplace_back(1, 0, edgeToPrev[node]);
findval(previous[node], stop, expVal, edge, known);
return (expVal < inTree);
}
else
{
known = edgeToPrev[node];
return findval(find(node), stop, expVal, edge, known);
}
}
int count(int node, std::vector<int> &edges, int nodes)
{
disjointreset(nodes);
golden.clear();
for (int edge : edges)
{
merge(graph[node][edge].start, graph[node][edge].end);
golden.emplace_back(graph[node][edge].id);
}
int inTree = 0;
for (Edge &edge : spanning)
if (merge(edge.start, edge.end))
{
inTree += isRoyal[edge.id];
golden.emplace_back(edge.id);
}
return (count_common_roads(golden) - inTree);
}
int search(int left, int right, std::vector<int> &sspace, int node, int nodes)
{
if (right - left == 1)
return left;
std::vector<int> ask;
for (int iEdge = left; iEdge < (left + right) / 2; iEdge++)
ask.emplace_back(sspace[iEdge]);
if (count(node, ask, nodes) > 0)
return search(left, (left + right) / 2, sspace, node, nodes);
return search((left + right) / 2, right, sspace, node, nodes);
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v)
{
int roads = u.size();
for (int iRoad = 0; iRoad < roads; iRoad++)
{
graph[u[iRoad]].emplace_back(u[iRoad], v[iRoad], iRoad);
graph[v[iRoad]].emplace_back(v[iRoad], u[iRoad], iRoad);
}
disjointreset(n);
for (int iRoad = 0; iRoad < roads; iRoad++)
{
if (merge(u[iRoad], v[iRoad]))
{
posInSpanning[iRoad] = spanning.size();
spanning.emplace_back(u[iRoad], v[iRoad], iRoad);
golden.emplace_back(iRoad);
}
else
{
posInSpanning[iRoad] = -1;
}
}
int inTree = count_common_roads(golden);
root(0);
setbinlift(n);
disjointreset(n);
for (int iRoad = 0; iRoad < roads; iRoad++)
if (posInSpanning[iRoad] == -1)
{
int anc = lca(u[iRoad], v[iRoad]);
int known = -1;
int val = std::max(findval(u[iRoad], anc, inTree, iRoad, known), findval(v[iRoad], anc, inTree, iRoad, known));
if (val == -1 && !todo.empty())
{
if (known == -1)
val = 0;
else
val = isRoyal[known] ^ (replace(known, iRoad) != inTree);
}
while (!todo.empty())
{
isRoyal[todo.back().id] = val ^ (todo.back().start);
todo.pop_back();
}
}
for (int iNode = 0; iNode < n; iNode++)
{
std::vector<int> searchspace;
for (int iEdge = 0; iEdge < (int)graph[order[iNode]].size(); iEdge++)
if (seen[graph[order[iNode]][iEdge].end] && posInSpanning[graph[order[iNode]][iEdge].id] == -1)
searchspace.emplace_back(iEdge);
int connected = count(order[iNode], searchspace, n);
for (int iRoy = 0; iRoy < connected; iRoy++)
{
int pos = search(0, searchspace.size(), searchspace, order[iNode], n);
isRoyal[graph[order[iNode]][searchspace[pos]].id] = true;
searchspace.erase(searchspace.begin() + pos);
}
seen[order[iNode]] = true;
}
for (int iEdge = 0; iEdge < roads; iEdge++)
if (isRoyal[iEdge])
royal.emplace_back(iEdge);
return royal;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
380 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
352 KB |
correct |
13 |
Correct |
2 ms |
428 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
380 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
352 KB |
correct |
13 |
Correct |
2 ms |
428 KB |
correct |
14 |
Correct |
4 ms |
376 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
3 ms |
376 KB |
correct |
17 |
Correct |
3 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
376 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
3 ms |
376 KB |
correct |
21 |
Correct |
3 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
380 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
376 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
380 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
352 KB |
correct |
13 |
Correct |
2 ms |
428 KB |
correct |
14 |
Correct |
4 ms |
376 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
3 ms |
376 KB |
correct |
17 |
Correct |
3 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
376 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
3 ms |
376 KB |
correct |
21 |
Correct |
3 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
380 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
376 KB |
correct |
34 |
Correct |
49 ms |
1912 KB |
correct |
35 |
Correct |
48 ms |
1840 KB |
correct |
36 |
Correct |
42 ms |
1656 KB |
correct |
37 |
Correct |
13 ms |
376 KB |
correct |
38 |
Correct |
48 ms |
1972 KB |
correct |
39 |
Correct |
47 ms |
1784 KB |
correct |
40 |
Correct |
43 ms |
1660 KB |
correct |
41 |
Correct |
43 ms |
1912 KB |
correct |
42 |
Correct |
44 ms |
2040 KB |
correct |
43 |
Correct |
13 ms |
1400 KB |
correct |
44 |
Correct |
12 ms |
1016 KB |
correct |
45 |
Correct |
13 ms |
1148 KB |
correct |
46 |
Correct |
11 ms |
1016 KB |
correct |
47 |
Correct |
8 ms |
632 KB |
correct |
48 |
Correct |
5 ms |
376 KB |
correct |
49 |
Correct |
6 ms |
504 KB |
correct |
50 |
Correct |
8 ms |
636 KB |
correct |
51 |
Correct |
13 ms |
1144 KB |
correct |
52 |
Correct |
12 ms |
1016 KB |
correct |
53 |
Correct |
11 ms |
1016 KB |
correct |
54 |
Correct |
14 ms |
1464 KB |
correct |
55 |
Correct |
13 ms |
1144 KB |
correct |
56 |
Correct |
13 ms |
1272 KB |
correct |
57 |
Correct |
13 ms |
1144 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
151 ms |
5296 KB |
correct |
4 |
Correct |
247 ms |
7160 KB |
correct |
5 |
Correct |
246 ms |
7160 KB |
correct |
6 |
Correct |
227 ms |
7032 KB |
correct |
7 |
Correct |
215 ms |
7240 KB |
correct |
8 |
Correct |
209 ms |
7152 KB |
correct |
9 |
Correct |
244 ms |
7160 KB |
correct |
10 |
Correct |
238 ms |
7232 KB |
correct |
11 |
Correct |
252 ms |
7184 KB |
correct |
12 |
Correct |
257 ms |
7160 KB |
correct |
13 |
Correct |
2 ms |
376 KB |
correct |
14 |
Correct |
246 ms |
7172 KB |
correct |
15 |
Correct |
259 ms |
7160 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
380 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
352 KB |
correct |
13 |
Correct |
2 ms |
428 KB |
correct |
14 |
Correct |
4 ms |
376 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
3 ms |
376 KB |
correct |
17 |
Correct |
3 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
376 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
3 ms |
376 KB |
correct |
21 |
Correct |
3 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
380 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
376 KB |
correct |
34 |
Correct |
49 ms |
1912 KB |
correct |
35 |
Correct |
48 ms |
1840 KB |
correct |
36 |
Correct |
42 ms |
1656 KB |
correct |
37 |
Correct |
13 ms |
376 KB |
correct |
38 |
Correct |
48 ms |
1972 KB |
correct |
39 |
Correct |
47 ms |
1784 KB |
correct |
40 |
Correct |
43 ms |
1660 KB |
correct |
41 |
Correct |
43 ms |
1912 KB |
correct |
42 |
Correct |
44 ms |
2040 KB |
correct |
43 |
Correct |
13 ms |
1400 KB |
correct |
44 |
Correct |
12 ms |
1016 KB |
correct |
45 |
Correct |
13 ms |
1148 KB |
correct |
46 |
Correct |
11 ms |
1016 KB |
correct |
47 |
Correct |
8 ms |
632 KB |
correct |
48 |
Correct |
5 ms |
376 KB |
correct |
49 |
Correct |
6 ms |
504 KB |
correct |
50 |
Correct |
8 ms |
636 KB |
correct |
51 |
Correct |
13 ms |
1144 KB |
correct |
52 |
Correct |
12 ms |
1016 KB |
correct |
53 |
Correct |
11 ms |
1016 KB |
correct |
54 |
Correct |
14 ms |
1464 KB |
correct |
55 |
Correct |
13 ms |
1144 KB |
correct |
56 |
Correct |
13 ms |
1272 KB |
correct |
57 |
Correct |
13 ms |
1144 KB |
correct |
58 |
Correct |
2 ms |
256 KB |
correct |
59 |
Correct |
2 ms |
376 KB |
correct |
60 |
Correct |
151 ms |
5296 KB |
correct |
61 |
Correct |
247 ms |
7160 KB |
correct |
62 |
Correct |
246 ms |
7160 KB |
correct |
63 |
Correct |
227 ms |
7032 KB |
correct |
64 |
Correct |
215 ms |
7240 KB |
correct |
65 |
Correct |
209 ms |
7152 KB |
correct |
66 |
Correct |
244 ms |
7160 KB |
correct |
67 |
Correct |
238 ms |
7232 KB |
correct |
68 |
Correct |
252 ms |
7184 KB |
correct |
69 |
Correct |
257 ms |
7160 KB |
correct |
70 |
Correct |
2 ms |
376 KB |
correct |
71 |
Correct |
246 ms |
7172 KB |
correct |
72 |
Correct |
259 ms |
7160 KB |
correct |
73 |
Correct |
2 ms |
376 KB |
correct |
74 |
Correct |
249 ms |
7160 KB |
correct |
75 |
Correct |
260 ms |
6904 KB |
correct |
76 |
Correct |
124 ms |
2936 KB |
correct |
77 |
Correct |
244 ms |
7216 KB |
correct |
78 |
Correct |
247 ms |
7244 KB |
correct |
79 |
Correct |
247 ms |
7160 KB |
correct |
80 |
Correct |
245 ms |
7160 KB |
correct |
81 |
Correct |
208 ms |
6568 KB |
correct |
82 |
Correct |
254 ms |
7140 KB |
correct |
83 |
Correct |
195 ms |
4344 KB |
correct |
84 |
Correct |
59 ms |
4984 KB |
correct |
85 |
Correct |
56 ms |
4856 KB |
correct |
86 |
Correct |
42 ms |
3192 KB |
correct |
87 |
Correct |
36 ms |
2684 KB |
correct |
88 |
Correct |
31 ms |
1912 KB |
correct |
89 |
Correct |
30 ms |
1784 KB |
correct |
90 |
Correct |
28 ms |
1656 KB |
correct |
91 |
Correct |
16 ms |
504 KB |
correct |
92 |
Correct |
15 ms |
504 KB |
correct |
93 |
Correct |
53 ms |
4984 KB |
correct |
94 |
Correct |
41 ms |
3192 KB |
correct |
95 |
Correct |
48 ms |
3224 KB |
correct |
96 |
Correct |
30 ms |
1784 KB |
correct |
97 |
Correct |
32 ms |
2040 KB |
correct |
98 |
Correct |
35 ms |
2680 KB |
correct |
99 |
Correct |
32 ms |
1912 KB |
correct |
100 |
Correct |
20 ms |
760 KB |
correct |
101 |
Correct |
15 ms |
504 KB |
correct |
102 |
Correct |
52 ms |
3832 KB |
correct |
103 |
Correct |
53 ms |
3704 KB |
correct |
104 |
Correct |
52 ms |
3704 KB |
correct |
105 |
Correct |
52 ms |
3664 KB |
correct |