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 maxn = 5e2 + 20;
const int maxm = maxn * maxn / 2;
vector<pair<int, int>> adj[maxn];
bool tree[maxm];
int flag[maxm];
int comp[maxn];
void dfs1(int u) {
flag[u] = true;
for (auto e: adj[u]) {
int v = e.first;
int id = e.second;
if (!flag[v]) {
tree[id] = true;
dfs1(v);
}
}
}
void dfs2(int u, int p) {
comp[u] = 1;
for (auto e: adj[u]) {
int v = e.first;
int id = e.second;
if (tree[id] && v != p) {
dfs2(v, u);
}
}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = u.size();
for (int i = 0; i < m; i++) {
adj[u[i]].emplace_back(v[i], i);
adj[v[i]].emplace_back(u[i], i);
}
dfs1(0);
for (int i = 0; i < m; i++) {
flag[i] = -1;
}
vector<int> res;
for (int i = 0; i < m; i++) {
if (tree[i]) {
res.push_back(i);
}
}
while (true) {
for (int i = 0; i < n - 1; i++) {
if (flag[res[i]] == -1) {
tree[res[i]] = false;
res.erase(res.begin() + i);
break;
}
}
if ((int)res.size() == n - 1) {
break;
}
for (int i = 0; i < n; i++) {
comp[i] = 0;
}
dfs2(0, -1);
vector<pair<int, int>> cnt;
int good = -1;
int mx = 0;
for (int i = 0; i < m; i++) {
if (comp[u[i]] != comp[v[i]] && flag[i] == 1) {
good = i;
break;
}
}
if (good == -1) {
for (int i = 0; i < m; i++) {
if (comp[u[i]] != comp[v[i]] && flag[i] == -1) {
res.push_back(i);
cnt.emplace_back(count_common_roads(res), i);
mx = max(mx, cnt.back().first);
res.pop_back();
}
}
for (auto p: cnt) {
if (p.first == mx) {
good = p.second;
flag[p.second] = 1;
}
else {
flag[p.second] = 0;
}
}
}
tree[good] = true;
res.push_back(good);
}
return res;
}
# | 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... |