#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
int N;
set< int > B;
/*int count_common_roads(vector< int > ans){
assert((int)ans.size() == N-1);
int tot = 0;
for(int i = 0; i < (int)ans.size(); i++){
tot += B.count(ans[i]);
}
return tot;
}
*/
vector< int > U;
int getParents(int a){
if(U[a] == a) return a;
else return U[a] = getParents(U[a]);
}
void Union(int a, int b){
U[getParents(a)] = getParents(b);
}
int makeBetter(vector< tuple<int, int, int> > &golden, vector< tuple<int, int, int> > &rem, int start, int n){
/*cout << "GOLDEN ";
for(tuple<int, int, int> tup : golden) cout << get<2>(tup) << ' ';
cout << endl;
cout << "REM ";
for(tuple<int, int, int> tup : rem) cout << get<2>(tup) << ' ';
cout << endl;*/
for(int i = 0; i < n-1; i++){
for(int j = 0; j < n;j++){
U[j] = j;
}
vector< tuple<int, int, int> > tempo;
vector< int > ans;
for(int j = 0; j < n-1; j++){
if(j == i) continue;
assert(getParents(get<0>(golden[j])) != getParents(get<1>(golden[j])));
Union(get<0>(golden[j]), get<1>(golden[j]));
tempo.push_back(golden[j]);
ans.push_back(get<2>(golden[j]));
}
//cout << "BEFORE ";
/*for(tuple<int, int, int> tup : tempo) cout << get<2>(tup) << ' ';
cout << endl;*/
for(int j = 0; j < (int)rem.size(); j++){
if(getParents(get<0>(rem[j])) == getParents(get<1>(rem[j]))) continue;
ans.push_back(get<2>(rem[j]));
//cout << "WORK " << get<2>(rem[j]) << endl;
int x = count_common_roads(ans);
/*for(int a : ans) cout << a << ' ';
cout << endl;*/
if(x >= start){
golden = tempo;
golden.push_back(rem[j]);
swap(rem[j], rem.back());
rem.pop_back();
rem.push_back(golden[i]);
return x;
}
ans.pop_back();
}
}
return start;
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
U.resize(n);
for(int i = 0; i < n; i++){
U[i] = i;
}
vector< tuple<int, int, int> > golden;
vector< tuple<int, int, int> > rem;
for(int i = 0; i < (int)u.size(); i++){
if(getParents(u[i]) == getParents(v[i])){
rem.push_back(make_tuple(u[i], v[i], i));
continue;
}
golden.push_back(make_tuple(u[i], v[i], i));
Union(u[i], v[i]);
}
vector< int > ans;
for(int i = 0; i < n-1; i++){
ans.push_back(get<2>(golden[i]));
}
int start = count_common_roads(ans);
while(start < n-1){
start = makeBetter(golden, rem, start,n);
}
//cout << start << endl;
ans.clear();
for(int i = 0; i < n-1; i++){
ans.push_back(get<2>(golden[i]));
}
sort(ans.begin(), ans.end());
return ans;
}
/*
int main(){
cin >> N;
int m;
cin >> m;
vector< int > u(m);
vector< int > v(m);
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
u[i] = a;
v[i] = b;
}
for(int i = 0; i < N-1; i++){
int a;
cin >> a;
B.insert(a);
}
vector< int > REP = find_roads(N,u,v);
for(int a : REP) cout << a << ' ';
cout << endl;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
380 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
380 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
380 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Incorrect |
9 ms |
256 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
380 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |