#include "simurgh.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=505;
const int mxM=10000;
const int mxK=60;
const ll INF=1e18;
struct uf{
int p[mxN];
void init(int n){for(int i=0;i<n;i++) p[i]=i;}
int fp(int a){return p[a]==a ? a : p[a]=fp(p[a]);}
void mrg(int a, int b){p[fp(a)]=fp(b);}
};
int N;
vector <int> v[mxN];
vector <pii> E;
int num[mxN][mxN];
int gold[mxN][mxN];
//dfs
bool Chk[mxN];
int dep[mxN], par[mxN];
vector <int> sub[mxN];
vector <int> tree;
void init(){
for(int i=0;i<mxN;i++) v[i].clear();
E.clear();
for(int i=0;i<mxN;i++) for(int j=0;j<mxN;j++) num[i][j]=-1, gold[i][j]=-1;
for(int i=0;i<mxN;i++) Chk[i]=false, dep[i]=0, par[i]=0, sub[i].clear();
tree.clear();
}
void dfs(int now){
Chk[now]=true;
sub[now].push_back(now);
for(int nxt : v[now]) if(!Chk[nxt]){
dep[nxt]=dep[now]+1;
par[nxt]=now;
tree.push_back(num[nxt][now]);
dfs(nxt);
for(int x : sub[nxt]) sub[now].push_back(x);
}
}
uf U_qry;
int make_query(vector <int> qry, bool proc){ // qry does not have a cycle!, proc: remove value of edges out of qry
vector <int> res=qry;
U_qry.init(N);
for(int x : qry) U_qry.mrg(E[x].fi, E[x].se);
for(int x : tree){
auto [now1, now2]=E[x];
if(U_qry.fp(now1)!=U_qry.fp(now2)){
res.push_back(x);
U_qry.mrg(now1, now2);
}
}
int val=count_common_roads(res);
if(proc){
for(int i=qry.size();i<res.size();i++){
auto [now1, now2]=E[res[i]];
if(gold[now1][now2]==1) val--;
}
}
return val;
}
vector <int> minu(vector <int> gph, int x){
vector <int> res;
for(int y : gph) if(y!=x) res.push_back(y);
return res;
}
void check_cycle(vector <int> cyc){
vector <int> qres(cyc.size(), -1);
for(int i=0;i<cyc.size();i++){
auto [now1, now2]=E[cyc[i]];
if(gold[now1][now2]==-1) qres[i]=make_query(minu(cyc, cyc[i]), false);
}
int know=-1, tot=-1;
for(int x : cyc) if(gold[E[x].fi][E[x].se]!=-1) know=x;
if(know!=-1){
tot=make_query(minu(cyc, know), false)+gold[E[know].fi][E[know].se];
}
else {
int maxi=-1, mini=1'000'000;
for(int i=0;i<cyc.size();i++) maxi=max(maxi, qres[i]), mini=min(mini, qres[i]);
bool same=(maxi==mini);
if(same) tot=maxi;
else{
int sum=0;
for(int i=0;i<cyc.size();i++) sum+=qres[i];
tot=sum/cyc.size()+1;
}
}
for(int i=0;i<cyc.size();i++){
auto [now1, now2]=E[cyc[i]];
if(gold[now1][now2]==-1){
if(qres[i]==tot) gold[now1][now2]=gold[now2][now1]=0;
else gold[now1][now2]=gold[now2][now1]=1;
}
}
}void make_spanning_tree(){
dfs(0);
for(int ednum : tree){
auto [now1, now2]=E[ednum];
if(dep[now1]>dep[now2]) swap(now1, now2);
if(gold[now1][now2]!=-1) continue;
int down=-1, up=-1;
for(int x : sub[now2]) for(int y : v[x]) if(dep[y]<=dep[now1] && (x!=now2 || y!=now1)) down=x, up=y;
if(up==-1){
gold[now1][now2]=gold[now2][now1]=1;
continue;
}
vector <int> cyc;
int pnt=down;
while(pnt!=up){
cyc.push_back(num[pnt][par[pnt]]);
pnt=par[pnt];
}
cyc.push_back(num[down][up]);
check_cycle(cyc);
}
}
vector <int> rng(vector <int> s, pii lr){
vector <int> res;
for(int i=lr.fi;i<=lr.se;i++) res.push_back(s[i]);
return res;
}
void find_gold(){
for(int i=0;i<N;i++){
vector <int> ct;
for(int j=i+1;j<N;j++){
if(num[i][j]!=-1 && gold[i][j]==-1) ct.push_back(num[i][j]);
}
while(ct.size() && make_query(ct, true)!=0){
int s=0, e=ct.size()-1;
while(s!=e){
int mid=(s+e)/2;
if(make_query(rng(ct, pii(s, mid)), true)==0) s=mid+1;
else e=mid;
}
auto [now1, now2]=E[ct[s]];
gold[now1][now2]=gold[now2][now1]=1;
ct=minu(ct, ct[s]);
}
}
}
std::vector<int> find_roads(int n, std::vector<int> a, std::vector<int> b) {
N=n;
init();
for(int i=0;i<a.size();i++){
int t1=a[i], t2=b[i];
v[t1].push_back(t2);
v[t2].push_back(t1);
E.emplace_back(t1, t2);
num[t1][t2]=num[t2][t1]=i;
}
make_spanning_tree();
find_gold();
vector<int> r;
for(int i=0;i<N;i++) for(int j=i+1;j<N;j++) if(gold[i][j]==1) r.push_back(num[i][j]);
return r;
}
Compilation message
simurgh.cpp: In function 'int make_query(std::vector<int>, bool)':
simurgh.cpp:67:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=qry.size();i<res.size();i++){
| ~^~~~~~~~~~~
simurgh.cpp: In function 'void check_cycle(std::vector<int>)':
simurgh.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i=0;i<cyc.size();i++){
| ~^~~~~~~~~~~
simurgh.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i=0;i<cyc.size();i++) maxi=max(maxi, qres[i]), mini=min(mini, qres[i]);
| ~^~~~~~~~~~~
simurgh.cpp:97:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=0;i<cyc.size();i++) sum+=qres[i];
| ~^~~~~~~~~~~
simurgh.cpp:101:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int i=0;i<cyc.size();i++){
| ~^~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:157:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2392 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
1 ms |
2260 KB |
correct |
10 |
Correct |
1 ms |
2392 KB |
correct |
11 |
Correct |
2 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2256 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2392 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
1 ms |
2260 KB |
correct |
10 |
Correct |
1 ms |
2392 KB |
correct |
11 |
Correct |
2 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2256 KB |
correct |
14 |
Correct |
2 ms |
2392 KB |
correct |
15 |
Correct |
2 ms |
2396 KB |
correct |
16 |
Correct |
2 ms |
2396 KB |
correct |
17 |
Correct |
2 ms |
2284 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
2 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
2 ms |
2392 KB |
correct |
26 |
Correct |
2 ms |
2392 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2484 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2392 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2392 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
1 ms |
2260 KB |
correct |
10 |
Correct |
1 ms |
2392 KB |
correct |
11 |
Correct |
2 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2256 KB |
correct |
14 |
Correct |
2 ms |
2392 KB |
correct |
15 |
Correct |
2 ms |
2396 KB |
correct |
16 |
Correct |
2 ms |
2396 KB |
correct |
17 |
Correct |
2 ms |
2284 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
2 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
2 ms |
2392 KB |
correct |
26 |
Correct |
2 ms |
2392 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2484 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2392 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
34 |
Correct |
23 ms |
3800 KB |
correct |
35 |
Correct |
22 ms |
3800 KB |
correct |
36 |
Correct |
20 ms |
3544 KB |
correct |
37 |
Correct |
8 ms |
2648 KB |
correct |
38 |
Correct |
23 ms |
3688 KB |
correct |
39 |
Correct |
21 ms |
3544 KB |
correct |
40 |
Correct |
22 ms |
3540 KB |
correct |
41 |
Correct |
20 ms |
3796 KB |
correct |
42 |
Correct |
20 ms |
3844 KB |
correct |
43 |
Correct |
11 ms |
3416 KB |
correct |
44 |
Correct |
10 ms |
2912 KB |
correct |
45 |
Correct |
10 ms |
3164 KB |
correct |
46 |
Correct |
14 ms |
3064 KB |
correct |
47 |
Correct |
8 ms |
2652 KB |
correct |
48 |
Correct |
3 ms |
2392 KB |
correct |
49 |
Correct |
6 ms |
2684 KB |
correct |
50 |
Correct |
8 ms |
2824 KB |
correct |
51 |
Correct |
11 ms |
2980 KB |
correct |
52 |
Correct |
12 ms |
2908 KB |
correct |
53 |
Correct |
10 ms |
2908 KB |
correct |
54 |
Correct |
12 ms |
3160 KB |
correct |
55 |
Correct |
10 ms |
3160 KB |
correct |
56 |
Correct |
10 ms |
3160 KB |
correct |
57 |
Correct |
10 ms |
3164 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
65 ms |
6320 KB |
correct |
4 |
Correct |
98 ms |
8136 KB |
correct |
5 |
Correct |
101 ms |
8132 KB |
correct |
6 |
Correct |
96 ms |
8144 KB |
correct |
7 |
Correct |
88 ms |
8144 KB |
correct |
8 |
Correct |
96 ms |
8388 KB |
correct |
9 |
Correct |
103 ms |
8144 KB |
correct |
10 |
Correct |
99 ms |
8144 KB |
correct |
11 |
Correct |
96 ms |
8136 KB |
correct |
12 |
Correct |
100 ms |
8140 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
96 ms |
8144 KB |
correct |
15 |
Correct |
106 ms |
8140 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2392 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
1 ms |
2260 KB |
correct |
10 |
Correct |
1 ms |
2392 KB |
correct |
11 |
Correct |
2 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2256 KB |
correct |
14 |
Correct |
2 ms |
2392 KB |
correct |
15 |
Correct |
2 ms |
2396 KB |
correct |
16 |
Correct |
2 ms |
2396 KB |
correct |
17 |
Correct |
2 ms |
2284 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
2 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
2 ms |
2392 KB |
correct |
26 |
Correct |
2 ms |
2392 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2484 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2392 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
34 |
Correct |
23 ms |
3800 KB |
correct |
35 |
Correct |
22 ms |
3800 KB |
correct |
36 |
Correct |
20 ms |
3544 KB |
correct |
37 |
Correct |
8 ms |
2648 KB |
correct |
38 |
Correct |
23 ms |
3688 KB |
correct |
39 |
Correct |
21 ms |
3544 KB |
correct |
40 |
Correct |
22 ms |
3540 KB |
correct |
41 |
Correct |
20 ms |
3796 KB |
correct |
42 |
Correct |
20 ms |
3844 KB |
correct |
43 |
Correct |
11 ms |
3416 KB |
correct |
44 |
Correct |
10 ms |
2912 KB |
correct |
45 |
Correct |
10 ms |
3164 KB |
correct |
46 |
Correct |
14 ms |
3064 KB |
correct |
47 |
Correct |
8 ms |
2652 KB |
correct |
48 |
Correct |
3 ms |
2392 KB |
correct |
49 |
Correct |
6 ms |
2684 KB |
correct |
50 |
Correct |
8 ms |
2824 KB |
correct |
51 |
Correct |
11 ms |
2980 KB |
correct |
52 |
Correct |
12 ms |
2908 KB |
correct |
53 |
Correct |
10 ms |
2908 KB |
correct |
54 |
Correct |
12 ms |
3160 KB |
correct |
55 |
Correct |
10 ms |
3160 KB |
correct |
56 |
Correct |
10 ms |
3160 KB |
correct |
57 |
Correct |
10 ms |
3164 KB |
correct |
58 |
Correct |
1 ms |
2396 KB |
correct |
59 |
Correct |
1 ms |
2396 KB |
correct |
60 |
Correct |
65 ms |
6320 KB |
correct |
61 |
Correct |
98 ms |
8136 KB |
correct |
62 |
Correct |
101 ms |
8132 KB |
correct |
63 |
Correct |
96 ms |
8144 KB |
correct |
64 |
Correct |
88 ms |
8144 KB |
correct |
65 |
Correct |
96 ms |
8388 KB |
correct |
66 |
Correct |
103 ms |
8144 KB |
correct |
67 |
Correct |
99 ms |
8144 KB |
correct |
68 |
Correct |
96 ms |
8136 KB |
correct |
69 |
Correct |
100 ms |
8140 KB |
correct |
70 |
Correct |
1 ms |
2396 KB |
correct |
71 |
Correct |
96 ms |
8144 KB |
correct |
72 |
Correct |
106 ms |
8140 KB |
correct |
73 |
Correct |
1 ms |
2392 KB |
correct |
74 |
Correct |
96 ms |
8148 KB |
correct |
75 |
Correct |
99 ms |
7884 KB |
correct |
76 |
Correct |
53 ms |
4820 KB |
correct |
77 |
Correct |
99 ms |
8148 KB |
correct |
78 |
Correct |
101 ms |
8144 KB |
correct |
79 |
Correct |
99 ms |
8144 KB |
correct |
80 |
Correct |
98 ms |
8028 KB |
correct |
81 |
Correct |
88 ms |
7372 KB |
correct |
82 |
Correct |
110 ms |
7888 KB |
correct |
83 |
Correct |
78 ms |
5588 KB |
correct |
84 |
Correct |
47 ms |
6348 KB |
correct |
85 |
Correct |
46 ms |
5840 KB |
correct |
86 |
Correct |
38 ms |
4868 KB |
correct |
87 |
Correct |
35 ms |
4308 KB |
correct |
88 |
Correct |
32 ms |
3800 KB |
correct |
89 |
Correct |
33 ms |
3932 KB |
correct |
90 |
Correct |
32 ms |
3544 KB |
correct |
91 |
Correct |
21 ms |
2652 KB |
correct |
92 |
Correct |
9 ms |
2396 KB |
correct |
93 |
Correct |
44 ms |
6100 KB |
correct |
94 |
Correct |
39 ms |
4820 KB |
correct |
95 |
Correct |
39 ms |
4816 KB |
correct |
96 |
Correct |
31 ms |
3796 KB |
correct |
97 |
Correct |
38 ms |
3796 KB |
correct |
98 |
Correct |
34 ms |
4416 KB |
correct |
99 |
Correct |
32 ms |
3800 KB |
correct |
100 |
Correct |
23 ms |
3160 KB |
correct |
101 |
Correct |
12 ms |
2908 KB |
correct |
102 |
Correct |
44 ms |
5720 KB |
correct |
103 |
Correct |
45 ms |
5560 KB |
correct |
104 |
Correct |
43 ms |
5584 KB |
correct |
105 |
Correct |
47 ms |
5584 KB |
correct |