This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define maxn 505
struct unionfind{
int rnk[maxn],par[maxn];
void init(int n){
for(int i=0;i<n;++i)par[i]=i,rnk[i]=1;
}
int fp(int i){return (par[i]==i)?i:par[i]=fp(par[i]);}
void join(int x,int y){
x=fp(x),y=fp(y);
if(x==y)return;
if(rnk[x]<rnk[y])par[x]=y;
else par[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
}
}ufds;
int num[maxn],low[maxn],par[maxn],cnt,bcnt,good[maxn*maxn];
bool in[maxn];
vector<pair<int,int>> AL[maxn];
vector<int> bcc[maxn];
stack<int> s;
void dfs(int u){
num[u]=low[u]=cnt++;
s.push(u);
for(auto[v,i]:AL[u]){
if(num[v]==-1){
par[v]=u;
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>=num[u]){
while(true){
int x=s.top();s.pop();
bcc[bcnt].push_back(x);
if(x==v)break;
}
bcc[bcnt].push_back(u);
++bcnt;
}
if(low[v]>num[u]){
good[i]=true;
}
}
else if(v!=par[u]){
low[u]=min(low[u],num[v]);
}
}
}
vector<int> find_roads(int n,vector<int> u,vector<int> v){
int m=u.size();
for(int i=0;i<m;++i){
AL[u[i]].push_back({v[i],i});
AL[v[i]].push_back({u[i],i});
}
memset(num,-1,sizeof num);
dfs(0);
for(int i=0;i<bcnt;++i){
if(bcc[i].size()==1)continue;
for(int x:bcc[i]){
in[x]=true;
}
for(int x:bcc[i]){
ufds.init(n);
vector<int> e,bcce;
for(int i=0;i<m;++i){
if((u[i]==x&&in[v[i]])||(v[i]==x&&in[u[i]])){
bcce.push_back(i);
continue;
}
if(ufds.fp(u[i])==ufds.fp(v[i]))continue;
e.push_back(i);
ufds.join(u[i],v[i]);
}
vector<pair<int,int>> res;
for(int i:bcce){
e.push_back(i);
if(e.size()!=n-1)assert(false);
res.push_back({count_common_roads(e),i});
e.pop_back();
}
sort(res.begin(),res.end());
int last=res.back().first;
for(int i=res.size()-1;i>=0;--i){
if(res[i].first==last){
good[res[i].second]=true;
}
}
}
for(int x:bcc[i])in[x]=false;
}
vector<int> ans;
for(int i=0;i<m;++i){
if(good[i])ans.push_back(i);
}
return ans;
}
Compilation message (stderr)
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:83:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
83 | if(e.size()!=n-1)assert(false);
| ~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |