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;
int query(set<int> x) {
vector<int> edges;
for (int i: x) edges.push_back(i);
return count_common_roads(edges);
}
int n, m;
vector<vector<int>> graphList;
vector<vector<int>> graphMatrix;
vector<vector<int>> knownEdges;
struct solve {
vector<int> vis;
set<int> usedEdges;
void dfs(int v, int p, int forbidden, int col) {
if (vis[v]!=-1) return;
vis[v] = col;
if (p!=-1) {
usedEdges.insert(graphMatrix[v][p]);
}
for (int u: graphList[v]) {
if (u == forbidden) continue;
dfs(u, v, forbidden, col);
}
}
void start(int v) {
vis.assign(n, -1);
usedEdges.clear();
int col = 0;
vector<vector<int>> colors;
for (int u: graphList[v]) {
if (vis[u]==-1) {
dfs(u, -1, v, col);
col++;
colors.push_back({});
}
colors[vis[u]].push_back(u);
}
for (auto i: colors) usedEdges.insert(graphMatrix[v][i[0]]);
for (auto i: colors) {
vector<int> vals;
usedEdges.erase(graphMatrix[v][i[0]]);
int hi = -1;
for (int j: i) {
if (knownEdges[v][j] != -1 && hi == -1) {
usedEdges.insert(graphMatrix[v][j]);
vals.push_back(query(usedEdges));
usedEdges.erase(graphMatrix[v][j]);
if (knownEdges[v][j] == 1) hi = vals.back();
if (knownEdges[v][j] == 0) hi = vals.back()+1;
continue;
}
if (knownEdges[v][j] != -1 && hi != -1) {
if (knownEdges[v][j] == 1) vals.push_back(hi);
if (knownEdges[v][j] == 0) vals.push_back(hi-1);
continue;
}
usedEdges.insert(graphMatrix[v][j]);
vals.push_back(query(usedEdges));
usedEdges.erase(graphMatrix[v][j]);
}
usedEdges.insert(graphMatrix[v][i[0]]);
if (hi == -1) hi = *max_element(vals.begin(), vals.end());
for (int j = 0; j<(int)i.size(); j++) {
if (vals[j] == hi) {
knownEdges[i[j]][v] = 1;
knownEdges[v][i[j]] = 1;
}
else {
knownEdges[i[j]][v] = 0;
knownEdges[v][i[j]] = 0;
}
}
}
}
vector<int> output;
solve() {
for (int i = 0; i<n; i++) {
start(i);
}
output.clear();
for (int i = 0; i<n; i++) {
for (int j = i+1; j<n; j++) {
if (knownEdges[i][j]==0) continue;
if (knownEdges[i][j]==-1) continue;
// assert(knownEdges[i][j]==1);
output.push_back(graphMatrix[i][j]);
}
}
}
};
vector<int> find_roads(int _n, vector<int> u, vector<int> v) {
n = _n;
m = u.size();
graphList.assign(n, vector<int>());
graphMatrix.assign(n, vector<int>(n, -1));
knownEdges.assign(n, vector<int>(n, -1));
for (int i = 0; i<m; i++) {
graphList[u[i]].push_back(v[i]);
graphList[v[i]].push_back(u[i]);
graphMatrix[u[i]][v[i]] = i;
graphMatrix[v[i]][u[i]] = i;
}
solve sol;
return sol.output;
}
# | 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... |