#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> par, s;
void reset(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
s[i] = 1;
}
}
int root(int x) {
if (x == par[x]) return x;
return par[x] = root(par[x]);
}
void join(int a, int b) {
a = root(a);
b = root(b);
if (a == b) return;
if (s[a] < s[b]) swap(a, b);
par[b] = a;
s[a] += s[b];
}
bool is(int a, int b) {
return root(a) == root(b);
}
bool works(const vector<int> &span, const vector<int> &u, const vector<int> &v) {
for (auto to : span) {
const int st = u[to];
const int et = v[to];
if (is(st, et)) return false;
join(st, et);
}
return true;
}
const int MAX_N = 240 + 5;
vector<vector<pair<int, int>>> g(MAX_N);
bitset<MAX_N> vis;
vector<int> span;
void get_any(int node) {
vis[node] = 1;
for (auto [nxt, to] : g[node]) {
if (!vis[nxt]) {
vis[nxt] = 1;
span.emplace_back(to);
get_any(nxt);
}
}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
const int m = v.size();
const int t = n - 1;
par = s = vector<int>(n);
for (int i = 0; i < m; i++) {
const int st = u[i];
const int et = v[i];
g[st].emplace_back(et, i);
g[et].emplace_back(st, i);
}
get_any(0);
int score = count_common_roads(span);
if (score == t) {
return span;
}
vector<int> best_way;
for (auto &edge : span) {
int starting_edge = edge;
bool swap = false;
const int st = u[edge];
const int et = v[edge];
best_way.clear();
for (auto [nxt, w] : g[st]) {
best_way.emplace_back(w);
}
for (auto [nxt, w] : g[et]) {
best_way.emplace_back(w);
}
for (auto to : best_way) {
reset(n);
edge = to;
if(!works(span, u, v)) {
continue;
}
int nxt_score = count_common_roads(span);
if (nxt_score > score) {
score = nxt_score;
assert(edge == to);
swap = true;
break;
}
}
if (!swap) {
edge = starting_edge;
}
}
return span;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
correct |
2 |
Incorrect |
0 ms |
384 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
correct |
2 |
Incorrect |
0 ms |
384 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
correct |
2 |
Incorrect |
0 ms |
384 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
correct |
2 |
Incorrect |
1 ms |
288 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
correct |
2 |
Incorrect |
0 ms |
384 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |