# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
127662 | Masterdan | Simurgh (IOI17_simurgh) | C++14 | 0 ms | 0 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>
#define MIN -1
#define MAX 1000000000
#define pb push_back
#define mp make _pair
#define all(a) a.begin (), a.end ()
using namespace std;
typedef long long int ll;
typedef vector <int> vi;
int p[500];
int m,n;
bool aux[500];
vector<pair<int,int>> g[500];
vector<int> u,v;
int findset(int x){
return x == p[x] ? x : p[x] = findset(p[x]);}
bool initset(vector<int> ans){
for(int i=0;i<n;i++)
p[i] = i;
for(int i=0;i<ans.size();i++){
int id = ans[i];
int a = u[id];
int b = v[id];
if(findset(a) == findset(b))
continue;
p[findset(b)] = findset(a);
}
for(int i=0;i<n;i++){
if(findset(i) != findset(0))
return 0;}
return 1;
}
vector<int> find_roads(int N, vector<int> U, vector<int> V) {
vector<int> ans;
u = U;
v = V;
n = N;
m = u.size();
for(int i=0;i<n;i++)
p[i] = i;
int lst;
for(int i=0;i<m;i++){
int a = u[i];
int b = v[i];
g[a].push_back({b,i});
g[b].push_back({a,i});
if(findset(a) == findset(b))
continue;
aux[i] = 1;
ans.push_back(i);
p[findset(b)] = findset(a);
}
lst = count_common_roads(ans);
for(int i=0;i<ans.size();i++){
int id = ans[i];
int a = u[id];
int b = v[id];
for(int j=0;j<m;j++){
if(aux[j])
continue;
ans[i] = j;
if(initset(ans) == 1){
int r = count_common_roads(ans);
if(r < lst){
ans[i] = id;
break;
}
if(r > lst){
lst = r;
aux[j] = 1;
aux[id] = 0;
break;
}
}
ans[i] = id;
}
}
return ans;
}