#include "simurgh.h"
#include<bits/stdc++.h>
#define ii pair<int,int>
#define st first
#define nd second
#define pb push_back
#define iii pair<ii,int>
#define all(x) x.begin(),x.end()
#define ppb pop_back
#define ll long long
#define sz(x) ((int)x.size())
using namespace std;
int n,m,inus,ous;
int normal;
vector<int> inmst,path,ans;
int pos[505*505];
int ata[505],ok[505];
vector<iii> rem,e;
vector<ii> v[505];
int flag,reach;
int bul(int x) {
if(ata[x]==x) return x;
return ata[x]=bul(ata[x]);
}
void finish(int w) {
int bef=0;
vector<int> ress;
for(int i=0;i<sz(path);i++) {
if(bef && ok[path[i]]!=0) {
continue ;
}
bef=max(bef,ok[path[i]]);
swap(inmst[pos[path[i]]],w);
int cur=count_common_roads(inmst);
swap(inmst[pos[path[i]]],w);
ress.pb(cur);
if(cur>normal || (ok[path[i]]==1 && cur==normal)) {
flag=1;
}
if(cur<normal || (ok[path[i]]==2 && cur==normal)) {
flag=-1;
}
}
int pr=0;
if(flag==1) {
for(int i=0;i<sz(path);i++) {
if(ok[path[i]]) continue ;
if(ress[pr]==normal) {
ok[path[i]]=1;
ans.pb(path[i]);
}
else {
ok[path[i]]=2;
}
++pr;
}
}
else if(flag==-1) {
for(int i=0;i<sz(path);i++) {
if(ok[path[i]]) continue ;
if(ress[pr]==normal) {
ok[path[i]]=2;
}
else {
ok[path[i]]=1;
ans.pb(path[i]);
}
++pr;
}
flag=0;
}
else {
for(int i=0;i<sz(path);i++) {
ok[path[i]]=2;
}
}
}
void dfs(int node,int ata,int to,int w) {
if(reach) return ;
if(node==to) {
finish(w);
reach=1;
return ;
}
for(ii i:v[node]) {
if(i.st==ata) continue ;
path.pb(i.nd);
dfs(i.st,node,to,w);
path.ppb();
}
}
void make_mst() {
for(int i=0;i<n;i++) ata[i]=i;
for(auto i:e) {
if(bul(i.st.st)!=bul(i.st.nd)) {
inmst.pb(i.nd);
pos[i.nd]=sz(inmst)-1;
ata[bul(i.st.st)]=bul(i.st.nd);
v[i.st.st].pb({i.st.nd,i.nd});
v[i.st.nd].pb({i.st.st,i.nd});
}
else {
rem.pb(i);
}
}
sort(all(inmst));
normal=count_common_roads(inmst);
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
m=sz(u);
::n=n;
for(int i=0;i<m;i++) {
e.pb({{u[i],v[i]},i});
}
random_shuffle(all(e));
make_mst();
for(auto edge:rem) {
flag=0;
reach=0;
dfs(edge.st.st,-1,edge.st.nd,edge.nd);
if(flag) {
ans.pb(edge.nd);
}
if(sz(ans)==n-1) break ;
}
for(int i=0;i<sz(inmst);i++) if(!ok[inmst[i]]) ans.pb(inmst[i]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
248 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
248 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
248 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
484 KB |
correct |
2 |
Incorrect |
2 ms |
564 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
248 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |