#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
using pii = pair<int, int>;
#define F first
#define S second
const int N = 510;
const int M = N*(N-1)/2;
const int LIM = 30000;
int n, m;
vector<int> U, V; // edge list
vector<pii> G[N]; // adj list
vector<int> T; // tree edges
vector<pii> GT[N]; // tree adj list
bool is_tree[M];
int ans[M]; // answer to each edge (-1 = unknown)
int defcnt; // answer for the default spanning tree
int tick;
pii disc[N], low[N];
void dfs(int u, int p)
{
disc[u] = low[u] = pii(++tick, -1);
for (auto v : G[u]) {
if (!disc[v.F].F) {
// push edge to tree
is_tree[v.S] = true;
T.push_back(v.S);
GT[u].emplace_back(v.F, v.S);
GT[v.F].emplace_back(u, v.S);
// tarjan's algorithm
dfs(v.F, u);
low[u] = min(low[u], low[v.F]);
if (low[v.F].F > disc[u].F)
ans[v.S] = 1;
} else if (v.F != p) {
low[u] = min(low[u], pii(disc[v.F].F, v.S));
}
}
}
vector<int> find_path(int u, int e, int t)
{
if (u == t) {
vector<int> ret;
ret.push_back(e);
return ret;
}
for (auto v : GT[u]) if (v.S != e) {
vector<int> ret = find_path(v.F, v.S, t);
if (!ret.empty()) {
if (e != -1)
ret.push_back(e);
return ret;
}
}
return vector<int>();
}
int get_changes(int rem, int add)
{
vector<int> q(T);
q.erase(find(q.begin(), q.end(), rem));
q.push_back(add);
return count_common_roads(q) - defcnt;
}
void query_cycle(int ce)
{
if (ans[ce] != -1) return;
int s = U[ce], t = V[ce];
vector<int> path = find_path(s, -1, t);
// find edge in path that we already know answer to
int fnd = -1;
for (auto e : path) {
if (ans[e] != -1) {
fnd = e;
break;
}
}
if (fnd != -1) { // case where we can quickly determine ce's value
ans[ce] = ans[fnd] + get_changes(fnd, ce);
for (auto e : path) { // update other edge in path we don't know
if (ans[e] == -1)
ans[e] = ans[ce] - get_changes(e, ce);
}
} else { // don't know, so query everything in the cycle
for (auto e : path) {
int d = get_changes(e, ce);
if (d == 1)
ans[ce] = 1, ans[e] = 0;
else if (d == -1)
ans[ce] = 0, ans[e] = 1;
}
if (ans[ce] == -1) ans[ce] = 0;
for (auto e : path) {
if (ans[e] == -1)
ans[e] = ans[ce];
}
}
}
int par[N];
int root(int u) {
if (par[u] == u) return u;
return par[u] = root(par[u]);
}
bool merge(int u, int v) {
u = root(u), v = root(v);
if (u == v) return false;
par[u] = v;
return true;
}
int query_adj(vector<int> &adj)
{
if (adj.empty()) return 0;
iota(par, par+n, 0);
vector<int> q;
for (auto e : adj) {
if (merge(U[e], V[e]))
q.push_back(e);
}
int cnt = 0;
for (auto e : T) {
if (merge(U[e], V[e])) {
cnt += ans[e];
q.push_back(e);
}
}
int ret = count_common_roads(q);
return ret-cnt;
}
void solve(vector<int> &adj, int cnt = -1)
{
if (cnt == -1)
cnt = query_adj(adj);
if (cnt == 0) {
for (auto e : adj)
ans[e] = 0;
return;
}
if (cnt == adj.size()) {
for (auto e : adj)
ans[e] = 1;
return;
}
int mid = adj.size()/2;
vector<int> L(adj.begin(), adj.begin()+mid);
vector<int> R(adj.begin()+mid, adj.end());
int lcnt = query_adj(L);
solve(L, lcnt);
solve(R, cnt-lcnt);
}
vector<int> find_roads(int _n, vector<int> _U, vector<int> _V)
{
// create adj list
n = _n;
U = _U, V = _V;
m = U.size();
for (int i = 0; i < m; ++i) {
int u = U[i], v = V[i];
G[u].emplace_back(v, i);
G[v].emplace_back(u, i);
}
// build dfs tree and find answer for the tree
fill(ans, ans+m, -1);
dfs(0, -1);
defcnt = count_common_roads(T);
for (int i = 0; i < n; ++i) {
if (low[i].S != -1)
query_cycle(low[i].S);
}
// find answer for each node
for (int u = 0; u < n; ++u) {
vector<int> adj;
for (auto v : G[u]) {
if (ans[v.S] == -1)
adj.push_back(v.S);
}
solve(adj);
}
// return answer
vector<int> ret;
for (int i = 0; i < m; ++i) {
if (ans[i])
ret.push_back(i);
}
return ret;
}
Compilation message
simurgh.cpp: In function 'void solve(std::vector<int>&, int)':
simurgh.cpp:150:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (cnt == adj.size()) {
~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
correct |
2 |
Correct |
4 ms |
612 KB |
correct |
3 |
Correct |
3 ms |
612 KB |
correct |
4 |
Correct |
3 ms |
644 KB |
correct |
5 |
Correct |
2 ms |
644 KB |
correct |
6 |
Correct |
3 ms |
644 KB |
correct |
7 |
Correct |
3 ms |
644 KB |
correct |
8 |
Correct |
2 ms |
644 KB |
correct |
9 |
Correct |
4 ms |
644 KB |
correct |
10 |
Correct |
2 ms |
720 KB |
correct |
11 |
Correct |
4 ms |
720 KB |
correct |
12 |
Correct |
3 ms |
720 KB |
correct |
13 |
Correct |
3 ms |
768 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
correct |
2 |
Correct |
4 ms |
612 KB |
correct |
3 |
Correct |
3 ms |
612 KB |
correct |
4 |
Correct |
3 ms |
644 KB |
correct |
5 |
Correct |
2 ms |
644 KB |
correct |
6 |
Correct |
3 ms |
644 KB |
correct |
7 |
Correct |
3 ms |
644 KB |
correct |
8 |
Correct |
2 ms |
644 KB |
correct |
9 |
Correct |
4 ms |
644 KB |
correct |
10 |
Correct |
2 ms |
720 KB |
correct |
11 |
Correct |
4 ms |
720 KB |
correct |
12 |
Correct |
3 ms |
720 KB |
correct |
13 |
Correct |
3 ms |
768 KB |
correct |
14 |
Correct |
5 ms |
768 KB |
correct |
15 |
Correct |
5 ms |
768 KB |
correct |
16 |
Correct |
5 ms |
768 KB |
correct |
17 |
Correct |
5 ms |
768 KB |
correct |
18 |
Correct |
3 ms |
768 KB |
correct |
19 |
Correct |
4 ms |
768 KB |
correct |
20 |
Correct |
4 ms |
768 KB |
correct |
21 |
Correct |
5 ms |
768 KB |
correct |
22 |
Correct |
4 ms |
816 KB |
correct |
23 |
Correct |
4 ms |
816 KB |
correct |
24 |
Correct |
4 ms |
816 KB |
correct |
25 |
Correct |
3 ms |
816 KB |
correct |
26 |
Correct |
4 ms |
816 KB |
correct |
27 |
Correct |
4 ms |
816 KB |
correct |
28 |
Correct |
4 ms |
816 KB |
correct |
29 |
Correct |
4 ms |
816 KB |
correct |
30 |
Correct |
4 ms |
816 KB |
correct |
31 |
Correct |
4 ms |
816 KB |
correct |
32 |
Correct |
4 ms |
816 KB |
correct |
33 |
Correct |
4 ms |
816 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
correct |
2 |
Correct |
4 ms |
612 KB |
correct |
3 |
Correct |
3 ms |
612 KB |
correct |
4 |
Correct |
3 ms |
644 KB |
correct |
5 |
Correct |
2 ms |
644 KB |
correct |
6 |
Correct |
3 ms |
644 KB |
correct |
7 |
Correct |
3 ms |
644 KB |
correct |
8 |
Correct |
2 ms |
644 KB |
correct |
9 |
Correct |
4 ms |
644 KB |
correct |
10 |
Correct |
2 ms |
720 KB |
correct |
11 |
Correct |
4 ms |
720 KB |
correct |
12 |
Correct |
3 ms |
720 KB |
correct |
13 |
Correct |
3 ms |
768 KB |
correct |
14 |
Correct |
5 ms |
768 KB |
correct |
15 |
Correct |
5 ms |
768 KB |
correct |
16 |
Correct |
5 ms |
768 KB |
correct |
17 |
Correct |
5 ms |
768 KB |
correct |
18 |
Correct |
3 ms |
768 KB |
correct |
19 |
Correct |
4 ms |
768 KB |
correct |
20 |
Correct |
4 ms |
768 KB |
correct |
21 |
Correct |
5 ms |
768 KB |
correct |
22 |
Correct |
4 ms |
816 KB |
correct |
23 |
Correct |
4 ms |
816 KB |
correct |
24 |
Correct |
4 ms |
816 KB |
correct |
25 |
Correct |
3 ms |
816 KB |
correct |
26 |
Correct |
4 ms |
816 KB |
correct |
27 |
Correct |
4 ms |
816 KB |
correct |
28 |
Correct |
4 ms |
816 KB |
correct |
29 |
Correct |
4 ms |
816 KB |
correct |
30 |
Correct |
4 ms |
816 KB |
correct |
31 |
Correct |
4 ms |
816 KB |
correct |
32 |
Correct |
4 ms |
816 KB |
correct |
33 |
Correct |
4 ms |
816 KB |
correct |
34 |
Correct |
35 ms |
2316 KB |
correct |
35 |
Correct |
35 ms |
2472 KB |
correct |
36 |
Correct |
32 ms |
2472 KB |
correct |
37 |
Correct |
11 ms |
2472 KB |
correct |
38 |
Correct |
34 ms |
2860 KB |
correct |
39 |
Correct |
45 ms |
2932 KB |
correct |
40 |
Correct |
26 ms |
2932 KB |
correct |
41 |
Correct |
34 ms |
3368 KB |
correct |
42 |
Correct |
35 ms |
3568 KB |
correct |
43 |
Correct |
16 ms |
3568 KB |
correct |
44 |
Correct |
22 ms |
3568 KB |
correct |
45 |
Correct |
16 ms |
3568 KB |
correct |
46 |
Correct |
16 ms |
3568 KB |
correct |
47 |
Correct |
11 ms |
3568 KB |
correct |
48 |
Correct |
5 ms |
3568 KB |
correct |
49 |
Correct |
6 ms |
3568 KB |
correct |
50 |
Correct |
14 ms |
3568 KB |
correct |
51 |
Correct |
17 ms |
3568 KB |
correct |
52 |
Correct |
15 ms |
3568 KB |
correct |
53 |
Correct |
14 ms |
3568 KB |
correct |
54 |
Correct |
18 ms |
3996 KB |
correct |
55 |
Correct |
15 ms |
3996 KB |
correct |
56 |
Correct |
18 ms |
3996 KB |
correct |
57 |
Correct |
17 ms |
4056 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4056 KB |
correct |
2 |
Correct |
3 ms |
4056 KB |
correct |
3 |
Correct |
93 ms |
7848 KB |
correct |
4 |
Correct |
140 ms |
10608 KB |
correct |
5 |
Correct |
213 ms |
11504 KB |
correct |
6 |
Correct |
135 ms |
12432 KB |
correct |
7 |
Correct |
104 ms |
13320 KB |
correct |
8 |
Correct |
133 ms |
14288 KB |
correct |
9 |
Correct |
150 ms |
15216 KB |
correct |
10 |
Correct |
161 ms |
16236 KB |
correct |
11 |
Correct |
161 ms |
17096 KB |
correct |
12 |
Correct |
153 ms |
18000 KB |
correct |
13 |
Correct |
2 ms |
18000 KB |
correct |
14 |
Correct |
167 ms |
18820 KB |
correct |
15 |
Correct |
161 ms |
19860 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
correct |
2 |
Correct |
4 ms |
612 KB |
correct |
3 |
Correct |
3 ms |
612 KB |
correct |
4 |
Correct |
3 ms |
644 KB |
correct |
5 |
Correct |
2 ms |
644 KB |
correct |
6 |
Correct |
3 ms |
644 KB |
correct |
7 |
Correct |
3 ms |
644 KB |
correct |
8 |
Correct |
2 ms |
644 KB |
correct |
9 |
Correct |
4 ms |
644 KB |
correct |
10 |
Correct |
2 ms |
720 KB |
correct |
11 |
Correct |
4 ms |
720 KB |
correct |
12 |
Correct |
3 ms |
720 KB |
correct |
13 |
Correct |
3 ms |
768 KB |
correct |
14 |
Correct |
5 ms |
768 KB |
correct |
15 |
Correct |
5 ms |
768 KB |
correct |
16 |
Correct |
5 ms |
768 KB |
correct |
17 |
Correct |
5 ms |
768 KB |
correct |
18 |
Correct |
3 ms |
768 KB |
correct |
19 |
Correct |
4 ms |
768 KB |
correct |
20 |
Correct |
4 ms |
768 KB |
correct |
21 |
Correct |
5 ms |
768 KB |
correct |
22 |
Correct |
4 ms |
816 KB |
correct |
23 |
Correct |
4 ms |
816 KB |
correct |
24 |
Correct |
4 ms |
816 KB |
correct |
25 |
Correct |
3 ms |
816 KB |
correct |
26 |
Correct |
4 ms |
816 KB |
correct |
27 |
Correct |
4 ms |
816 KB |
correct |
28 |
Correct |
4 ms |
816 KB |
correct |
29 |
Correct |
4 ms |
816 KB |
correct |
30 |
Correct |
4 ms |
816 KB |
correct |
31 |
Correct |
4 ms |
816 KB |
correct |
32 |
Correct |
4 ms |
816 KB |
correct |
33 |
Correct |
4 ms |
816 KB |
correct |
34 |
Correct |
35 ms |
2316 KB |
correct |
35 |
Correct |
35 ms |
2472 KB |
correct |
36 |
Correct |
32 ms |
2472 KB |
correct |
37 |
Correct |
11 ms |
2472 KB |
correct |
38 |
Correct |
34 ms |
2860 KB |
correct |
39 |
Correct |
45 ms |
2932 KB |
correct |
40 |
Correct |
26 ms |
2932 KB |
correct |
41 |
Correct |
34 ms |
3368 KB |
correct |
42 |
Correct |
35 ms |
3568 KB |
correct |
43 |
Correct |
16 ms |
3568 KB |
correct |
44 |
Correct |
22 ms |
3568 KB |
correct |
45 |
Correct |
16 ms |
3568 KB |
correct |
46 |
Correct |
16 ms |
3568 KB |
correct |
47 |
Correct |
11 ms |
3568 KB |
correct |
48 |
Correct |
5 ms |
3568 KB |
correct |
49 |
Correct |
6 ms |
3568 KB |
correct |
50 |
Correct |
14 ms |
3568 KB |
correct |
51 |
Correct |
17 ms |
3568 KB |
correct |
52 |
Correct |
15 ms |
3568 KB |
correct |
53 |
Correct |
14 ms |
3568 KB |
correct |
54 |
Correct |
18 ms |
3996 KB |
correct |
55 |
Correct |
15 ms |
3996 KB |
correct |
56 |
Correct |
18 ms |
3996 KB |
correct |
57 |
Correct |
17 ms |
4056 KB |
correct |
58 |
Correct |
2 ms |
4056 KB |
correct |
59 |
Correct |
3 ms |
4056 KB |
correct |
60 |
Correct |
93 ms |
7848 KB |
correct |
61 |
Correct |
140 ms |
10608 KB |
correct |
62 |
Correct |
213 ms |
11504 KB |
correct |
63 |
Correct |
135 ms |
12432 KB |
correct |
64 |
Correct |
104 ms |
13320 KB |
correct |
65 |
Correct |
133 ms |
14288 KB |
correct |
66 |
Correct |
150 ms |
15216 KB |
correct |
67 |
Correct |
161 ms |
16236 KB |
correct |
68 |
Correct |
161 ms |
17096 KB |
correct |
69 |
Correct |
153 ms |
18000 KB |
correct |
70 |
Correct |
2 ms |
18000 KB |
correct |
71 |
Correct |
167 ms |
18820 KB |
correct |
72 |
Correct |
161 ms |
19860 KB |
correct |
73 |
Correct |
2 ms |
19860 KB |
correct |
74 |
Correct |
184 ms |
20760 KB |
correct |
75 |
Correct |
140 ms |
21520 KB |
correct |
76 |
Correct |
83 ms |
21520 KB |
correct |
77 |
Correct |
146 ms |
22884 KB |
correct |
78 |
Correct |
165 ms |
23740 KB |
correct |
79 |
Correct |
170 ms |
24676 KB |
correct |
80 |
Correct |
163 ms |
25600 KB |
correct |
81 |
Correct |
117 ms |
25744 KB |
correct |
82 |
Correct |
167 ms |
27128 KB |
correct |
83 |
Correct |
114 ms |
27128 KB |
correct |
84 |
Correct |
72 ms |
27128 KB |
correct |
85 |
Correct |
61 ms |
27128 KB |
correct |
86 |
Correct |
56 ms |
27128 KB |
correct |
87 |
Correct |
47 ms |
27128 KB |
correct |
88 |
Correct |
35 ms |
27128 KB |
correct |
89 |
Correct |
42 ms |
27128 KB |
correct |
90 |
Correct |
36 ms |
27128 KB |
correct |
91 |
Correct |
24 ms |
27128 KB |
correct |
92 |
Correct |
10 ms |
27128 KB |
correct |
93 |
Correct |
81 ms |
28296 KB |
correct |
94 |
Correct |
48 ms |
28296 KB |
correct |
95 |
Correct |
48 ms |
28296 KB |
correct |
96 |
Correct |
34 ms |
28296 KB |
correct |
97 |
Correct |
35 ms |
28296 KB |
correct |
98 |
Correct |
38 ms |
28296 KB |
correct |
99 |
Correct |
38 ms |
28296 KB |
correct |
100 |
Correct |
25 ms |
28296 KB |
correct |
101 |
Correct |
12 ms |
28296 KB |
correct |
102 |
Correct |
67 ms |
29456 KB |
correct |
103 |
Correct |
65 ms |
29920 KB |
correct |
104 |
Correct |
60 ms |
30416 KB |
correct |
105 |
Correct |
61 ms |
30820 KB |
correct |