#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cout << #x << " :: " << x << endl;
#define _ << " " <<
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
using vi = vector<int>;
const int mxN = 505;
const int mxM = (500*499)/2 + 10;
const int mxQ = 8000;
int N, M, q = 0;
int U[mxM], V[mxM];
vector<int> al[mxN];
vector<int> span;
int golden[mxM];
struct UnionFind {
vector<int> pa, sz; int comp;
UnionFind(int n) { pa.assign(n,0); iota(ALL(pa),0); sz.assign(n,0); comp = n;}
int findset(int i) { return (i == pa[i]) ? i : (pa[i] = findset(pa[i])); }
bool unionset(int i, int j) {
int x = findset(i), y = findset(j);
if (x != y) {
--comp;
if (sz[x] < sz[y]) swap(x,y);
sz[x] += sz[y], pa[y] = x;
return true;
} return false;
}
};
int ask(vector<int> edges, bool sub=false) {
//assert(++q <= mxQ);
UnionFind UF(N);
for (int e : edges) {
int u = U[e], v = V[e];
assert(UF.unionset(u,v));
UF.unionset(u,v);
}
int cnt = 0;
for (int e : span) if (UF.unionset(U[e],V[e])) {
edges.push_back(e);
cnt += (golden[e] == 1);
}
//cout << "ASK: "; for (int e : edges) { cout << U[e] _ V[e] << ", "; } cout << endl;
assert(UF.comp == 1);
return count_common_roads(edges) - (sub ? cnt : 0);
}
int pa[mxN], pre[mxN], low[mxN], dfc, dfr;
vi ears[mxM];
int esz = 0;
vi* cur[mxN];
vector<vi*> decomp;
void dfs(int u, int p) {
pre[u] = low[u] = dfc++;
vector<vi*> vec;
for (int e : al[u]) {
int v = U[e]^V[e]^u;
if (v == p) continue;
if (pre[v] == -1) {
pa[v] = e;
dfs(v,u);
span.push_back(e);
if (low[v] > pre[u]) { // bridge
golden[e] = 1;
if (cur[v] != nullptr) decomp.push_back(cur[v]);
} else {
low[u] = min(low[u],low[v]);
cur[v]->push_back(e);
vec.push_back(cur[v]);
}
} else if (pre[v] < pre[u]) {
low[u] = min(low[u],pre[v]);
ears[esz] = {v,e};
vec.push_back(&ears[esz++]);
}
}
int fst = 0;
cur[u] = nullptr;
for (vi* x : vec) {
int d = pre[*x->begin()];
if (d > low[u] || (d == low[u] && ++fst > 1)) decomp.push_back(x);
else cur[u] = x;
}
if (u == dfr && cur[u] != nullptr) decomp.push_back(cur[u]);
}
void solve(vi* ear) {
//{cout << "EAR " << SZ(*ear) << ": ";
//int u = *ear->begin();
//bool fst = 1;
//for (int e : (*ear)) {
// if (fst) fst = 0;
// else u ^= U[e]^V[e];
// cout << u << ' ';
//}
//cout << endl;}
vector<int> x[2];
int u = (*ear)[0], v = u, f = (*ear)[1];
FOR(i,1,SZ(*ear)-1){
int e = (*ear)[i];
if (i > 1) x[golden[e] != -1].push_back(e);
v ^= U[e]^V[e];
}
while (v != u) {
x[golden[pa[v]] != -1].push_back(pa[v]);
v ^= U[pa[v]]^V[pa[v]];
}
if (x[0].empty());
else if (x[1].empty()) {
vector<int> que;
//cout << f << " CASE 1: ";
//for (int& e : x[0]) cout << e << ": " << U[e] _ V[e] << ", ";
//cout << endl;
int mx = ask(x[0]);
FOR(i,0,SZ(x[0])-1){
//TRACE("ask" _ x[0][i] _ "without" _ U[x[0][i]] _ V[x[0][i]]);
swap(x[0][i],f);
int q = ask(x[0]);
swap(x[0][i],f);
que.push_back(q);
mx = max(mx,q);
}
FOR(i,0,SZ(x[0])-1){
//TRACE(i _ que[i] _ mx);
golden[x[0][i]] = que[i] < mx; // removed decreases
}
} else {
vector<int> edges(x[0]);
FOR(i,1,SZ(x[1])-1) edges.push_back(x[1][i]);
edges.push_back(f);
//cout << f << " CASE 2: ";
//for (int& e : x[0]) cout << e << ": " << U[e] _ V[e] << ", ";
//cout << endl;
int r = ask(edges);
FOR(i,0,SZ(x[0])-1){
swap(edges[i],x[1][0]);
int q = ask(edges);
swap(edges[i],x[1][0]);
golden[x[0][i]] = ((golden[x[1][0]] && q == r) || q < r);
}
}
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
N = n;
M = SZ(u);
FOR(i,0,M-1){
U[i] = u[i], V[i] = v[i];
al[u[i]].push_back(i);
al[v[i]].push_back(i);
}
fill(golden,golden+M,-1);
fill(pre,pre+n,-1); dfc = dfr = 0;
dfs(0,-1);
for (vi* x : decomp) solve(x);
//for (int& e : span) { TRACE(e _ ":" _ U[e] _ V[e] _ golden[e]); }
FOR(i,0,N-1) {
for (;;) {
vector<int> all;
for (int e : al[i]) if (golden[e] == -1) all.push_back(e);
if (all.empty()) break;
if (!ask(all,true)) {
for (int e : all) golden[e] = 0;
break;
}
int lo = -1, hi = SZ(all)-1;
while (hi-lo > 1) {
int mid = (lo+hi)/2;
vector<int> edges;
FOR(i,lo+1,mid) edges.push_back(all[i]);
if (ask(edges,true)) hi = mid;
else lo = mid;
}
golden[all[hi]] = 1;
}
}
vector<int> ans;
FOR(i,0,M-1) if (golden[i] == 1) ans.push_back(i);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3328 KB |
correct |
2 |
Correct |
6 ms |
3328 KB |
correct |
3 |
Correct |
6 ms |
3328 KB |
correct |
4 |
Correct |
6 ms |
3328 KB |
correct |
5 |
Correct |
6 ms |
3328 KB |
correct |
6 |
Correct |
6 ms |
3328 KB |
correct |
7 |
Correct |
6 ms |
3328 KB |
correct |
8 |
Correct |
6 ms |
3328 KB |
correct |
9 |
Correct |
6 ms |
3328 KB |
correct |
10 |
Correct |
6 ms |
3328 KB |
correct |
11 |
Correct |
6 ms |
3328 KB |
correct |
12 |
Correct |
6 ms |
3328 KB |
correct |
13 |
Correct |
6 ms |
3328 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3328 KB |
correct |
2 |
Correct |
6 ms |
3328 KB |
correct |
3 |
Correct |
6 ms |
3328 KB |
correct |
4 |
Correct |
6 ms |
3328 KB |
correct |
5 |
Correct |
6 ms |
3328 KB |
correct |
6 |
Correct |
6 ms |
3328 KB |
correct |
7 |
Correct |
6 ms |
3328 KB |
correct |
8 |
Correct |
6 ms |
3328 KB |
correct |
9 |
Correct |
6 ms |
3328 KB |
correct |
10 |
Correct |
6 ms |
3328 KB |
correct |
11 |
Correct |
6 ms |
3328 KB |
correct |
12 |
Correct |
6 ms |
3328 KB |
correct |
13 |
Correct |
6 ms |
3328 KB |
correct |
14 |
Correct |
9 ms |
3456 KB |
correct |
15 |
Correct |
9 ms |
3456 KB |
correct |
16 |
Correct |
8 ms |
3456 KB |
correct |
17 |
Correct |
9 ms |
3328 KB |
correct |
18 |
Correct |
7 ms |
3328 KB |
correct |
19 |
Correct |
8 ms |
3456 KB |
correct |
20 |
Correct |
8 ms |
3328 KB |
correct |
21 |
Correct |
8 ms |
3328 KB |
correct |
22 |
Correct |
7 ms |
3328 KB |
correct |
23 |
Correct |
7 ms |
3328 KB |
correct |
24 |
Correct |
7 ms |
3328 KB |
correct |
25 |
Correct |
6 ms |
3328 KB |
correct |
26 |
Correct |
7 ms |
3328 KB |
correct |
27 |
Correct |
7 ms |
3328 KB |
correct |
28 |
Correct |
7 ms |
3328 KB |
correct |
29 |
Correct |
7 ms |
3328 KB |
correct |
30 |
Correct |
7 ms |
3328 KB |
correct |
31 |
Correct |
7 ms |
3328 KB |
correct |
32 |
Correct |
8 ms |
3328 KB |
correct |
33 |
Correct |
7 ms |
3328 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3328 KB |
correct |
2 |
Correct |
6 ms |
3328 KB |
correct |
3 |
Correct |
6 ms |
3328 KB |
correct |
4 |
Correct |
6 ms |
3328 KB |
correct |
5 |
Correct |
6 ms |
3328 KB |
correct |
6 |
Correct |
6 ms |
3328 KB |
correct |
7 |
Correct |
6 ms |
3328 KB |
correct |
8 |
Correct |
6 ms |
3328 KB |
correct |
9 |
Correct |
6 ms |
3328 KB |
correct |
10 |
Correct |
6 ms |
3328 KB |
correct |
11 |
Correct |
6 ms |
3328 KB |
correct |
12 |
Correct |
6 ms |
3328 KB |
correct |
13 |
Correct |
6 ms |
3328 KB |
correct |
14 |
Correct |
9 ms |
3456 KB |
correct |
15 |
Correct |
9 ms |
3456 KB |
correct |
16 |
Correct |
8 ms |
3456 KB |
correct |
17 |
Correct |
9 ms |
3328 KB |
correct |
18 |
Correct |
7 ms |
3328 KB |
correct |
19 |
Correct |
8 ms |
3456 KB |
correct |
20 |
Correct |
8 ms |
3328 KB |
correct |
21 |
Correct |
8 ms |
3328 KB |
correct |
22 |
Correct |
7 ms |
3328 KB |
correct |
23 |
Correct |
7 ms |
3328 KB |
correct |
24 |
Correct |
7 ms |
3328 KB |
correct |
25 |
Correct |
6 ms |
3328 KB |
correct |
26 |
Correct |
7 ms |
3328 KB |
correct |
27 |
Correct |
7 ms |
3328 KB |
correct |
28 |
Correct |
7 ms |
3328 KB |
correct |
29 |
Correct |
7 ms |
3328 KB |
correct |
30 |
Correct |
7 ms |
3328 KB |
correct |
31 |
Correct |
7 ms |
3328 KB |
correct |
32 |
Correct |
8 ms |
3328 KB |
correct |
33 |
Correct |
7 ms |
3328 KB |
correct |
34 |
Correct |
97 ms |
5756 KB |
correct |
35 |
Correct |
99 ms |
5628 KB |
correct |
36 |
Correct |
77 ms |
4992 KB |
correct |
37 |
Correct |
22 ms |
3476 KB |
correct |
38 |
Correct |
99 ms |
5628 KB |
correct |
39 |
Correct |
91 ms |
5372 KB |
correct |
40 |
Correct |
75 ms |
4992 KB |
correct |
41 |
Correct |
95 ms |
5628 KB |
correct |
42 |
Correct |
94 ms |
5628 KB |
correct |
43 |
Correct |
41 ms |
4608 KB |
correct |
44 |
Correct |
35 ms |
4352 KB |
correct |
45 |
Correct |
39 ms |
4480 KB |
correct |
46 |
Correct |
34 ms |
4224 KB |
correct |
47 |
Correct |
22 ms |
3712 KB |
correct |
48 |
Correct |
11 ms |
3328 KB |
correct |
49 |
Correct |
16 ms |
3584 KB |
correct |
50 |
Correct |
21 ms |
3832 KB |
correct |
51 |
Correct |
40 ms |
4480 KB |
correct |
52 |
Correct |
35 ms |
4352 KB |
correct |
53 |
Correct |
32 ms |
4224 KB |
correct |
54 |
Correct |
41 ms |
4608 KB |
correct |
55 |
Correct |
48 ms |
4480 KB |
correct |
56 |
Correct |
49 ms |
4488 KB |
correct |
57 |
Correct |
48 ms |
4608 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3328 KB |
correct |
2 |
Correct |
6 ms |
3328 KB |
correct |
3 |
Correct |
363 ms |
10300 KB |
correct |
4 |
Correct |
644 ms |
13804 KB |
correct |
5 |
Correct |
653 ms |
13804 KB |
correct |
6 |
Correct |
625 ms |
13804 KB |
correct |
7 |
Correct |
633 ms |
13676 KB |
correct |
8 |
Correct |
631 ms |
13804 KB |
correct |
9 |
Correct |
656 ms |
13760 KB |
correct |
10 |
Correct |
658 ms |
13676 KB |
correct |
11 |
Correct |
658 ms |
13808 KB |
correct |
12 |
Correct |
662 ms |
13724 KB |
correct |
13 |
Correct |
6 ms |
3328 KB |
correct |
14 |
Correct |
639 ms |
13676 KB |
correct |
15 |
Correct |
641 ms |
13804 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3328 KB |
correct |
2 |
Correct |
6 ms |
3328 KB |
correct |
3 |
Correct |
6 ms |
3328 KB |
correct |
4 |
Correct |
6 ms |
3328 KB |
correct |
5 |
Correct |
6 ms |
3328 KB |
correct |
6 |
Correct |
6 ms |
3328 KB |
correct |
7 |
Correct |
6 ms |
3328 KB |
correct |
8 |
Correct |
6 ms |
3328 KB |
correct |
9 |
Correct |
6 ms |
3328 KB |
correct |
10 |
Correct |
6 ms |
3328 KB |
correct |
11 |
Correct |
6 ms |
3328 KB |
correct |
12 |
Correct |
6 ms |
3328 KB |
correct |
13 |
Correct |
6 ms |
3328 KB |
correct |
14 |
Correct |
9 ms |
3456 KB |
correct |
15 |
Correct |
9 ms |
3456 KB |
correct |
16 |
Correct |
8 ms |
3456 KB |
correct |
17 |
Correct |
9 ms |
3328 KB |
correct |
18 |
Correct |
7 ms |
3328 KB |
correct |
19 |
Correct |
8 ms |
3456 KB |
correct |
20 |
Correct |
8 ms |
3328 KB |
correct |
21 |
Correct |
8 ms |
3328 KB |
correct |
22 |
Correct |
7 ms |
3328 KB |
correct |
23 |
Correct |
7 ms |
3328 KB |
correct |
24 |
Correct |
7 ms |
3328 KB |
correct |
25 |
Correct |
6 ms |
3328 KB |
correct |
26 |
Correct |
7 ms |
3328 KB |
correct |
27 |
Correct |
7 ms |
3328 KB |
correct |
28 |
Correct |
7 ms |
3328 KB |
correct |
29 |
Correct |
7 ms |
3328 KB |
correct |
30 |
Correct |
7 ms |
3328 KB |
correct |
31 |
Correct |
7 ms |
3328 KB |
correct |
32 |
Correct |
8 ms |
3328 KB |
correct |
33 |
Correct |
7 ms |
3328 KB |
correct |
34 |
Correct |
97 ms |
5756 KB |
correct |
35 |
Correct |
99 ms |
5628 KB |
correct |
36 |
Correct |
77 ms |
4992 KB |
correct |
37 |
Correct |
22 ms |
3476 KB |
correct |
38 |
Correct |
99 ms |
5628 KB |
correct |
39 |
Correct |
91 ms |
5372 KB |
correct |
40 |
Correct |
75 ms |
4992 KB |
correct |
41 |
Correct |
95 ms |
5628 KB |
correct |
42 |
Correct |
94 ms |
5628 KB |
correct |
43 |
Correct |
41 ms |
4608 KB |
correct |
44 |
Correct |
35 ms |
4352 KB |
correct |
45 |
Correct |
39 ms |
4480 KB |
correct |
46 |
Correct |
34 ms |
4224 KB |
correct |
47 |
Correct |
22 ms |
3712 KB |
correct |
48 |
Correct |
11 ms |
3328 KB |
correct |
49 |
Correct |
16 ms |
3584 KB |
correct |
50 |
Correct |
21 ms |
3832 KB |
correct |
51 |
Correct |
40 ms |
4480 KB |
correct |
52 |
Correct |
35 ms |
4352 KB |
correct |
53 |
Correct |
32 ms |
4224 KB |
correct |
54 |
Correct |
41 ms |
4608 KB |
correct |
55 |
Correct |
48 ms |
4480 KB |
correct |
56 |
Correct |
49 ms |
4488 KB |
correct |
57 |
Correct |
48 ms |
4608 KB |
correct |
58 |
Correct |
6 ms |
3328 KB |
correct |
59 |
Correct |
6 ms |
3328 KB |
correct |
60 |
Correct |
363 ms |
10300 KB |
correct |
61 |
Correct |
644 ms |
13804 KB |
correct |
62 |
Correct |
653 ms |
13804 KB |
correct |
63 |
Correct |
625 ms |
13804 KB |
correct |
64 |
Correct |
633 ms |
13676 KB |
correct |
65 |
Correct |
631 ms |
13804 KB |
correct |
66 |
Correct |
656 ms |
13760 KB |
correct |
67 |
Correct |
658 ms |
13676 KB |
correct |
68 |
Correct |
658 ms |
13808 KB |
correct |
69 |
Correct |
662 ms |
13724 KB |
correct |
70 |
Correct |
6 ms |
3328 KB |
correct |
71 |
Correct |
639 ms |
13676 KB |
correct |
72 |
Correct |
641 ms |
13804 KB |
correct |
73 |
Correct |
6 ms |
3328 KB |
correct |
74 |
Correct |
635 ms |
13676 KB |
correct |
75 |
Correct |
615 ms |
13492 KB |
correct |
76 |
Correct |
236 ms |
7152 KB |
correct |
77 |
Correct |
646 ms |
13712 KB |
correct |
78 |
Correct |
668 ms |
13716 KB |
correct |
79 |
Correct |
665 ms |
13676 KB |
correct |
80 |
Correct |
658 ms |
13460 KB |
correct |
81 |
Correct |
528 ms |
12012 KB |
correct |
82 |
Correct |
654 ms |
13420 KB |
correct |
83 |
Correct |
391 ms |
8656 KB |
correct |
84 |
Correct |
273 ms |
9968 KB |
correct |
85 |
Correct |
237 ms |
9072 KB |
correct |
86 |
Correct |
154 ms |
7280 KB |
correct |
87 |
Correct |
111 ms |
6140 KB |
correct |
88 |
Correct |
90 ms |
5500 KB |
correct |
89 |
Correct |
95 ms |
5372 KB |
correct |
90 |
Correct |
79 ms |
4992 KB |
correct |
91 |
Correct |
40 ms |
3456 KB |
correct |
92 |
Correct |
27 ms |
3328 KB |
correct |
93 |
Correct |
218 ms |
9072 KB |
correct |
94 |
Correct |
150 ms |
7160 KB |
correct |
95 |
Correct |
145 ms |
7200 KB |
correct |
96 |
Correct |
84 ms |
5120 KB |
correct |
97 |
Correct |
95 ms |
5500 KB |
correct |
98 |
Correct |
105 ms |
6140 KB |
correct |
99 |
Correct |
91 ms |
5500 KB |
correct |
100 |
Correct |
54 ms |
3840 KB |
correct |
101 |
Correct |
30 ms |
3456 KB |
correct |
102 |
Correct |
286 ms |
8568 KB |
correct |
103 |
Correct |
306 ms |
8688 KB |
correct |
104 |
Correct |
295 ms |
8568 KB |
correct |
105 |
Correct |
295 ms |
8568 KB |
correct |