#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
using ll = int;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
namespace TRUNG{
const ll MAXN = 510;
ll n,m;
ll ans[MAXN*MAXN];
vector <pll> g[MAXN];
bool sus[MAXN*MAXN];
ll in[MAXN],out[MAXN];
ll timeDFS;
vector <pll> a;
ll cnt_query;
void dfs(ll u,ll p){
in[u] = ++timeDFS;
for (auto tmp:g[u]){
ll v = tmp.fi,id = tmp.se;
if (v==p)continue;
dfs(v,u);
}
out[u] = timeDFS;
}
bool sus_edge(ll i,ll j){
ll x = a[i].fi;
if (in[a[i].se] > in[x])x = a[i].se;
// cout<<i<<' '<<j<<' '<<x<<endl;
if ((in[x] <= in[a[j].fi] && in[a[j].fi] <= out[x])+(in[x] <= in[a[j].se] && in[a[j].se] <= out[x])==1)return 1;
return 0;
}
namespace DSU{
ll dsu[MAXN];
void init(){
memset(dsu,-1,sizeof dsu);
}
ll f(ll x){
if (dsu[x] < 0)return x;
return (dsu[x] = f(dsu[x]));
}
void join(ll x,ll y){
x = f(x),y = f(y);
if (x!=y){
if (dsu[x] > dsu[y])swap(x,y);
dsu[x] += dsu[y];
dsu[y] = x;
}
}
}
bitset <MAXN> bs[MAXN];
ll ind[MAXN][MAXN];
vector <ll> tree,extra;
ll superior_count(vector <ll> all){
DSU::init();
for (auto x:all){
DSU::join(a[x].fi,a[x].se);
}
ll res = 0;
for (auto x:tree){
if (DSU::f(a[x].fi) != DSU::f(a[x].se)){
DSU::join(a[x].fi,a[x].se);
all.push_back(x);
res-=ans[x];
}
}
assert(++cnt_query<=8000);
res += count_common_roads(all);
return res;
}
void solve(vector <ll> all,ll k){
if (k==0)return;
if (sz(all) == 1){
ans[all[0]] = k;
return;
}
ll mid = sz(all) / 2;
vector <ll> L,R;
for (ll i = 0;i < sz(all);i ++){
if (i < mid)L.push_back(all[i]);
else R.push_back(all[i]);
}
ll cnt_L = superior_count(L);
solve(L,cnt_L);
solve(R,k-cnt_L);
}
}
std::vector<int> find_roads(int N, std::vector<int> U, std::vector<int> V) {
using namespace TRUNG;
memset(ans,-1,sizeof ans);
ll m = sz(U),n=N ;
a.resize(m);
for (ll i = 0;i < m;i ++)a[i] = MP(U[i],V[i]);
DSU::init();
for (ll i = 0;i < m;i ++){
if (DSU::f(a[i].fi) != DSU::f(a[i].se)){sus[i] = 1;tree.push_back(i);}
DSU::join(a[i].fi,a[i].se);
}
DSU::init();
for (ll i = 0;i < m;i ++){
if (sus[i])continue;
if (DSU::f(a[i].fi) != DSU::f(a[i].se))extra.push_back(i);
DSU::join(a[i].fi,a[i].se);
}
for (auto id:tree){
g[a[id].fi].push_back(MP(a[id].se,id));
g[a[id].se].push_back(MP(a[id].fi,id));
}
dfs(0,0);
// cout<<in[4]<<' '<<out[4]<<' '<<in[1]<<' '<<out[1]<<endl;
// for (auto x:tree)cout<<x<<' ';
// cout<<'\n';
// for (auto x:extra)cout<<x<<' ';
// cout<<'\n';
// cout<<sus_edge(4,7)<<endl;
ll cur = count_common_roads(tree);
assert(++cnt_query<=8000);
for (auto id:tree){
// cout<<id<<endl;
if (ans[id] != -1)continue;
for (auto x:extra){
if (sus_edge(id,x)){
// cout<<"WOW "<<x<<' '<<id<<endl;
auto cal = [&](ll y){
vector <ll> tmp;
for (auto sss:tree)if (sss != y)tmp.push_back(sss);
tmp.push_back(x);
assert(++cnt_query<=8000);
// cout<<"CAL "<<y<<": ";
// for (auto z:tmp)cout<<z<<' ';
// cout<<endl;
ll res = count_common_roads(tmp);
// cout<<"VAL "<<res<<endl;
return res;
};
vector <ll> cycle;
for (auto y:tree){
if (sus_edge(y,x)){
cycle.push_back(y);
}
}
// for (auto y:cycle)cout<<y<<' ';
// cout<<endl;
ll sum = -1;
if (ans[x] != -1)sum = ans[x] + cur;
for (auto y:cycle){
if (ans[y] != -1 && sum == -1){
sum = cal(y) + ans[y];
break;
}
}
if (sum==-1){
vector <ll> com(sz(cycle));
for (ll j = 0;j < sz(cycle);j ++){
com[j] = cal(cycle[j]);
if (com[j] < cur)sum = cur;
if (com[j] > cur)sum = cur+1;
// cout<<cycle[j]<<' '<<com[j]<<endl;
}
if (sum == -1)sum = cur;
for (ll j = 0;j < sz(cycle);j ++){
ans[cycle[j]] = sum - com[j];
}
ans[x] = sum - cur;
}
else{
for (auto y:cycle)if (ans[y] == -1){
ans[y] = sum - cal(y);
}
ans[x] = sum - cur;
}
break;
}
}
if (ans[id] == -1)ans[id] = 1;
}
// for (ll j = 0;j < m;j ++)cout<<ans[j]<<' ';
// cout<<endl;
for (ll j = 0;j < m;j ++){
pll x = a[j];
ind[x.fi][x.se]=ind[x.se][x.fi]=j;
if (ans[j] == -1)bs[x.fi][x.se] = bs[x.se][x.fi] = 1;
}
while (true){
bitset <MAXN> rem;
rem.set();
bitset <MAXN> tmp;
vector <ll> cur;
for (ll i = 0;i < n;i ++){
if (rem[i]){
queue <ll> q;
q.push(i);
rem[i] = 0;
while (!q.empty()){
ll u = q.front();
q.pop();
tmp = rem & bs[u];
for (ll v = tmp._Find_first();v < n;v = tmp._Find_next(v)){
cur.push_back(ind[u][v]);
bs[u][v] = bs[v][u] = 0;
q.push(v);
rem[v] = 0;
}
}
}
}
if (!sz(cur))break;
// cout<<sz(cur)<<' '<<cur[0]<<endl;
solve(cur,superior_count(cur));
}
vector <ll> r;
for (ll i = 0;i < m;i ++)if (ans[i]==1)r.push_back(i);
// assert(cnt_query<=8000);
// count_common_roads
return r;
}
Compilation message
simurgh.cpp: In function 'void TRUNG::dfs(ll, ll)':
simurgh.cpp:26:27: warning: unused variable 'id' [-Wunused-variable]
26 | ll v = tmp.fi,id = tmp.se;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2392 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2392 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
1 ms |
2756 KB |
correct |
16 |
Correct |
2 ms |
2652 KB |
correct |
17 |
Correct |
2 ms |
2652 KB |
correct |
18 |
Correct |
1 ms |
2652 KB |
correct |
19 |
Correct |
1 ms |
2652 KB |
correct |
20 |
Correct |
1 ms |
2652 KB |
correct |
21 |
Correct |
1 ms |
2756 KB |
correct |
22 |
Correct |
1 ms |
2652 KB |
correct |
23 |
Correct |
1 ms |
2652 KB |
correct |
24 |
Correct |
1 ms |
2652 KB |
correct |
25 |
Correct |
1 ms |
2652 KB |
correct |
26 |
Correct |
1 ms |
2652 KB |
correct |
27 |
Correct |
1 ms |
2652 KB |
correct |
28 |
Correct |
2 ms |
2652 KB |
correct |
29 |
Correct |
1 ms |
2652 KB |
correct |
30 |
Correct |
1 ms |
2648 KB |
correct |
31 |
Correct |
1 ms |
2652 KB |
correct |
32 |
Correct |
1 ms |
2652 KB |
correct |
33 |
Correct |
1 ms |
2652 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2392 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
1 ms |
2756 KB |
correct |
16 |
Correct |
2 ms |
2652 KB |
correct |
17 |
Correct |
2 ms |
2652 KB |
correct |
18 |
Correct |
1 ms |
2652 KB |
correct |
19 |
Correct |
1 ms |
2652 KB |
correct |
20 |
Correct |
1 ms |
2652 KB |
correct |
21 |
Correct |
1 ms |
2756 KB |
correct |
22 |
Correct |
1 ms |
2652 KB |
correct |
23 |
Correct |
1 ms |
2652 KB |
correct |
24 |
Correct |
1 ms |
2652 KB |
correct |
25 |
Correct |
1 ms |
2652 KB |
correct |
26 |
Correct |
1 ms |
2652 KB |
correct |
27 |
Correct |
1 ms |
2652 KB |
correct |
28 |
Correct |
2 ms |
2652 KB |
correct |
29 |
Correct |
1 ms |
2652 KB |
correct |
30 |
Correct |
1 ms |
2648 KB |
correct |
31 |
Correct |
1 ms |
2652 KB |
correct |
32 |
Correct |
1 ms |
2652 KB |
correct |
33 |
Correct |
1 ms |
2652 KB |
correct |
34 |
Correct |
20 ms |
3672 KB |
correct |
35 |
Correct |
23 ms |
3500 KB |
correct |
36 |
Correct |
17 ms |
3416 KB |
correct |
37 |
Correct |
5 ms |
2772 KB |
correct |
38 |
Correct |
24 ms |
3676 KB |
correct |
39 |
Correct |
18 ms |
3572 KB |
correct |
40 |
Correct |
15 ms |
3416 KB |
correct |
41 |
Correct |
21 ms |
3676 KB |
correct |
42 |
Correct |
21 ms |
3672 KB |
correct |
43 |
Correct |
6 ms |
3164 KB |
correct |
44 |
Correct |
5 ms |
3164 KB |
correct |
45 |
Correct |
6 ms |
3064 KB |
correct |
46 |
Correct |
5 ms |
2908 KB |
correct |
47 |
Correct |
3 ms |
2908 KB |
correct |
48 |
Correct |
2 ms |
2652 KB |
correct |
49 |
Correct |
3 ms |
2668 KB |
correct |
50 |
Correct |
3 ms |
2908 KB |
correct |
51 |
Correct |
6 ms |
3160 KB |
correct |
52 |
Correct |
5 ms |
3164 KB |
correct |
53 |
Correct |
5 ms |
2908 KB |
correct |
54 |
Correct |
6 ms |
3164 KB |
correct |
55 |
Correct |
9 ms |
3164 KB |
correct |
56 |
Correct |
6 ms |
3160 KB |
correct |
57 |
Correct |
5 ms |
3160 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2652 KB |
correct |
3 |
Correct |
64 ms |
5248 KB |
correct |
4 |
Correct |
96 ms |
6720 KB |
correct |
5 |
Correct |
102 ms |
6740 KB |
correct |
6 |
Correct |
96 ms |
6740 KB |
correct |
7 |
Correct |
69 ms |
6668 KB |
correct |
8 |
Correct |
71 ms |
6736 KB |
correct |
9 |
Correct |
104 ms |
6640 KB |
correct |
10 |
Correct |
102 ms |
6720 KB |
correct |
11 |
Correct |
94 ms |
6712 KB |
correct |
12 |
Correct |
99 ms |
6660 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
78 ms |
6740 KB |
correct |
15 |
Correct |
87 ms |
6736 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2392 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
1 ms |
2756 KB |
correct |
16 |
Correct |
2 ms |
2652 KB |
correct |
17 |
Correct |
2 ms |
2652 KB |
correct |
18 |
Correct |
1 ms |
2652 KB |
correct |
19 |
Correct |
1 ms |
2652 KB |
correct |
20 |
Correct |
1 ms |
2652 KB |
correct |
21 |
Correct |
1 ms |
2756 KB |
correct |
22 |
Correct |
1 ms |
2652 KB |
correct |
23 |
Correct |
1 ms |
2652 KB |
correct |
24 |
Correct |
1 ms |
2652 KB |
correct |
25 |
Correct |
1 ms |
2652 KB |
correct |
26 |
Correct |
1 ms |
2652 KB |
correct |
27 |
Correct |
1 ms |
2652 KB |
correct |
28 |
Correct |
2 ms |
2652 KB |
correct |
29 |
Correct |
1 ms |
2652 KB |
correct |
30 |
Correct |
1 ms |
2648 KB |
correct |
31 |
Correct |
1 ms |
2652 KB |
correct |
32 |
Correct |
1 ms |
2652 KB |
correct |
33 |
Correct |
1 ms |
2652 KB |
correct |
34 |
Correct |
20 ms |
3672 KB |
correct |
35 |
Correct |
23 ms |
3500 KB |
correct |
36 |
Correct |
17 ms |
3416 KB |
correct |
37 |
Correct |
5 ms |
2772 KB |
correct |
38 |
Correct |
24 ms |
3676 KB |
correct |
39 |
Correct |
18 ms |
3572 KB |
correct |
40 |
Correct |
15 ms |
3416 KB |
correct |
41 |
Correct |
21 ms |
3676 KB |
correct |
42 |
Correct |
21 ms |
3672 KB |
correct |
43 |
Correct |
6 ms |
3164 KB |
correct |
44 |
Correct |
5 ms |
3164 KB |
correct |
45 |
Correct |
6 ms |
3064 KB |
correct |
46 |
Correct |
5 ms |
2908 KB |
correct |
47 |
Correct |
3 ms |
2908 KB |
correct |
48 |
Correct |
2 ms |
2652 KB |
correct |
49 |
Correct |
3 ms |
2668 KB |
correct |
50 |
Correct |
3 ms |
2908 KB |
correct |
51 |
Correct |
6 ms |
3160 KB |
correct |
52 |
Correct |
5 ms |
3164 KB |
correct |
53 |
Correct |
5 ms |
2908 KB |
correct |
54 |
Correct |
6 ms |
3164 KB |
correct |
55 |
Correct |
9 ms |
3164 KB |
correct |
56 |
Correct |
6 ms |
3160 KB |
correct |
57 |
Correct |
5 ms |
3160 KB |
correct |
58 |
Correct |
1 ms |
2396 KB |
correct |
59 |
Correct |
1 ms |
2652 KB |
correct |
60 |
Correct |
64 ms |
5248 KB |
correct |
61 |
Correct |
96 ms |
6720 KB |
correct |
62 |
Correct |
102 ms |
6740 KB |
correct |
63 |
Correct |
96 ms |
6740 KB |
correct |
64 |
Correct |
69 ms |
6668 KB |
correct |
65 |
Correct |
71 ms |
6736 KB |
correct |
66 |
Correct |
104 ms |
6640 KB |
correct |
67 |
Correct |
102 ms |
6720 KB |
correct |
68 |
Correct |
94 ms |
6712 KB |
correct |
69 |
Correct |
99 ms |
6660 KB |
correct |
70 |
Correct |
1 ms |
2396 KB |
correct |
71 |
Correct |
78 ms |
6740 KB |
correct |
72 |
Correct |
87 ms |
6736 KB |
correct |
73 |
Correct |
1 ms |
2392 KB |
correct |
74 |
Correct |
92 ms |
6740 KB |
correct |
75 |
Correct |
83 ms |
6484 KB |
correct |
76 |
Correct |
42 ms |
4192 KB |
correct |
77 |
Correct |
94 ms |
6736 KB |
correct |
78 |
Correct |
91 ms |
6664 KB |
correct |
79 |
Correct |
89 ms |
6736 KB |
correct |
80 |
Correct |
92 ms |
6492 KB |
correct |
81 |
Correct |
64 ms |
5972 KB |
correct |
82 |
Correct |
104 ms |
6576 KB |
correct |
83 |
Correct |
69 ms |
4752 KB |
correct |
84 |
Correct |
25 ms |
4896 KB |
correct |
85 |
Correct |
23 ms |
4696 KB |
correct |
86 |
Correct |
18 ms |
4216 KB |
correct |
87 |
Correct |
14 ms |
3676 KB |
correct |
88 |
Correct |
11 ms |
3432 KB |
correct |
89 |
Correct |
13 ms |
3420 KB |
correct |
90 |
Correct |
11 ms |
3420 KB |
correct |
91 |
Correct |
5 ms |
2716 KB |
correct |
92 |
Correct |
4 ms |
2652 KB |
correct |
93 |
Correct |
23 ms |
4824 KB |
correct |
94 |
Correct |
17 ms |
4224 KB |
correct |
95 |
Correct |
17 ms |
3932 KB |
correct |
96 |
Correct |
11 ms |
3416 KB |
correct |
97 |
Correct |
15 ms |
3432 KB |
correct |
98 |
Correct |
14 ms |
3788 KB |
correct |
99 |
Correct |
12 ms |
3632 KB |
correct |
100 |
Correct |
7 ms |
2908 KB |
correct |
101 |
Correct |
5 ms |
2652 KB |
correct |
102 |
Correct |
20 ms |
4700 KB |
correct |
103 |
Correct |
20 ms |
4700 KB |
correct |
104 |
Correct |
21 ms |
4696 KB |
correct |
105 |
Correct |
20 ms |
4700 KB |
correct |