#include "simurgh.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct dsu{
int n;
vector<int> sz, p;
dsu(int _n): n(_n){
sz.resize(n, 1);
p.resize(n);
for(int i = 0; i < n; i++) p[i] = i;
}
int get(int x){
if(p[x] != x) p[x] = get(p[x]);
return p[x];
}
bool unite(int a, int b){
a = get(a), b = get(b);
if(a == b) return 0;
if(sz[b] > sz[a]) swap(a, b);
sz[a]+=sz[b];
sz[b] = 0;
p[b] = a;
return 1;
}
};
int n, m;
vector<int> U, V;
vector<int> royal;
vector<vector<pair<int, int>>> graph;
vector<int> generate_tree(vector<int> E){
dsu ds(n);
vector<int> T;
for(int i: E){
if(ds.unite(U[i], V[i])) T.pb(i);
else return {};
}
for(int i = 0; i < m; i++) if(ds.unite(U[i], V[i])) T.pb(i);
return T;
}
int replacement_edge(vector<int> E, int i){
dsu ds(n);
for(int j: E) if(j != i) ds.unite(U[j], V[j]);
for(int j = 0; j < m; j++) if(j != i && ds.unite(U[j], V[j])) return j;
return -1;
}
int count_extra_royal(vector<int> T, vector<int> C){
dsu ds(n);
int c = 0;
vector<int> q;
for(int i: C) ds.unite(U[i], V[i]), q.pb(i);
for(int i: T) if(ds.unite(U[i], V[i])) c+=max(royal[i], 0), q.pb(i);
return count_common_roads(q)-c;
}
vector<int> find_roads(int _n, vector<int> _U, vector<int> _V){
swap(n, _n), swap(U, _U), swap(V, _V);
m = U.size();
royal.assign(m, -1);
graph.resize(n);
for(int i = 0; i < m; i++){
graph[U[i]].pb({V[i], i});
graph[V[i]].pb({U[i], i});
}
vector<int> A, B;
int e = 0;
while(1){
vector<int> C = A;
for(int i: B) C.pb(i);
int ei = C.size();
C.pb(e);
if(C.size() == n-1){
int cnt = 0;
for(int i = 0; i < A.size(); i++) if(royal[A[i]] == 1) cnt++;
int x = count_common_roads(C);
for(int i: B) A.pb(i);
A = C;
for(int i: B) royal[i] = (x > cnt);
royal[e] = (x > cnt);
break;
}
auto T = generate_tree(C);
int i = replacement_edge(T, e);
if(i == -1){
for(int j: B) royal[j] = 1;
royal[e] = 1;
A.insert(A.end(), B.begin(), B.end());
B.clear();
A.pb(e);
if(ei == n-2) break;
e = T[ei+1];
continue;
}
int x = count_common_roads(T);
T[ei] = i;
int y = count_common_roads(T);
T[ei] = e;
if(x > y){
for(int j: B) royal[j] = 1;
royal[e] = 1;
royal[i] = 0;
A.insert(A.end(), B.begin(), B.end());
B.clear();
A.pb(e);
if(ei == n-2) break;
e = T[ei+1];
}
else if(x < y){
for(int j: B) royal[j] = 0;
royal[e] = 0;
royal[i] = 1;
A.insert(A.end(), B.begin(), B.end());
B.clear();
A.pb(e);
if(ei == n-2) break;
e = T[ei+1];
}
else{
if(royal[i] != -1){
for(int j: B) royal[j] = royal[i];
royal[e] = royal[i];
A.insert(A.end(), B.begin(), B.end());
B.clear();
A.pb(e);
if(ei == n-2) break;
e = T[ei+1];
continue;
}
C.pb(i);
auto T2 = generate_tree(C);
if(T2.empty()){
for(int j: B) royal[j] = 0;
royal[e] = 0;
royal[i] = 0;
A.insert(A.end(), B.begin(), B.end());
B.clear();
A.pb(e);
if(ei == n-2) break;
e = T[ei+1];
}
else{
B.pb(e);
e = i;
}
}
}
vector<int> ans;
for(int i = 0; i < m; i++) if(royal[i] == 1) ans.pb(i);
for(int nd = 0; nd < n; nd++){
vector<int> S;
for(auto [x, i]: graph[nd]) if(royal[i] == -1) S.pb(i);
if(S.empty()) continue;
int X = count_extra_royal(A, S);
while(X > 0){
int a = 0, b = int(S.size())-1;
while(a < b){
vector<int> s;
int md = (a+b)/2;
for(int j = 0; j <= md; j++) s.pb(S[j]);
int x = count_extra_royal(A, s);
if(x >= X) b = md;
else a = md+1;
}
royal[S[a]] = 1;
ans.pb(S[a]);
X--;
}
for(int i: S) if(royal[i] == -1) royal[i] = 0;
}
return ans;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:82:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
82 | if(C.size() == n-1){
| ~~~~~~~~~^~~~~~
simurgh.cpp:84:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < A.size(); i++) if(royal[A[i]] == 1) cnt++;
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
300 KB |
correct |
4 |
Incorrect |
1 ms |
212 KB |
WA in grader: NO |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
300 KB |
correct |
4 |
Incorrect |
1 ms |
212 KB |
WA in grader: NO |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
300 KB |
correct |
4 |
Incorrect |
1 ms |
212 KB |
WA in grader: NO |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
546 ms |
4316 KB |
correct |
4 |
Correct |
980 ms |
5984 KB |
correct |
5 |
Correct |
950 ms |
5936 KB |
correct |
6 |
Correct |
972 ms |
6104 KB |
correct |
7 |
Correct |
997 ms |
5972 KB |
correct |
8 |
Correct |
991 ms |
5996 KB |
correct |
9 |
Correct |
966 ms |
5968 KB |
correct |
10 |
Correct |
961 ms |
5860 KB |
correct |
11 |
Correct |
940 ms |
5964 KB |
correct |
12 |
Correct |
952 ms |
5980 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
951 ms |
5948 KB |
correct |
15 |
Correct |
950 ms |
5964 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
300 KB |
correct |
4 |
Incorrect |
1 ms |
212 KB |
WA in grader: NO |
5 |
Halted |
0 ms |
0 KB |
- |