#include "bits/stdc++.h"
#include "simurgh.h"
//#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
const int N = 505;
struct DSU{
vector<int> par,siz;
stack<array<int,2>> st;
DSU(int _n){
siz.assign(_n,1);
par.assign(_n,0);
iota(all(par),0);
}
int find(int a){
if(par[a]==a) return a;
return find(par[a]);
}
void unite(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
if(siz[a]>siz[b]) swap(a,b);
st.push({a,b});
par[a]=b;
siz[b]+=siz[a];
}
void rollback(){
if(st.empty()) return;
auto x = st.top();
st.pop();
par[x[0]] = x[0];
siz[x[1]] -= siz[x[0]];
}
};
vector<array<int,2>> edges;
vector<int> kader;
int n,m;
bool check(int a){
vector<int> vis(m, 0);
if(kader[a] == -1) return 0;
if(kader[a] == 1) return 1;
if(count(all(kader),1) == n - 1) return 0;
vector<int> span;
DSU dsu(n),dsu2(n);
for(int x = 0; x < m ; x++){
if(kader[x] != 1) continue;
span.push_back(x);
vis[x] = 1;
dsu.unite(edges[x][0],edges[x][1]);
dsu2.unite(edges[x][0],edges[x][1]);
}
if(dsu.find(edges[a][0])==dsu.find(edges[a][1])){
kader[a] = -1;
return 0;
}
dsu.unite(edges[a][0],edges[a][1]);
int sil = sz(span);
vis[a] = 1;
span.push_back(a);
for(int i = a + 1; i < m ; i++){
if(dsu.find(edges[i][0]) == dsu.find(edges[i][1])) continue;
if(kader[i] != 0) continue;
dsu.unite(edges[i][0],edges[i][1]);
dsu2.unite(edges[i][0],edges[i][1]);
vis[i] = 1;
span.push_back(i);
}
int kaydet = count_common_roads(span);
span.erase(span.begin()+sil);
vector<int> same;
same.push_back(a);
for(int i = a + 1; i < m ; i++){
if(vis[i]) continue;
if(kader[i] != 0) continue;
if(dsu2.find(edges[i][0]) == dsu2.find(edges[i][1])) continue;
dsu2.unite(edges[i][0],edges[i][1]);
span.push_back(i);
int kaydet2 = count_common_roads(span);
if(kaydet2 == kaydet + 1){
for(int U : same) kader[U] = -1;
kader[i] = 1;
return 0;
}
else if(kaydet2 == kaydet - 1){
for(int U : same) kader[U] = 1;
kader[i] = -1;
return 1;
}
else same.push_back(i);
span.pop_back();
dsu2.rollback();
}
for(int U : same) kader[U] = 1;
return 1;
}
vector<int> find_roads(int _n, vector<int> u, vector<int> v){
m = sz(u), n = _n;
kader.assign(m, 0);
for(int i=0;i<m;i++) edges.push_back({u[i],v[i]});
for(int i=0;i<m;i++) check(i);
vector<int> ans;
for(int i=0;i<m;i++) if(kader[i]==1) ans.push_back(i);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
correct |
2 |
Correct |
1 ms |
336 KB |
correct |
3 |
Correct |
1 ms |
336 KB |
correct |
4 |
Correct |
1 ms |
336 KB |
correct |
5 |
Correct |
1 ms |
336 KB |
correct |
6 |
Correct |
1 ms |
336 KB |
correct |
7 |
Correct |
1 ms |
336 KB |
correct |
8 |
Correct |
1 ms |
336 KB |
correct |
9 |
Correct |
1 ms |
336 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
336 KB |
correct |
12 |
Correct |
1 ms |
336 KB |
correct |
13 |
Correct |
1 ms |
336 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
correct |
2 |
Correct |
1 ms |
336 KB |
correct |
3 |
Correct |
1 ms |
336 KB |
correct |
4 |
Correct |
1 ms |
336 KB |
correct |
5 |
Correct |
1 ms |
336 KB |
correct |
6 |
Correct |
1 ms |
336 KB |
correct |
7 |
Correct |
1 ms |
336 KB |
correct |
8 |
Correct |
1 ms |
336 KB |
correct |
9 |
Correct |
1 ms |
336 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
336 KB |
correct |
12 |
Correct |
1 ms |
336 KB |
correct |
13 |
Correct |
1 ms |
336 KB |
correct |
14 |
Correct |
3 ms |
336 KB |
correct |
15 |
Correct |
3 ms |
488 KB |
correct |
16 |
Correct |
3 ms |
336 KB |
correct |
17 |
Correct |
3 ms |
476 KB |
correct |
18 |
Correct |
1 ms |
336 KB |
correct |
19 |
Correct |
3 ms |
336 KB |
correct |
20 |
Correct |
3 ms |
336 KB |
correct |
21 |
Correct |
2 ms |
336 KB |
correct |
22 |
Correct |
3 ms |
472 KB |
correct |
23 |
Correct |
2 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
504 KB |
correct |
25 |
Correct |
1 ms |
336 KB |
correct |
26 |
Correct |
2 ms |
336 KB |
correct |
27 |
Correct |
2 ms |
336 KB |
correct |
28 |
Correct |
2 ms |
504 KB |
correct |
29 |
Correct |
1 ms |
336 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
1 ms |
336 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
504 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
correct |
2 |
Correct |
1 ms |
336 KB |
correct |
3 |
Correct |
1 ms |
336 KB |
correct |
4 |
Correct |
1 ms |
336 KB |
correct |
5 |
Correct |
1 ms |
336 KB |
correct |
6 |
Correct |
1 ms |
336 KB |
correct |
7 |
Correct |
1 ms |
336 KB |
correct |
8 |
Correct |
1 ms |
336 KB |
correct |
9 |
Correct |
1 ms |
336 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
336 KB |
correct |
12 |
Correct |
1 ms |
336 KB |
correct |
13 |
Correct |
1 ms |
336 KB |
correct |
14 |
Correct |
3 ms |
336 KB |
correct |
15 |
Correct |
3 ms |
488 KB |
correct |
16 |
Correct |
3 ms |
336 KB |
correct |
17 |
Correct |
3 ms |
476 KB |
correct |
18 |
Correct |
1 ms |
336 KB |
correct |
19 |
Correct |
3 ms |
336 KB |
correct |
20 |
Correct |
3 ms |
336 KB |
correct |
21 |
Correct |
2 ms |
336 KB |
correct |
22 |
Correct |
3 ms |
472 KB |
correct |
23 |
Correct |
2 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
504 KB |
correct |
25 |
Correct |
1 ms |
336 KB |
correct |
26 |
Correct |
2 ms |
336 KB |
correct |
27 |
Correct |
2 ms |
336 KB |
correct |
28 |
Correct |
2 ms |
504 KB |
correct |
29 |
Correct |
1 ms |
336 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
1 ms |
336 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
504 KB |
correct |
34 |
Correct |
346 ms |
1484 KB |
correct |
35 |
Correct |
292 ms |
1484 KB |
correct |
36 |
Correct |
193 ms |
1104 KB |
correct |
37 |
Correct |
8 ms |
504 KB |
correct |
38 |
Correct |
303 ms |
1652 KB |
correct |
39 |
Correct |
238 ms |
1360 KB |
correct |
40 |
Correct |
188 ms |
1104 KB |
correct |
41 |
Correct |
375 ms |
1484 KB |
correct |
42 |
Correct |
366 ms |
1488 KB |
correct |
43 |
Correct |
415 ms |
1072 KB |
correct |
44 |
Correct |
264 ms |
848 KB |
correct |
45 |
Correct |
236 ms |
848 KB |
correct |
46 |
Correct |
153 ms |
848 KB |
correct |
47 |
Correct |
49 ms |
592 KB |
correct |
48 |
Correct |
4 ms |
504 KB |
correct |
49 |
Correct |
10 ms |
336 KB |
correct |
50 |
Correct |
39 ms |
592 KB |
correct |
51 |
Correct |
407 ms |
848 KB |
correct |
52 |
Correct |
287 ms |
896 KB |
correct |
53 |
Correct |
200 ms |
848 KB |
correct |
54 |
Correct |
270 ms |
1080 KB |
correct |
55 |
Correct |
204 ms |
944 KB |
correct |
56 |
Correct |
207 ms |
848 KB |
correct |
57 |
Correct |
220 ms |
848 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
correct |
2 |
Correct |
1 ms |
336 KB |
correct |
3 |
Incorrect |
180 ms |
3556 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
correct |
2 |
Correct |
1 ms |
336 KB |
correct |
3 |
Correct |
1 ms |
336 KB |
correct |
4 |
Correct |
1 ms |
336 KB |
correct |
5 |
Correct |
1 ms |
336 KB |
correct |
6 |
Correct |
1 ms |
336 KB |
correct |
7 |
Correct |
1 ms |
336 KB |
correct |
8 |
Correct |
1 ms |
336 KB |
correct |
9 |
Correct |
1 ms |
336 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
336 KB |
correct |
12 |
Correct |
1 ms |
336 KB |
correct |
13 |
Correct |
1 ms |
336 KB |
correct |
14 |
Correct |
3 ms |
336 KB |
correct |
15 |
Correct |
3 ms |
488 KB |
correct |
16 |
Correct |
3 ms |
336 KB |
correct |
17 |
Correct |
3 ms |
476 KB |
correct |
18 |
Correct |
1 ms |
336 KB |
correct |
19 |
Correct |
3 ms |
336 KB |
correct |
20 |
Correct |
3 ms |
336 KB |
correct |
21 |
Correct |
2 ms |
336 KB |
correct |
22 |
Correct |
3 ms |
472 KB |
correct |
23 |
Correct |
2 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
504 KB |
correct |
25 |
Correct |
1 ms |
336 KB |
correct |
26 |
Correct |
2 ms |
336 KB |
correct |
27 |
Correct |
2 ms |
336 KB |
correct |
28 |
Correct |
2 ms |
504 KB |
correct |
29 |
Correct |
1 ms |
336 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
1 ms |
336 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
504 KB |
correct |
34 |
Correct |
346 ms |
1484 KB |
correct |
35 |
Correct |
292 ms |
1484 KB |
correct |
36 |
Correct |
193 ms |
1104 KB |
correct |
37 |
Correct |
8 ms |
504 KB |
correct |
38 |
Correct |
303 ms |
1652 KB |
correct |
39 |
Correct |
238 ms |
1360 KB |
correct |
40 |
Correct |
188 ms |
1104 KB |
correct |
41 |
Correct |
375 ms |
1484 KB |
correct |
42 |
Correct |
366 ms |
1488 KB |
correct |
43 |
Correct |
415 ms |
1072 KB |
correct |
44 |
Correct |
264 ms |
848 KB |
correct |
45 |
Correct |
236 ms |
848 KB |
correct |
46 |
Correct |
153 ms |
848 KB |
correct |
47 |
Correct |
49 ms |
592 KB |
correct |
48 |
Correct |
4 ms |
504 KB |
correct |
49 |
Correct |
10 ms |
336 KB |
correct |
50 |
Correct |
39 ms |
592 KB |
correct |
51 |
Correct |
407 ms |
848 KB |
correct |
52 |
Correct |
287 ms |
896 KB |
correct |
53 |
Correct |
200 ms |
848 KB |
correct |
54 |
Correct |
270 ms |
1080 KB |
correct |
55 |
Correct |
204 ms |
944 KB |
correct |
56 |
Correct |
207 ms |
848 KB |
correct |
57 |
Correct |
220 ms |
848 KB |
correct |
58 |
Correct |
1 ms |
336 KB |
correct |
59 |
Correct |
1 ms |
336 KB |
correct |
60 |
Incorrect |
180 ms |
3556 KB |
WA in grader: NO |
61 |
Halted |
0 ms |
0 KB |
- |