QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#225720 | #6402. MEXimum Spanning Tree | YouKn0wWho | WA | 80ms | 3840kb | C++23 | 6.9kb | 2023-10-25 01:56:53 | 2023-10-25 01:56:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1005, MAX_INDEPENDENT_SET_SIZE = 1005;
using T = pair<int, int>;
struct GroundSetElement {
int color;
T edge;
bool in_independent_set; // if this element is in the IS
int independent_set_position; // the index of this element in the IS
};
vector<GroundSetElement> elements;
vector<int> independent_set; // stores the indices of the ground set elements
struct GraphicOracle {
struct GraphBasis {
vector<int> par, rnk, sz;
int c;
GraphBasis(){}
GraphBasis(int n) : par(n + 1), rnk(n + 1, 0), sz(n + 1, 1), c(n) {
for (int i = 1; i <= n; ++i) par[i] = i;
}
int find(int i) {
return (par[i] == i ? i : (par[i] = find(par[i])));
}
bool same(int i, int j) {
return find(i) == find(j);
}
int get_size(int i) {
return sz[find(i)];
}
int count() {
return c; //connected components
}
int add_edge(T edge) { // add an independent edge
auto [i, j] = edge;
if ((i = find(i)) == (j = find(j))) return -1;
else --c;
if (rnk[i] > rnk[j]) swap(i, j);
par[i] = j;
sz[j] += sz[i];
if (rnk[i] == rnk[j]) rnk[j]++;
return j;
}
bool independent_with(T edge) {
return !same(edge.first, edge.second);
}
};
int n;
GraphBasis basis; // of independent set
static const int LG = 10;
vector<int> g[N];
int par[N][LG + 1], dep[N];
void dfs(int u, int p = 0) {
par[u][0] = p;
dep[u] = dep[p] + 1;
for (int i = 1; i <= LG; i++) par[u][i] = par[par[u][i - 1]][i - 1];
for (auto v: g[u]) if (v != p) {
dfs(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int k = LG; k >= 0; k--) if (dep[par[u][k]] >= dep[v]) u = par[u][k];
if (u == v) return u;
for (int k = LG; k >= 0; k--) if (par[u][k] != par[v][k]) u = par[u][k], v = par[v][k];
return par[u][0];
}
int dist(int u, int v) {
int l = lca(u, v);
return dep[u] + dep[v] - (dep[l] << 1);
}
GraphicOracle(){}
GraphicOracle(int n) : n(n) {}
// can we insert elements[id] without breaking independence?
bool can_insert(int id) {
return basis.independent_with(elements[id].edge);
}
// can we insert elements[inserted_id] and remove elements[removed_id]
// without breaking independence?
bool can_exchange(int inserted_id, int removed_id) {
if (basis.independent_with(elements[inserted_id].edge)) {
return true;
}
auto [u, v] = elements[inserted_id].edge;
auto [p, q] = elements[removed_id].edge;
if (dist(u, v) == dist(u, p) + dist(q, v) + 1) {
return true;
}
swap(u, v);
if (dist(u, v) == dist(u, p) + dist(q, v) + 1) {
return true;
}
return false;
}
// prepare the oracle for the current independent set
void prepare() {
basis = GraphBasis(n);
for (int i = 0; i < independent_set.size(); i++) {
basis.add_edge(elements[independent_set[i]].edge);
}
for (int i = 1; i <= n; i++) {
g[i].clear();
dep[i] = 0;
}
for (auto i: independent_set) {
auto [u, v] = elements[i].edge;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (!dep[i]) {
dfs(i);
}
}
}
}graphic_oracle;
struct ColorfulOracle {
int color_count;
vector<bool> color_used;
ColorfulOracle(int _color_count = 0) {
color_count = _color_count;
color_used = vector<bool>(color_count + 1, false);
}
// can we insert elements[id] without breaking independence?
bool can_insert(int id) {
int inserted_color = elements[id].color;
return !color_used[inserted_color];
}
// can we insert elements[inserted_id] and remove elements[removed_id]
// without breaking independence?
bool can_exchange(int inserted_id, int removed_id) {
int inserted_color = elements[inserted_id].color;
int removed_color = elements[removed_id].color;
if (!color_used[inserted_color]) return true;
return inserted_color == removed_color;
}
// prepare the oracle for the current independent set
void prepare() {
for (int c = 0; c < color_count; c++) {
color_used[c] = false;
}
for (auto idx : independent_set) {
color_used[elements[idx].color] = true;
}
}
}colorful_oracle;
// try to increment the size of the independent set
// implementation details: https://codeforces.com/blog/entry/69287#:~:text=Implementation%20and%20complexity
bool augment() {
// swapping the oracles might run faster
auto oracle1 = colorful_oracle;
auto oracle2 = graphic_oracle;
oracle1.prepare();
oracle2.prepare();
const int SOURCE = -1;
const int NOT_VISITED = -2;
const int NOT_FOUND = -3;
int sz = elements.size();
vector<int> par(sz, NOT_VISITED);
int endpoint = NOT_FOUND;
queue<int> q;
for (int i = 0; i < sz; i++) {
if (oracle1.can_insert(i)) {
par[i] = SOURCE;
q.push(i);
}
}
while (q.size()) {
int cur = q.front();
q.pop();
if (elements[cur].in_independent_set) {
for (int to = 0; to < sz; to++) {
if (par[to] != NOT_VISITED) continue;
if (!oracle1.can_exchange(to, cur)) continue;
par[to] = cur;
q.push(to);
}
} else {
if (oracle2.can_insert(cur)) {
endpoint = cur;
break;
}
for (auto to : independent_set) {
if (par[to] != NOT_VISITED) continue;
if (!oracle2.can_exchange(cur, to)) continue;
par[to] = cur;
q.push(to);
}
}
}
if (endpoint == NOT_FOUND) return false;
do {
elements[endpoint].in_independent_set ^= true;
endpoint = par[endpoint];
} while (endpoint != SOURCE);
independent_set.clear();
for (int i = 0; i < sz; i++) {
if (elements[i].in_independent_set) {
elements[i].independent_set_position = independent_set.size();
independent_set.push_back(i);
}
}
return true;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
int u[m + 1], v[m + 1], w[m + 1], a[m + 1];
for (int i = 1; i <= m; i++) {
cin >> u[i] >> v[i] >> w[i];
a[i] = i;
}
sort(a + 1, a + m + 1, [&](int i, int j) {return w[i] < w[j];});
int ans = 0;
graphic_oracle = GraphicOracle(n);
colorful_oracle = ColorfulOracle(n);
int cur = 0;
for (int _i = 1; _i <= m; _i++) {
int i = a[_i];
if (w[i] > cur) break;
if (w[i] == cur) ++cur;
elements.emplace_back();
elements.back().color = w[i];
elements.back().edge = T(u[i], v[i]);
elements.back().in_independent_set = false;
while (augment());
if (independent_set.size() != cur) {
break;
}
ans = cur;
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3652kb
input:
4 4 1 2 0 2 3 1 1 3 1 3 4 2
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 37ms
memory: 3840kb
input:
1000 1000 647 790 6 91 461 435 90 72 74 403 81 240 893 925 395 817 345 136 88 71 821 831 962 53 164 270 298 14 550 317 99 580 81 26 477 488 977 474 861 413 483 167 872 675 17 819 327 449 594 242 68 381 983 319 867 582 358 869 225 669 274 352 392 40 388 998 246 477 44 508 979 286 483 776 71 580 438 6...
output:
502
result:
ok 1 number(s): "502"
Test #3:
score: 0
Accepted
time: 80ms
memory: 3776kb
input:
900 1000 232 890 107 425 399 19 5 74 753 105 333 163 779 42 582 359 647 524 767 409 48 239 780 443 484 489 546 97 634 562 627 866 714 500 357 590 60 728 591 407 686 210 547 32 370 76 772 500 407 584 772 73 699 69 332 847 516 829 754 727 562 756 678 819 303 128 781 667 263 535 672 767 89 762 216 878 ...
output:
801
result:
ok 1 number(s): "801"
Test #4:
score: 0
Accepted
time: 34ms
memory: 3824kb
input:
500 1000 381 118 230 258 331 21 403 71 207 170 2 125 467 99 6 369 100 492 70 187 352 99 163 123 135 51 352 461 175 486 275 194 236 299 14 19 16 1 68 7 229 316 235 433 320 411 179 463 112 329 326 464 169 52 377 93 51 84 336 335 42 240 379 182 496 344 377 481 195 88 286 491 199 425 165 37 292 44 197 2...
output:
403
result:
ok 1 number(s): "403"
Test #5:
score: -100
Wrong Answer
time: 61ms
memory: 3776kb
input:
900 1000 698 454 775 6 762 755 585 346 86 220 245 253 54 634 184 634 249 234 454 363 546 520 799 501 103 346 134 381 346 792 835 782 614 359 220 485 634 68 54 411 220 439 701 364 791 220 876 15 70 346 317 220 461 769 577 431 117 488 107 706 160 39 864 220 172 721 431 400 556 801 364 716 37 845 83 99...
output:
733
result:
wrong answer 1st numbers differ - expected: '801', found: '733'