#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
int N;
const int dydis = 251;
vector<pair<int, int> > brn;
vector<int> zn(dydis * dydis, -1);
vector<pair<int, int> > gr[dydis];
vector<int> tikrai;
int tevas[dydis];
int fP(int v){
if(tevas[v] == v) return v;
return tevas[v] = fP(tevas[v]);
}
void conn(int a, int b){
a = fP(a);
b = fP(b);
if(a == b) return;
tevas[a] = b;
}
set<int> virs;
bool vis[dydis] = {0};
int deg[dydis] = {0};
void form(){
for(int i = 0; i < N; i++) {
gr[i].clear();
vis[i] = 0;
deg[i] = 0;
}
virs.clear();
for(int i = 0; i < (int) brn.size(); i++){
if(zn[i] != -1) continue;
int a = fP(brn[i].first);
int b = fP(brn[i].second);
gr[a].push_back({b, i});
gr[b].push_back({a, i});
virs.insert(a); virs.insert(b);
}
}
vector<int> pl;
void dfs(int v){
vis[v] = 1;
for(auto x : gr[v]){
if(vis[x.first]) continue;
pl.push_back(x.second);
deg[v]++;
deg[x.first]++;
}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
//count_common_roads
N = n;
vector<int> ret;
for(int i = 0; i < N; i++) tevas[i] = i;
for(int i = 0; i < (int)u.size(); i++) {
brn.push_back({u[i], v[i]});
}
int it = 0;
while((int) tikrai.size() != n-1){
if(it++ > 10) break;
pl.clear();
form();
int v = *virs.begin();
dfs(v);
int u = -1;
for(int i = 0; i < n; i++) if(deg[i] == 1) u = i;
// cout << "komponentai: \n"; for(int i = 0; i < n; i++) cout << i << ": " << fP(i) << endl; cout << endl;
// cout << "suformuoju medi: \n"; for(int i = 0; i < pl.size(); i++) cout << brn[pl[i]].first << " - > " << brn[pl[i]].second << endl;
// cout << "\nmedzio lapas: " << u << endl;
// cout << endl;
vector<int> rest;
for(int i = 0; i < (int)pl.size(); i++){
int a = fP(brn[pl[i]].first);
int b = fP(brn[pl[i]].second);
if(a == u || b == u){
continue;
}
rest.push_back(pl[i]);
}
for(auto x : tikrai) rest.push_back(x);
vector<pair<int, int> > aa;
int mx = 0;
for(auto x : gr[u]){
rest.push_back(x.second);
int cm = count_common_roads(rest);
aa.push_back({cm, x.second});
mx = max(mx, cm);
rest.pop_back();
}
for(auto x : aa){
if(x.first == mx) {
tikrai.push_back(x.second);
zn[x.second] = 1;
conn(brn[x.second].first, brn[x.second].second);
//cout << brn[x.second].first << " -> " << brn[x.second].second << " tikrai geras" << endl;
}else{
zn[x.second] = 0;
// cout << brn[x.second].first << " -> " << brn[x.second].second << " tikrai blogas" << endl;
}
}
}
return tikrai;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
correct |
2 |
Incorrect |
1 ms |
460 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
correct |
2 |
Incorrect |
1 ms |
460 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
correct |
2 |
Incorrect |
1 ms |
460 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
correct |
2 |
Incorrect |
1 ms |
460 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
correct |
2 |
Incorrect |
1 ms |
460 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |