# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
289496 | eohomegrownapps | Simurgh (IOI17_simurgh) | C++14 | 204 ms | 3204 KiB |
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;
//count_common_roads
vector<int> u;
vector<int> v;
vector<int> royal; // -1
int n;
int e;
//int count_common_roads(vector<int> &r)
struct UFDS{
int n;
vector<int> parent;
UFDS(int _n){
n = _n;
parent.resize(n);
for (int i = 0; i<n; i++){
parent[i]=i;
}
}
int root(int i){
if (parent[i]==i){return i;}
return parent[i]=root(parent[i]);
}
void connect(int a, int b){
int ra = root(a);
int rb = root(b);
if (ra==rb){return;}
parent[ra]=rb;
}
bool connected(int a, int b){
return root(a) == root(b);
}
};
std::vector<int> find_roads(int N, std::vector<int> U, std::vector<int> V) {
n=N; u=U; v=V; e=u.size();
royal.resize(e,-1);
vector<int> ans;
while (ans.size()<n-1){
/*cout<<"royal: ";
for (int i : ans){
cout<<i<<' ';
}cout<<endl;*/
UFDS ufds(n);
// add all royal roads first
vector<int> mst;
for (int i = 0; i<e; i++){
if (royal[i]==1){
mst.push_back(i);
ufds.connect(u[i],v[i]);
}
}
for (int i = 0; i<e; i++){
if (mst.size()==n-2){
break;
}
if (royal[i]==-1 && !ufds.connected(u[i],v[i])){
mst.push_back(i);
ufds.connect(u[i],v[i]);
}
}
/*for (int i : mst){
cout<<i<<' ';
}cout<<endl;*/
// try all edges
vector<int> edges;
vector<int> values;
int maxval = 0;
for (int i = 0; i<e; i++){
if (royal[i]!=-1){continue;}
if (ufds.connected(u[i],v[i])){continue;}
// try connecting edge
mst.push_back(i);
values.push_back(count_common_roads(mst));
mst.pop_back();
edges.push_back(i);
maxval=max(maxval,values.back());
}
for (int i = 0; i<edges.size(); i++){
if (values[i]==maxval){
royal[edges[i]]=1;
ans.push_back(edges[i]);
} else {
royal[edges[i]]=0;
}
}
}
return ans;
}
Compilation message (stderr)
# | 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... |