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;
struct Dsu {
vector<int> pai, w;
int comp;
Dsu(int n) : pai(n), w(n, 1), comp(n) {
iota(pai.begin(), pai.end(), 0);
}
int find(int x) {
if (x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
void join(int x, int y) {
x = find(x), y = find(y);
if (w[x] < w[y]) swap(x, y);
pai[y] = x;
w[x] += w[y];
comp--;
}
bool con(int x, int y) {
return find(x) == find(y);
}
};
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
int m = u.size();
vector<int> golden(m);
for (int vtx = 0; vtx < n; vtx++) {
vector<pair<int, int>> num;
vector<int> r;
Dsu dsu(n);
for (int i = 0; i < m; i++) {
if (u[i] == vtx or v[i] == vtx) continue;
if (!dsu.con(u[i], v[i])) {
dsu.join(u[i], v[i]);
r.push_back(i);
}
}
for (int i = 0; i < m; i++) {
if (u[i] == vtx or v[i] == vtx) {
if (!dsu.con(u[i], v[i]) and dsu.comp == 2) {
r.push_back(i);
num.push_back({i, count_common_roads(r)});
r.pop_back();
}
}
}
int maxi = 0;
for (auto const& p : num) {
maxi = max(maxi, p.second);
}
for (auto const& p : num) {
if (p.second == maxi) {
golden[p.first] = 1;
}
}
}
vector<int> r;
Dsu dsu(n);
for (int i = 0; i < m; i++) {
if (golden[i]) {
r.push_back(i);
dsu.join(u[i], v[i]);
}
}
for (int i = 0; i < m; i++) {
if (!golden[i] and !dsu.con(u[i], v[i])) {
golden[i] = 1;
dsu.join(u[i], v[i]);
r.push_back(i);
}
}
return r;
}
# | 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... |