#include "simurgh.h"
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(int i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (506)
vector<int> r, tmp;
int m, p[MAXN], state[MAXN*MAXN], depth[MAXN], pe[MAXN];
struct u {
int p[MAXN], sz[MAXN];
void init() {
FOR(i,0,MAXN-1) p[i]=i,sz[i]=1;
}
bool merge(int x,int y) {
x=find(x), y=find(y);
if(x==y) return 0;
if(sz[x]>sz[y]) swap(x, y);
p[x]=y, sz[y]+=sz[x]; return 1;
}
int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
} u;
struct edge {
int x, ind;
}; vector<edge> v[MAXN];
bitset<MAXN*MAXN> span;
bitset<MAXN> vis;
int ask(vector<int> r) {
return count_common_roads(r);
}
vector<int> find_roads(int n, vector<int> U, vector<int> V) {
m = siz(U);
FOR(i,0,m-1) {
int a=U[i], b=V[i];
v[a].pb({b, i}), v[b].pb({a, i});
}
function<void(ll)>dfs=[&](ll x){
vis[x]=1;
for(auto i:v[x]) if(!vis[i.x]) {
depth[i.x]=depth[x]+1, p[i.x] = x, pe[i.x]=i.ind, dfs(i.x), span[i.ind] = 1, tmp.eb(i.ind);
}
};
dfs(0), assert(tmp.size() == n-1);
int init = ask(tmp);
FOR(i,0,m-1) if(!span[i]) {
int a=U[i], b=V[i];
if(depth[a] > depth[b]) swap(a, b);
vector<int> E;
int done = -1;
while(b ^ a) {
if(state[pe[b]]) done=pe[b];
else E.eb(pe[b]);
b = p[b];
}
if(E.empty()) continue;
if(done == -1) {
vector<pi> ind;
for(auto e:E) {
tmp.erase(find(all(tmp), e)), tmp.eb(i);
ind.eb(ask(tmp), e);
tmp.pop_back(), tmp.eb(e);
}
sort(all(ind));
if(ind[0].f == ind.back().f) {
for(auto i:ind) state[i.s] = init == i.f ? 1 : 2;
} else {
for(auto i:ind) if(i.f == ind.back().f) state[i.s] = 1; else state[i.s] = 2;
}
} else {
tmp.erase(find(all(tmp), done)), tmp.eb(i);
int init = ask(tmp);
tmp.pop_back(), tmp.eb(done);
for(auto e:E) {
tmp.erase(find(all(tmp), e)), tmp.eb(i);
if(ask(tmp) == init) state[e] = state[done];
else state[e] = 3 - state[done];
tmp.pop_back(), tmp.eb(e);
}
}
}
FOR(i,0,m-1) if(span[i] && state[i]==0) state[i] = 2; else if(!span[i]) assert(state[i] == 0);
// FOR(i,0,m-1) if(span[i]) {
// cerr<<i<<" is "<<state[i]<<'\n';
// }
FOR(i,0,n-1) {
deque<int> E;
for(auto j:v[i]) if(!span[j.ind] && !state[j.ind]) E.eb(j.ind);
if(E.empty()) continue;
shuffle(all(E), rng);
auto good=[&](ll until) {
vector<int> tmp2;
u.init();
FOR(ii,0,until) { int i=E[ii];
u.merge(U[i], V[i]);
tmp2.eb(i);
}
int cost = 0;
for(auto i:tmp) if(u.merge(U[i], V[i])) {
tmp2.eb(i), cost += state[i] == 2;
}
return ask(tmp2) - cost;
};
int have = good(siz(E) - 1);
for(;have;--have) {
assert(siz(E));
int st=-1, en=siz(E)-1;
while(en-st>1) {
int mid=(st+en)>>1;
if(good(mid)>=1) en=mid;
else st=mid;
}
FOR(j,0,en-1) state[E.front()]=1, E.pop_front();
state[E.front()]=2, E.pop_front();
}
while(E.size()) state[E.front()]=1, E.pop_front();
}
assert(r.empty());
FOR(i,0,m-1) if(state[i]==2) r.eb(i);
assert(siz(r)==n-1);
return r;
}
Compilation message
In file included from /usr/include/c++/9/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
from simurgh.cpp:3:
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:69:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
69 | dfs(0), assert(tmp.size() == n-1);
| ~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
correct |
2 |
Correct |
1 ms |
364 KB |
correct |
3 |
Correct |
1 ms |
364 KB |
correct |
4 |
Correct |
1 ms |
364 KB |
correct |
5 |
Correct |
1 ms |
364 KB |
correct |
6 |
Correct |
1 ms |
364 KB |
correct |
7 |
Correct |
1 ms |
364 KB |
correct |
8 |
Correct |
1 ms |
364 KB |
correct |
9 |
Correct |
1 ms |
364 KB |
correct |
10 |
Correct |
1 ms |
364 KB |
correct |
11 |
Correct |
1 ms |
364 KB |
correct |
12 |
Correct |
1 ms |
364 KB |
correct |
13 |
Correct |
1 ms |
384 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
correct |
2 |
Correct |
1 ms |
364 KB |
correct |
3 |
Correct |
1 ms |
364 KB |
correct |
4 |
Correct |
1 ms |
364 KB |
correct |
5 |
Correct |
1 ms |
364 KB |
correct |
6 |
Correct |
1 ms |
364 KB |
correct |
7 |
Correct |
1 ms |
364 KB |
correct |
8 |
Correct |
1 ms |
364 KB |
correct |
9 |
Correct |
1 ms |
364 KB |
correct |
10 |
Correct |
1 ms |
364 KB |
correct |
11 |
Correct |
1 ms |
364 KB |
correct |
12 |
Correct |
1 ms |
364 KB |
correct |
13 |
Correct |
1 ms |
384 KB |
correct |
14 |
Correct |
2 ms |
364 KB |
correct |
15 |
Correct |
2 ms |
364 KB |
correct |
16 |
Correct |
2 ms |
364 KB |
correct |
17 |
Correct |
2 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
364 KB |
correct |
19 |
Correct |
2 ms |
364 KB |
correct |
20 |
Correct |
2 ms |
364 KB |
correct |
21 |
Correct |
2 ms |
364 KB |
correct |
22 |
Correct |
2 ms |
364 KB |
correct |
23 |
Correct |
1 ms |
364 KB |
correct |
24 |
Correct |
1 ms |
364 KB |
correct |
25 |
Correct |
1 ms |
364 KB |
correct |
26 |
Correct |
1 ms |
384 KB |
correct |
27 |
Correct |
1 ms |
364 KB |
correct |
28 |
Correct |
1 ms |
364 KB |
correct |
29 |
Correct |
1 ms |
364 KB |
correct |
30 |
Correct |
1 ms |
364 KB |
correct |
31 |
Correct |
1 ms |
364 KB |
correct |
32 |
Correct |
1 ms |
364 KB |
correct |
33 |
Correct |
1 ms |
364 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
correct |
2 |
Correct |
1 ms |
364 KB |
correct |
3 |
Correct |
1 ms |
364 KB |
correct |
4 |
Correct |
1 ms |
364 KB |
correct |
5 |
Correct |
1 ms |
364 KB |
correct |
6 |
Correct |
1 ms |
364 KB |
correct |
7 |
Correct |
1 ms |
364 KB |
correct |
8 |
Correct |
1 ms |
364 KB |
correct |
9 |
Correct |
1 ms |
364 KB |
correct |
10 |
Correct |
1 ms |
364 KB |
correct |
11 |
Correct |
1 ms |
364 KB |
correct |
12 |
Correct |
1 ms |
364 KB |
correct |
13 |
Correct |
1 ms |
384 KB |
correct |
14 |
Correct |
2 ms |
364 KB |
correct |
15 |
Correct |
2 ms |
364 KB |
correct |
16 |
Correct |
2 ms |
364 KB |
correct |
17 |
Correct |
2 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
364 KB |
correct |
19 |
Correct |
2 ms |
364 KB |
correct |
20 |
Correct |
2 ms |
364 KB |
correct |
21 |
Correct |
2 ms |
364 KB |
correct |
22 |
Correct |
2 ms |
364 KB |
correct |
23 |
Correct |
1 ms |
364 KB |
correct |
24 |
Correct |
1 ms |
364 KB |
correct |
25 |
Correct |
1 ms |
364 KB |
correct |
26 |
Correct |
1 ms |
384 KB |
correct |
27 |
Correct |
1 ms |
364 KB |
correct |
28 |
Correct |
1 ms |
364 KB |
correct |
29 |
Correct |
1 ms |
364 KB |
correct |
30 |
Correct |
1 ms |
364 KB |
correct |
31 |
Correct |
1 ms |
364 KB |
correct |
32 |
Correct |
1 ms |
364 KB |
correct |
33 |
Correct |
1 ms |
364 KB |
correct |
34 |
Correct |
47 ms |
1772 KB |
correct |
35 |
Correct |
48 ms |
1644 KB |
correct |
36 |
Correct |
38 ms |
1388 KB |
correct |
37 |
Correct |
10 ms |
512 KB |
correct |
38 |
Correct |
46 ms |
1772 KB |
correct |
39 |
Correct |
40 ms |
1644 KB |
correct |
40 |
Correct |
37 ms |
1644 KB |
correct |
41 |
Correct |
46 ms |
1644 KB |
correct |
42 |
Correct |
47 ms |
1644 KB |
correct |
43 |
Correct |
23 ms |
1260 KB |
correct |
44 |
Correct |
17 ms |
1004 KB |
correct |
45 |
Correct |
19 ms |
1004 KB |
correct |
46 |
Correct |
17 ms |
876 KB |
correct |
47 |
Correct |
10 ms |
640 KB |
correct |
48 |
Correct |
4 ms |
364 KB |
correct |
49 |
Correct |
7 ms |
492 KB |
correct |
50 |
Correct |
12 ms |
620 KB |
correct |
51 |
Correct |
20 ms |
1004 KB |
correct |
52 |
Correct |
17 ms |
1004 KB |
correct |
53 |
Correct |
17 ms |
876 KB |
correct |
54 |
Correct |
20 ms |
1260 KB |
correct |
55 |
Correct |
19 ms |
1004 KB |
correct |
56 |
Correct |
19 ms |
1132 KB |
correct |
57 |
Correct |
18 ms |
1004 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
correct |
2 |
Correct |
1 ms |
364 KB |
correct |
3 |
Correct |
151 ms |
4460 KB |
correct |
4 |
Correct |
248 ms |
6252 KB |
correct |
5 |
Correct |
253 ms |
6124 KB |
correct |
6 |
Correct |
242 ms |
6124 KB |
correct |
7 |
Correct |
226 ms |
6252 KB |
correct |
8 |
Correct |
232 ms |
6124 KB |
correct |
9 |
Correct |
246 ms |
6124 KB |
correct |
10 |
Correct |
255 ms |
6120 KB |
correct |
11 |
Correct |
253 ms |
6124 KB |
correct |
12 |
Correct |
254 ms |
5996 KB |
correct |
13 |
Correct |
1 ms |
364 KB |
correct |
14 |
Correct |
239 ms |
5996 KB |
correct |
15 |
Correct |
251 ms |
6124 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
correct |
2 |
Correct |
1 ms |
364 KB |
correct |
3 |
Correct |
1 ms |
364 KB |
correct |
4 |
Correct |
1 ms |
364 KB |
correct |
5 |
Correct |
1 ms |
364 KB |
correct |
6 |
Correct |
1 ms |
364 KB |
correct |
7 |
Correct |
1 ms |
364 KB |
correct |
8 |
Correct |
1 ms |
364 KB |
correct |
9 |
Correct |
1 ms |
364 KB |
correct |
10 |
Correct |
1 ms |
364 KB |
correct |
11 |
Correct |
1 ms |
364 KB |
correct |
12 |
Correct |
1 ms |
364 KB |
correct |
13 |
Correct |
1 ms |
384 KB |
correct |
14 |
Correct |
2 ms |
364 KB |
correct |
15 |
Correct |
2 ms |
364 KB |
correct |
16 |
Correct |
2 ms |
364 KB |
correct |
17 |
Correct |
2 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
364 KB |
correct |
19 |
Correct |
2 ms |
364 KB |
correct |
20 |
Correct |
2 ms |
364 KB |
correct |
21 |
Correct |
2 ms |
364 KB |
correct |
22 |
Correct |
2 ms |
364 KB |
correct |
23 |
Correct |
1 ms |
364 KB |
correct |
24 |
Correct |
1 ms |
364 KB |
correct |
25 |
Correct |
1 ms |
364 KB |
correct |
26 |
Correct |
1 ms |
384 KB |
correct |
27 |
Correct |
1 ms |
364 KB |
correct |
28 |
Correct |
1 ms |
364 KB |
correct |
29 |
Correct |
1 ms |
364 KB |
correct |
30 |
Correct |
1 ms |
364 KB |
correct |
31 |
Correct |
1 ms |
364 KB |
correct |
32 |
Correct |
1 ms |
364 KB |
correct |
33 |
Correct |
1 ms |
364 KB |
correct |
34 |
Correct |
47 ms |
1772 KB |
correct |
35 |
Correct |
48 ms |
1644 KB |
correct |
36 |
Correct |
38 ms |
1388 KB |
correct |
37 |
Correct |
10 ms |
512 KB |
correct |
38 |
Correct |
46 ms |
1772 KB |
correct |
39 |
Correct |
40 ms |
1644 KB |
correct |
40 |
Correct |
37 ms |
1644 KB |
correct |
41 |
Correct |
46 ms |
1644 KB |
correct |
42 |
Correct |
47 ms |
1644 KB |
correct |
43 |
Correct |
23 ms |
1260 KB |
correct |
44 |
Correct |
17 ms |
1004 KB |
correct |
45 |
Correct |
19 ms |
1004 KB |
correct |
46 |
Correct |
17 ms |
876 KB |
correct |
47 |
Correct |
10 ms |
640 KB |
correct |
48 |
Correct |
4 ms |
364 KB |
correct |
49 |
Correct |
7 ms |
492 KB |
correct |
50 |
Correct |
12 ms |
620 KB |
correct |
51 |
Correct |
20 ms |
1004 KB |
correct |
52 |
Correct |
17 ms |
1004 KB |
correct |
53 |
Correct |
17 ms |
876 KB |
correct |
54 |
Correct |
20 ms |
1260 KB |
correct |
55 |
Correct |
19 ms |
1004 KB |
correct |
56 |
Correct |
19 ms |
1132 KB |
correct |
57 |
Correct |
18 ms |
1004 KB |
correct |
58 |
Correct |
1 ms |
364 KB |
correct |
59 |
Correct |
1 ms |
364 KB |
correct |
60 |
Correct |
151 ms |
4460 KB |
correct |
61 |
Correct |
248 ms |
6252 KB |
correct |
62 |
Correct |
253 ms |
6124 KB |
correct |
63 |
Correct |
242 ms |
6124 KB |
correct |
64 |
Correct |
226 ms |
6252 KB |
correct |
65 |
Correct |
232 ms |
6124 KB |
correct |
66 |
Correct |
246 ms |
6124 KB |
correct |
67 |
Correct |
255 ms |
6120 KB |
correct |
68 |
Correct |
253 ms |
6124 KB |
correct |
69 |
Correct |
254 ms |
5996 KB |
correct |
70 |
Correct |
1 ms |
364 KB |
correct |
71 |
Correct |
239 ms |
5996 KB |
correct |
72 |
Correct |
251 ms |
6124 KB |
correct |
73 |
Correct |
1 ms |
364 KB |
correct |
74 |
Correct |
251 ms |
5996 KB |
correct |
75 |
Correct |
242 ms |
6124 KB |
correct |
76 |
Correct |
111 ms |
2668 KB |
correct |
77 |
Correct |
253 ms |
6124 KB |
correct |
78 |
Correct |
253 ms |
6124 KB |
correct |
79 |
Correct |
251 ms |
6124 KB |
correct |
80 |
Correct |
247 ms |
5996 KB |
correct |
81 |
Correct |
210 ms |
5740 KB |
correct |
82 |
Correct |
258 ms |
5996 KB |
correct |
83 |
Correct |
164 ms |
3564 KB |
correct |
84 |
Correct |
111 ms |
4204 KB |
correct |
85 |
Correct |
102 ms |
3948 KB |
correct |
86 |
Correct |
80 ms |
2668 KB |
correct |
87 |
Correct |
68 ms |
2412 KB |
correct |
88 |
Correct |
57 ms |
1644 KB |
correct |
89 |
Correct |
59 ms |
1644 KB |
correct |
90 |
Correct |
53 ms |
1516 KB |
correct |
91 |
Correct |
26 ms |
492 KB |
correct |
92 |
Correct |
13 ms |
364 KB |
correct |
93 |
Correct |
96 ms |
4076 KB |
correct |
94 |
Correct |
75 ms |
2668 KB |
correct |
95 |
Correct |
76 ms |
2796 KB |
correct |
96 |
Correct |
56 ms |
1516 KB |
correct |
97 |
Correct |
59 ms |
1644 KB |
correct |
98 |
Correct |
64 ms |
2284 KB |
correct |
99 |
Correct |
58 ms |
1644 KB |
correct |
100 |
Correct |
35 ms |
748 KB |
correct |
101 |
Correct |
14 ms |
492 KB |
correct |
102 |
Correct |
101 ms |
3308 KB |
correct |
103 |
Correct |
103 ms |
3436 KB |
correct |
104 |
Correct |
99 ms |
3308 KB |
correct |
105 |
Correct |
99 ms |
3180 KB |
correct |