#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;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
256 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
256 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
3 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
256 KB |
correct |
9 |
Correct |
2 ms |
256 KB |
correct |
10 |
Correct |
2 ms |
380 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
3 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
256 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
256 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
256 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
3 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
256 KB |
correct |
9 |
Correct |
2 ms |
256 KB |
correct |
10 |
Correct |
2 ms |
380 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
3 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
256 KB |
correct |
14 |
Correct |
10 ms |
376 KB |
correct |
15 |
Correct |
9 ms |
396 KB |
correct |
16 |
Correct |
9 ms |
376 KB |
correct |
17 |
Correct |
22 ms |
376 KB |
correct |
18 |
Correct |
16 ms |
416 KB |
correct |
19 |
Correct |
23 ms |
376 KB |
correct |
20 |
Correct |
19 ms |
504 KB |
correct |
21 |
Correct |
14 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
376 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
252 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
2 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
504 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
256 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
256 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
3 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
256 KB |
correct |
9 |
Correct |
2 ms |
256 KB |
correct |
10 |
Correct |
2 ms |
380 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
3 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
256 KB |
correct |
14 |
Correct |
10 ms |
376 KB |
correct |
15 |
Correct |
9 ms |
396 KB |
correct |
16 |
Correct |
9 ms |
376 KB |
correct |
17 |
Correct |
22 ms |
376 KB |
correct |
18 |
Correct |
16 ms |
416 KB |
correct |
19 |
Correct |
23 ms |
376 KB |
correct |
20 |
Correct |
19 ms |
504 KB |
correct |
21 |
Correct |
14 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
376 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
252 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
2 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
504 KB |
correct |
34 |
Incorrect |
216 ms |
1488 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
correct |
2 |
Correct |
2 ms |
256 KB |
correct |
3 |
Incorrect |
161 ms |
3832 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
256 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
256 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
3 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
256 KB |
correct |
9 |
Correct |
2 ms |
256 KB |
correct |
10 |
Correct |
2 ms |
380 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
3 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
256 KB |
correct |
14 |
Correct |
10 ms |
376 KB |
correct |
15 |
Correct |
9 ms |
396 KB |
correct |
16 |
Correct |
9 ms |
376 KB |
correct |
17 |
Correct |
22 ms |
376 KB |
correct |
18 |
Correct |
16 ms |
416 KB |
correct |
19 |
Correct |
23 ms |
376 KB |
correct |
20 |
Correct |
19 ms |
504 KB |
correct |
21 |
Correct |
14 ms |
376 KB |
correct |
22 |
Correct |
2 ms |
376 KB |
correct |
23 |
Correct |
2 ms |
376 KB |
correct |
24 |
Correct |
2 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
2 ms |
376 KB |
correct |
27 |
Correct |
2 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
252 KB |
correct |
30 |
Correct |
2 ms |
376 KB |
correct |
31 |
Correct |
2 ms |
376 KB |
correct |
32 |
Correct |
2 ms |
376 KB |
correct |
33 |
Correct |
2 ms |
504 KB |
correct |
34 |
Incorrect |
216 ms |
1488 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |