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 <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
using pii = pair<int, int>;
int tim[505], ct, ign;
bool vis[505];
vector<pii> adj[505];
vector<int> times[505];
void dfs(int x, vector<int> &to) {
vis[x] = 1;
for(pii e:adj[x]) {
if(e.first == ign) {
times[ct].push_back(e.second);
continue;
}
if(vis[e.first]) continue;
vis[e.first] = 1; tim[e.first] = ct;
to.push_back(e.second);
dfs(e.first, to);
}
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
std::vector<int> r;
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);
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) times[j].clear();
//with this node as the main node, generate a spanning tree ignoring this node
ign = i;
std::vector<int> toask;
memset(vis, 0, sizeof vis); vis[i] = 1;
memset(tim, -1, sizeof tim);
for(int j=0;j<n;j++) {
ct = j;
if(i != j && !vis[j]) dfs(j, toask);
}
int cq = 0;
//for(int j=0;j<n;j++) cout << tim[j] << ' '; cout << '\n';
for(int ctime = 0;ctime<n;ctime++) {
if(times[ctime].empty()) continue;
for(int ktime=0;ktime<n;ktime++) {
if(ktime == ctime) continue;
for(int e:times[ktime]) toask.push_back(e);
}
std::vector<int> inans;
int mx = -1;
for(int e:times[ctime]) {
toask.push_back(e);
if(cq++ >= 29000) exit(1);
int res = count_common_roads(toask);
if(res >= mx) {
if(res > mx) inans.clear();
mx = res;
inans.push_back(e);
}
toask.pop_back();
}
for(int ktime=0;ktime<n;ktime++) {
if(ktime == ctime) continue;
for(int e:times[ktime]) toask.pop_back();
}
for(int e:inans) r.push_back(e);
}
}
sort(r.begin(), r.end());
r.resize(unique(r.begin(), r.end())-r.begin());
return r;
}
/*
4 6
0 1
0 2
0 3
1 2
1 3
2 3
0 1 5
*/
Compilation message (stderr)
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:65:13: warning: unused variable 'e' [-Wunused-variable]
65 | for(int e:times[ktime]) toask.pop_back();
| ^
# | 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... |