#include "simurgh.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 5e2 + 10, M = N * N / 2;
int d[M],p[N],f[N],h[N],pe[N],mt,P[N];
vector<pair<int,int>> adj[N];
vector<int> rt,V,U,ed[N];
int gp(int x){
return P[x]==-1?x:P[x]=gp(P[x]);
}
bool uni(int v, int u){
v=gp(v);
u=gp(u);
if(v==u) return 0;
P[u]=v;
return 1;
}
void dfs(int v){
f[v]=1;
for(auto [u,i] : adj[v]){
if(f[u]) continue;
h[u]=h[v]+1;
p[u]=v;
pe[u]=i;
rt.pb(i);
dfs(u);
}
if(!v) return;
}
void sfd(int v){
int bk=-1,bu;
for(auto [u,i] : adj[v]){
if(h[u]<h[v]-1){
bk=i;
bu=u;
}else if(p[u]==v) sfd(u);
}
if(v==0) return;
if(bk==-1){
d[pe[v]]=1;
return;
}
int bv=v;
if(d[pe[v]]!=-1) return;
vector<int> v1,v2,t;
while(bv!=bu){
v1.pb(pe[bv]);
bv=p[bv];
}
for(int i : v1){
if(d[i]!=-1){
if(d[bk]==-1){
t={bk};
for(int j : rt) if(j!=i) t.pb(j);
int k=count_common_roads(t);
if(k==mt) d[bk]=d[i];
else d[bk]=1-d[i];
}
v2.pb(0);
}else{
t={bk};
for(int j : rt) if(j!=i) t.pb(j);
int k=count_common_roads(t);
if(k==mt){
v2.pb(0);
}else{
v2.pb(1);
if(k>mt) d[bk]=1;
else d[bk]=0;
}
}
}
if(d[bk]==-1) d[bk]=0;
for(int i=0; i<(int)v2.size(); i++){
if(d[v1[i]]!=-1) continue;
if(v2[i]) d[v1[i]]=1-d[bk];
else d[v1[i]]=d[bk];
}
}
int jask(vector<int> vec){
memset(P,-1,sizeof P);
vector<int> t;
int ans=0;
for(int i : vec){
uni(V[i],U[i]);
t.pb(i);
}
for(int i : rt){
if(uni(V[i],U[i])){
t.pb(i);
ans-=d[i];
}
}
return ans+count_common_roads(t);
}
void solve(vector<int> vec, int x){
int k=vec.size();
if(k==1){
d[vec[0]]=x;
return;
}
if(x==0){
for(int i : vec) d[i]=0;
return;
}
vector<int> ls,rs;
for(int i=0; i<k; i++){
if(i<k/2) ls.pb(vec[i]);
else rs.pb(vec[i]);
}
int y=jask(ls);
solve(ls,y);
solve(rs,x-y);
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m=v.size();
V=v;
U=u;
memset(d,-1,sizeof d);
for(int i=0; i<m; i++){
adj[u[i]].pb({v[i],i});
adj[v[i]].pb({u[i],i});
ed[abs(V[i]-U[i])].pb(i);
}
p[0]=-1;
dfs(0);
mt=count_common_roads(rt);
sfd(0);
for(int i=1; i<n; i++){
if(ed[i].empty()) continue;
solve(ed[i],jask(ed[i]));
}
vector<int> ans;
for(int i=0; i<m; i++) if(d[i]==1) ans.pb(i);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
correct |
2 |
Incorrect |
1 ms |
852 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |