This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500 + 5;
vector<vector<pair<int, int>>> g(MAX_N);
bitset<MAX_N> vis;
bitset<MAX_N * MAX_N> invalid;
bitset<MAX_N * MAX_N> is_op;
vector<int> span, par, s, best_way, u, v;
int n;
void reset() {
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() {
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;
}
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) {
::n = n;
::u = u;
::v = 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;
}
for (auto &edge : span) {
int starting_edge = edge;
bool swap = false;
reset();
best_way.clear();
for (auto add_back : span) {
if (add_back == edge) continue;
join(u[add_back], v[add_back]);
}
for (int i = 0; i < m; i++) {
const int st = u[i];
const int et = v[i];
if (is(st, et) == false && !invalid[i]) {
best_way.emplace_back(i);
}
}
//~ for (auto to :
vector<pair<int, int>> sc;
for (auto i : best_way) {
edge = i;
assert(!invalid[i]);
int nxt_score = count_common_roads(span);
sc.emplace_back(edge, nxt_score);
if (nxt_score > score) {
score = nxt_score;
swap = true;
break;
}
if (nxt_score < score) {
invalid[i] = 1;
}
}
if (score == t) {
return span;
}
if (!swap) {
edge = starting_edge;
for (auto [nxt, res] : sc) {
if (res < score) {
invalid[nxt] = 1;
}
}
} else {
for (auto [nxt, res] : sc) {
if (res <= score) {
invalid[nxt] = 1;
}
}
}
}
return span;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |