#include "simurgh.h"
#include <bits/stdc++.h>
#define DEBUG 1
using namespace std;
namespace output{
void __(short x){cout<<x;}
void __(unsigned x){cout<<x;}
void __(int x){cout<<x;}
void __(long long x){cout<<x;}
void __(unsigned long long x){cout<<x;}
void __(double x){cout<<x;}
void __(long double x){cout<<x;}
void __(char x){cout<<x;}
void __(const char*x){cout<<x;}
void __(const string&x){cout<<x;}
void __(bool x){cout<<(x?"true":"false");}
template<class S,class T>
void __(const pair<S,T>&x){__(DEBUG?"(":""),__(x.first),__(DEBUG?", ":" "),__(x.second),__(DEBUG?")":"");}
template<class T>
void __(const vector<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
template<class T>
void __(const set<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
template<class T>
void __(const multiset<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
template<class S,class T>
void __(const map<S,T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
void pr(){cout<<"\n";}
template<class S,class... T>
void pr(const S&a,const T&...b){__(a);if(sizeof...(b))__(' ');pr(b...);}
}
using namespace output;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vl;
#define pb push_back
#define eb emplace_back
#define fox(i,x,y) for(int i=(x);i<=(y);i++)
#define foxr(i,x,y) for(int i=(y);i>=(x);i--)
#define F first
#define S second
const int MN = 505;
int N, M, i, x, y, bk[MN], par[MN], id[MN], use[MN], vs[MN], val[MN*MN], dep[MN], ds[MN];
vector<pii> adj[MN], tr[MN]; vi wew, ans, tmp, a, b; pii meh[MN];
void dfs(int n,int p){
vs[n] = 1;
meh[n] = {1<<30, 0};
for(auto v : adj[n]){
if(vs[v.F]){
if(v.F==p) continue;
if(dep[v.F]<meh[n].F) meh[n]={dep[v.F],v.S};
}
else{
dep[v.F] = dep[n]+1;
par[v.F] = n;
id[v.F] = v.S;
dfs(v.F, n);
wew.pb(v.S);
tr[n].pb(v);
tr[v.F].pb({n,v.S});
if(meh[v.F].F<meh[n].F) meh[n]=meh[v.F];
}
}
if(meh[n].F<dep[n]) bk[n]=meh[n].S;
}
inline void ini(){tmp.clear(); for(int _=0;_<N;_++)ds[_]=_;}
int fnd(int x){return ds[x]=x==ds[x]?x:fnd(ds[x]);}
inline bool uni(int x,int y){
x=fnd(x), y=fnd(y);
if(x!=y){ds[x]=y; return 1;}
else return 0;
}
inline int treeify(){
int res=0;
for(auto v : wew){
if(uni(a[v],b[v])){
tmp.pb(v);
res += val[v];
}
}
return res;
}
vi find_roads(int n,vi u,vi v){
N = n; M = (int)u.size();
for(i=0;i<M;i++){
adj[u[i]].pb({v[i],i});
adj[v[i]].pb({u[i],i});
}
a=u, b=v;
memset(bk,-1,sizeof(bk));
dfs(0,-1);
memset(val, -1, sizeof(val));
int init = count_common_roads(wew);
for(i=1;i<N;i++){
if(bk[i]==-1) val[id[i]]=1;
else{
if(val[bk[i]]==-1){
int ed = bk[i];
x=u[ed], y=v[ed];
if(dep[x]<dep[y]) swap(x,y);
int dn = -1; vi acc;
while(x!=y){
if(~val[id[x]]){
dn = id[x];
break;
}
acc.pb(id[x]);
x = par[x];
}
if(~dn){
wew.erase(find(wew.begin(),wew.end(),dn));
wew.pb(bk[i]);
int res = count_common_roads(wew);
wew.erase(find(wew.begin(),wew.end(),bk[i]));
wew.pb(dn);
val[bk[i]] = res-init+val[dn];
}
else{
int mx = init;
vi res;
for(auto v : acc){
wew.erase(find(wew.begin(),wew.end(),v));
wew.pb(bk[i]);
res.pb(count_common_roads(wew));
mx = max(res.back(), mx);
wew.pop_back();
wew.pb(v);
}
int fl=0;
for(auto v : res){
if(v!=mx) fl=1;
}
if(fl){
for(int j=0;j<(int)acc.size();j++){
if(res[j]!=mx) val[acc[j]]=1;
else val[acc[j]]=0;
}
if(init!=mx) val[bk[i]]=1;
else val[bk[i]]=0;
}
else{
for(int j=0;j<(int)acc.size();j++)
val[acc[j]]=0;
val[bk[i]]=0;
}
}
}
if(val[id[i]]==-1){
wew.erase(find(wew.begin(),wew.end(),id[i]));
wew.pb(bk[i]);
int res = count_common_roads(wew);
wew.erase(find(wew.begin(),wew.end(),bk[i]));
wew.pb(id[i]);
val[id[i]] = init-res+val[bk[i]];
}
}
}
for(i=0;i<M;i++){
if(val[i]==1) ans.pb(i);
}
for(i=0;i<N;i++){
vi vec;
for(auto v : adj[i]){
if(val[v.S]==-1)
vec.pb(v.S);
}
if(vec.size()){
ini();
for(auto v : vec){
tmp.pb(v);
uni(a[v],b[v]);
}
int r = treeify();
int k = count_common_roads(tmp)-r;
while(k--){
int lo=0, hi=(int)vec.size()-1;
while(lo<hi){
int mid=(lo+hi)>>1;
ini();
for(int j=mid+1;j<=hi;j++){
tmp.pb(vec[j]);
uni(a[vec[j]],b[vec[j]]);
}
int r = treeify();
if(count_common_roads(tmp)-r>0) lo=mid+1;
else hi=mid;
}
val[vec[lo]]=1;
ans.pb(vec[lo]);
for(int j=lo+1;j<(int)vec.size();j++)
val[vec[j]]=0;
vec.erase(vec.begin()+lo,vec.end());
}
}
}
//pr(">",ans);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
correct |
2 |
Correct |
6 ms |
1408 KB |
correct |
3 |
Correct |
5 ms |
1408 KB |
correct |
4 |
Correct |
5 ms |
1408 KB |
correct |
5 |
Correct |
5 ms |
1408 KB |
correct |
6 |
Correct |
5 ms |
1408 KB |
correct |
7 |
Correct |
5 ms |
1408 KB |
correct |
8 |
Correct |
5 ms |
1408 KB |
correct |
9 |
Correct |
5 ms |
1408 KB |
correct |
10 |
Correct |
5 ms |
1408 KB |
correct |
11 |
Correct |
5 ms |
1408 KB |
correct |
12 |
Correct |
5 ms |
1408 KB |
correct |
13 |
Correct |
5 ms |
1408 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
correct |
2 |
Correct |
6 ms |
1408 KB |
correct |
3 |
Correct |
5 ms |
1408 KB |
correct |
4 |
Correct |
5 ms |
1408 KB |
correct |
5 |
Correct |
5 ms |
1408 KB |
correct |
6 |
Correct |
5 ms |
1408 KB |
correct |
7 |
Correct |
5 ms |
1408 KB |
correct |
8 |
Correct |
5 ms |
1408 KB |
correct |
9 |
Correct |
5 ms |
1408 KB |
correct |
10 |
Correct |
5 ms |
1408 KB |
correct |
11 |
Correct |
5 ms |
1408 KB |
correct |
12 |
Correct |
5 ms |
1408 KB |
correct |
13 |
Correct |
5 ms |
1408 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
7 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
6 ms |
1408 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
6 ms |
1408 KB |
correct |
21 |
Correct |
6 ms |
1408 KB |
correct |
22 |
Correct |
6 ms |
1408 KB |
correct |
23 |
Correct |
6 ms |
1408 KB |
correct |
24 |
Correct |
6 ms |
1408 KB |
correct |
25 |
Correct |
5 ms |
1408 KB |
correct |
26 |
Correct |
5 ms |
1408 KB |
correct |
27 |
Correct |
6 ms |
1280 KB |
correct |
28 |
Correct |
5 ms |
1408 KB |
correct |
29 |
Correct |
6 ms |
1408 KB |
correct |
30 |
Correct |
6 ms |
1408 KB |
correct |
31 |
Correct |
6 ms |
1408 KB |
correct |
32 |
Correct |
6 ms |
1408 KB |
correct |
33 |
Correct |
6 ms |
1408 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
correct |
2 |
Correct |
6 ms |
1408 KB |
correct |
3 |
Correct |
5 ms |
1408 KB |
correct |
4 |
Correct |
5 ms |
1408 KB |
correct |
5 |
Correct |
5 ms |
1408 KB |
correct |
6 |
Correct |
5 ms |
1408 KB |
correct |
7 |
Correct |
5 ms |
1408 KB |
correct |
8 |
Correct |
5 ms |
1408 KB |
correct |
9 |
Correct |
5 ms |
1408 KB |
correct |
10 |
Correct |
5 ms |
1408 KB |
correct |
11 |
Correct |
5 ms |
1408 KB |
correct |
12 |
Correct |
5 ms |
1408 KB |
correct |
13 |
Correct |
5 ms |
1408 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
7 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
6 ms |
1408 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
6 ms |
1408 KB |
correct |
21 |
Correct |
6 ms |
1408 KB |
correct |
22 |
Correct |
6 ms |
1408 KB |
correct |
23 |
Correct |
6 ms |
1408 KB |
correct |
24 |
Correct |
6 ms |
1408 KB |
correct |
25 |
Correct |
5 ms |
1408 KB |
correct |
26 |
Correct |
5 ms |
1408 KB |
correct |
27 |
Correct |
6 ms |
1280 KB |
correct |
28 |
Correct |
5 ms |
1408 KB |
correct |
29 |
Correct |
6 ms |
1408 KB |
correct |
30 |
Correct |
6 ms |
1408 KB |
correct |
31 |
Correct |
6 ms |
1408 KB |
correct |
32 |
Correct |
6 ms |
1408 KB |
correct |
33 |
Correct |
6 ms |
1408 KB |
correct |
34 |
Correct |
40 ms |
2560 KB |
correct |
35 |
Correct |
41 ms |
2680 KB |
correct |
36 |
Correct |
34 ms |
2304 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
43 ms |
2560 KB |
correct |
39 |
Correct |
38 ms |
2560 KB |
correct |
40 |
Correct |
36 ms |
2304 KB |
correct |
41 |
Correct |
45 ms |
2560 KB |
correct |
42 |
Correct |
40 ms |
2560 KB |
correct |
43 |
Correct |
22 ms |
2176 KB |
correct |
44 |
Correct |
19 ms |
1920 KB |
correct |
45 |
Correct |
20 ms |
1920 KB |
correct |
46 |
Correct |
19 ms |
1920 KB |
correct |
47 |
Correct |
14 ms |
1580 KB |
correct |
48 |
Correct |
8 ms |
1408 KB |
correct |
49 |
Correct |
11 ms |
1536 KB |
correct |
50 |
Correct |
15 ms |
1664 KB |
correct |
51 |
Correct |
22 ms |
1920 KB |
correct |
52 |
Correct |
20 ms |
1920 KB |
correct |
53 |
Correct |
19 ms |
1920 KB |
correct |
54 |
Correct |
21 ms |
2176 KB |
correct |
55 |
Correct |
19 ms |
1920 KB |
correct |
56 |
Correct |
19 ms |
1920 KB |
correct |
57 |
Correct |
19 ms |
1920 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
correct |
2 |
Correct |
5 ms |
1408 KB |
correct |
3 |
Correct |
112 ms |
5120 KB |
correct |
4 |
Correct |
180 ms |
6648 KB |
correct |
5 |
Correct |
187 ms |
6648 KB |
correct |
6 |
Correct |
171 ms |
6520 KB |
correct |
7 |
Correct |
165 ms |
6520 KB |
correct |
8 |
Correct |
173 ms |
6520 KB |
correct |
9 |
Correct |
178 ms |
6520 KB |
correct |
10 |
Correct |
189 ms |
6648 KB |
correct |
11 |
Correct |
184 ms |
6520 KB |
correct |
12 |
Correct |
194 ms |
6520 KB |
correct |
13 |
Correct |
5 ms |
1408 KB |
correct |
14 |
Correct |
172 ms |
6520 KB |
correct |
15 |
Correct |
187 ms |
6652 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
correct |
2 |
Correct |
6 ms |
1408 KB |
correct |
3 |
Correct |
5 ms |
1408 KB |
correct |
4 |
Correct |
5 ms |
1408 KB |
correct |
5 |
Correct |
5 ms |
1408 KB |
correct |
6 |
Correct |
5 ms |
1408 KB |
correct |
7 |
Correct |
5 ms |
1408 KB |
correct |
8 |
Correct |
5 ms |
1408 KB |
correct |
9 |
Correct |
5 ms |
1408 KB |
correct |
10 |
Correct |
5 ms |
1408 KB |
correct |
11 |
Correct |
5 ms |
1408 KB |
correct |
12 |
Correct |
5 ms |
1408 KB |
correct |
13 |
Correct |
5 ms |
1408 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
7 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
6 ms |
1408 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
6 ms |
1408 KB |
correct |
21 |
Correct |
6 ms |
1408 KB |
correct |
22 |
Correct |
6 ms |
1408 KB |
correct |
23 |
Correct |
6 ms |
1408 KB |
correct |
24 |
Correct |
6 ms |
1408 KB |
correct |
25 |
Correct |
5 ms |
1408 KB |
correct |
26 |
Correct |
5 ms |
1408 KB |
correct |
27 |
Correct |
6 ms |
1280 KB |
correct |
28 |
Correct |
5 ms |
1408 KB |
correct |
29 |
Correct |
6 ms |
1408 KB |
correct |
30 |
Correct |
6 ms |
1408 KB |
correct |
31 |
Correct |
6 ms |
1408 KB |
correct |
32 |
Correct |
6 ms |
1408 KB |
correct |
33 |
Correct |
6 ms |
1408 KB |
correct |
34 |
Correct |
40 ms |
2560 KB |
correct |
35 |
Correct |
41 ms |
2680 KB |
correct |
36 |
Correct |
34 ms |
2304 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
43 ms |
2560 KB |
correct |
39 |
Correct |
38 ms |
2560 KB |
correct |
40 |
Correct |
36 ms |
2304 KB |
correct |
41 |
Correct |
45 ms |
2560 KB |
correct |
42 |
Correct |
40 ms |
2560 KB |
correct |
43 |
Correct |
22 ms |
2176 KB |
correct |
44 |
Correct |
19 ms |
1920 KB |
correct |
45 |
Correct |
20 ms |
1920 KB |
correct |
46 |
Correct |
19 ms |
1920 KB |
correct |
47 |
Correct |
14 ms |
1580 KB |
correct |
48 |
Correct |
8 ms |
1408 KB |
correct |
49 |
Correct |
11 ms |
1536 KB |
correct |
50 |
Correct |
15 ms |
1664 KB |
correct |
51 |
Correct |
22 ms |
1920 KB |
correct |
52 |
Correct |
20 ms |
1920 KB |
correct |
53 |
Correct |
19 ms |
1920 KB |
correct |
54 |
Correct |
21 ms |
2176 KB |
correct |
55 |
Correct |
19 ms |
1920 KB |
correct |
56 |
Correct |
19 ms |
1920 KB |
correct |
57 |
Correct |
19 ms |
1920 KB |
correct |
58 |
Correct |
5 ms |
1408 KB |
correct |
59 |
Correct |
5 ms |
1408 KB |
correct |
60 |
Correct |
112 ms |
5120 KB |
correct |
61 |
Correct |
180 ms |
6648 KB |
correct |
62 |
Correct |
187 ms |
6648 KB |
correct |
63 |
Correct |
171 ms |
6520 KB |
correct |
64 |
Correct |
165 ms |
6520 KB |
correct |
65 |
Correct |
173 ms |
6520 KB |
correct |
66 |
Correct |
178 ms |
6520 KB |
correct |
67 |
Correct |
189 ms |
6648 KB |
correct |
68 |
Correct |
184 ms |
6520 KB |
correct |
69 |
Correct |
194 ms |
6520 KB |
correct |
70 |
Correct |
5 ms |
1408 KB |
correct |
71 |
Correct |
172 ms |
6520 KB |
correct |
72 |
Correct |
187 ms |
6652 KB |
correct |
73 |
Correct |
5 ms |
1408 KB |
correct |
74 |
Correct |
213 ms |
6648 KB |
correct |
75 |
Correct |
195 ms |
6520 KB |
correct |
76 |
Correct |
92 ms |
3328 KB |
correct |
77 |
Correct |
185 ms |
6648 KB |
correct |
78 |
Correct |
180 ms |
6520 KB |
correct |
79 |
Correct |
180 ms |
6520 KB |
correct |
80 |
Correct |
190 ms |
6520 KB |
correct |
81 |
Correct |
151 ms |
6008 KB |
correct |
82 |
Correct |
181 ms |
6392 KB |
correct |
83 |
Correct |
135 ms |
4352 KB |
correct |
84 |
Correct |
94 ms |
5016 KB |
correct |
85 |
Correct |
81 ms |
4736 KB |
correct |
86 |
Correct |
76 ms |
3456 KB |
correct |
87 |
Correct |
61 ms |
3172 KB |
correct |
88 |
Correct |
57 ms |
2560 KB |
correct |
89 |
Correct |
59 ms |
2552 KB |
correct |
90 |
Correct |
50 ms |
2432 KB |
correct |
91 |
Correct |
27 ms |
1536 KB |
correct |
92 |
Correct |
17 ms |
1408 KB |
correct |
93 |
Correct |
82 ms |
4812 KB |
correct |
94 |
Correct |
67 ms |
3456 KB |
correct |
95 |
Correct |
66 ms |
3704 KB |
correct |
96 |
Correct |
57 ms |
2432 KB |
correct |
97 |
Correct |
58 ms |
2560 KB |
correct |
98 |
Correct |
59 ms |
3072 KB |
correct |
99 |
Correct |
56 ms |
2560 KB |
correct |
100 |
Correct |
39 ms |
1664 KB |
correct |
101 |
Correct |
19 ms |
1408 KB |
correct |
102 |
Correct |
74 ms |
4096 KB |
correct |
103 |
Correct |
76 ms |
4096 KB |
correct |
104 |
Correct |
73 ms |
3968 KB |
correct |
105 |
Correct |
71 ms |
3968 KB |
correct |