QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#319349 | #4829. Mark on a Graph | duongnc000 | 0 | 2ms | 4192kb | C++20 | 5.9kb | 2024-02-02 14:46:11 | 2024-02-02 14:46:12 |
answer
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
// https://i11www.iti.kit.edu/extra/publications/sw-fclt-05_t.pdf
// O(n + m * sqrt(m)) process() calls for graphs without loop or multiedges
void find_3_cycles(int n, const vector<array<int, 2>> &edge, auto process){
int m = (int)edge.size();
vector<int> deg(n), order, pos(n);
vector<vector<int>> appear(m + 1), adj(n), found(n);
for(auto [u, v]: edge) ++ deg[u], ++ deg[v];
for(auto u = 0; u < n; ++ u) appear[deg[u]].push_back(u);
for(auto d = m; d >= 0; -- d) order.insert(order.end(), appear[d].begin(), appear[d].end());
for(auto i = 0; i < n; ++ i) pos[order[i]] = i;
for(auto i = 0; i < m; ++ i){
int u = pos[edge[i][0]], v = pos[edge[i][1]];
adj[u].push_back(v), adj[v].push_back(u);
}
for(auto u = 0; u < n; ++ u) for(auto v: adj[u]) if(u < v){
for(auto i = 0, j = 0; i < (int)found[u].size() && j < (int)found[v].size(); ){
if(found[u][i] == found[v][j]){
process(order[u], order[v], order[found[u][i]]);
++ i, ++ j;
}
else if(found[u][i] < found[v][j]) ++ i;
else ++ j;
}
found[v].push_back(u);
}
}
template<bool Enable_small_to_large = true>
struct disjoint_set{
int n, _classes;
vector<int> p;
disjoint_set(int n): n(n), _classes(n), p(n, -1){ }
int make_set(){
p.push_back(-1);
++ _classes;
return n ++;
}
int classes() const{
return _classes;
}
int root(int u){
return p[u] < 0 ? u : p[u] = root(p[u]);
}
bool share(int a, int b){
return root(a) == root(b);
}
int size(int u){
return -p[root(u)];
}
bool merge(int u, int v){
u = root(u), v = root(v);
if(u == v) return false;
-- _classes;
if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v);
p[u] += p[v], p[v] = u;
return true;
}
bool merge(int u, int v, auto act){
u = root(u), v = root(v);
if(u == v) return false;
-- _classes;
bool swapped = false;
if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v), swapped = true;
p[u] += p[v], p[v] = u;
act(u, v, swapped);
return true;
}
void clear(){
_classes = n;
fill(p.begin(), p.end(), -1);
}
vector<vector<int>> group_up(){
vector<vector<int>> g(n);
for(auto i = 0; i < n; ++ i) g[root(i)].push_back(i);
g.erase(remove_if(g.begin(), g.end(), [&](auto &s){ return s.empty(); }), g.end());
return g;
}
};
int n, m;
vector<array<int, 2>> G;
map<int, int> adj[1005];
int indeg[1005], av[1005];
void add_edge(int u, int v) {
++indeg[u], ++indeg[v];
adj[u][v]++, adj[v][u]++;
}
bool check(vector<int> &pos) {
for (int i = 0; i < isz(pos); ++i) {
for (int j = i + 1; j < isz(pos); ++j) {
if (adj[pos[i]].find(pos[j]) == adj[pos[i]].end())
return true;
}
}
return false;
}
void solve() {
cin >> n >> m;
disjoint_set dsu(n);
iota(av + 1, av + n + 1, 1);
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
add_edge(u, v);
// dsu.merge(u, v);
// G.push_back({u, v});
}
sort(av + 1, av + n + 1, [&](int x, int y) {
return indeg[x] > indeg[y];
});
int cnt = 0;
vector<int> pos;
vector<pair<int, int>> op;
for (int i = 1; i <= 14; ++i) if (indeg[av[i]] % 2) {
pos.emplace_back(av[i]);
}
while (isz(pos) > 1 and check(pos)) {
int woo1 = -1, woo2 = -1;
for (int i = 0; i < isz(pos); ++i)
for (int j = i + 1; j < isz(pos); ++j)
if (adj[pos[i]].find(pos[j]) == adj[pos[i]].end())
woo1 = i, woo2 = j;
add_edge(pos[woo1], pos[woo2]);
op.emplace_back(pos[woo1], pos[woo2]);
vector<int> npos;
for (int i = 0; i < isz(pos); ++i) if (i != woo1 and i != woo2) npos.emplace_back(pos[i]);
pos = npos;
}
for (int idx : pos) {
for (int j = n; j >= 1; --j) if (adj[idx].find(av[j]) == adj[idx].end()) {
add_edge(av[j], idx);
op.emplace_back(av[j], idx);
break;
}
}
if (isz(op) == 0) {
cout << "ok" << endl;
return;
}
cout << "mark" << endl << isz(op) << endl;
for (auto [u, v] : op) cout << u << " " << v << endl;
// for (int i = 0; i < 10; ++i) cout << indeg[av[i + 1]] << " \n"[i == 9];
// assert(*max_element(indeg, indeg + n) <= 5);
// cout << dsu.classes() << endl;
// find_3_cycles(n, G, process);
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
cout << "Check array size pls sir" << endl;
#endif
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 4036kb
input:
1000 3560 603 151 415 20 102 569 895 552 678 734 24 614 689 518 440 223 751 919 223 433 711 551 502 634 706 583 812 501 514 535 780 751 720 530 532 384 888 139 864 791 292 675 171 881 30 592 464 557 280 299 654 650 894 335 250 532 792 10 83 969 118 771 579 300 852 983 243 940 957 939 817 889 911 319...
output:
mark 4 366 522 611 875 310 108 880 733
input:
1000 3564 652 184 217 396 116 662 865 475 726 163 833 495 648 183 781 363 255 184 383 844 831 639 48 932 279 904 379 167 332 494 484 763 66 4 850 500 64 422 253 49 368 663 405 669 292 133 583 66 999 200 125 457 567 806 856 437 841 125 932 719 339 898 655 721 790 102 267 511 201 790 18 186 497 360 58...
output:
ok
result:
ok all right
Test #2:
score: 100
Accepted
time: 1ms
memory: 4068kb
input:
1000 2000 457 335 160 497 464 992 892 255 853 3 308 301 970 363 541 299 89 418 425 128 626 827 603 854 484 874 755 295 607 483 798 552 356 850 320 357 254 940 675 901 168 525 301 636 520 555 773 910 343 701 889 966 218 529 909 950 71 64 682 284 424 138 721 792 670 544 386 72 654 909 725 235 592 437 ...
output:
mark 3 678 526 864 369 861 727
input:
1000 2003 711 181 426 320 386 503 377 826 97 233 231 792 1 993 437 440 532 381 554 952 660 299 227 182 690 916 584 649 546 673 756 751 178 529 731 827 39 689 519 541 620 568 379 375 755 727 628 115 257 999 70 672 564 74 863 90 99 543 982 741 450 23 411 279 758 705 167 507 78 414 605 104 965 231 50 9...
output:
ok
result:
ok all right
Test #3:
score: 100
Accepted
time: 2ms
memory: 4192kb
input:
1000 5000 449 632 597 26 701 322 249 190 411 770 666 596 989 995 112 861 445 818 544 659 24 680 739 593 344 439 193 932 600 526 574 869 216 918 716 793 259 686 555 993 255 578 659 271 328 524 729 672 39 771 241 866 27 790 417 109 56 403 338 299 387 232 280 306 589 794 833 419 900 802 54 697 539 807 ...
output:
mark 4 396 350 593 202 566 748 869 539
input:
1000 5004 258 506 563 742 458 36 208 588 178 845 559 170 361 150 350 788 619 34 977 914 578 699 634 106 213 340 156 570 83 564 647 61 128 762 345 143 821 596 976 995 155 835 60 376 334 471 116 10 467 792 494 48 464 286 956 247 726 21 405 143 612 419 42 981 693 70 407 899 285 697 256 532 649 530 240 ...
output:
ok
result:
ok all right
Test #4:
score: 100
Accepted
time: 1ms
memory: 4048kb
input:
1000 3156 347 398 792 278 754 442 413 757 391 130 636 625 207 437 81 415 47 974 887 779 524 619 379 894 868 594 653 919 29 117 123 867 632 505 648 147 130 420 495 876 637 659 882 348 462 878 282 646 398 525 419 224 926 448 305 934 855 570 396 345 774 918 336 123 502 491 984 783 845 142 790 594 754 4...
output:
mark 4 951 394 386 796 418 881 964 115
input:
1000 3160 540 43 372 439 654 789 845 121 900 154 734 213 215 143 475 825 787 266 439 852 556 16 417 346 843 426 150 447 841 423 101 811 455 976 125 273 578 437 174 351 693 865 418 309 885 887 287 920 786 474 465 456 28 441 766 866 141 754 668 435 869 225 879 34 721 841 127 255 836 887 615 748 611 74...
output:
ok
result:
ok all right
Test #5:
score: 0
Wrong Answer on the first run
input:
1000 3433 634 21 789 966 541 959 213 381 366 781 107 649 747 122 336 869 222 648 833 972 929 524 712 524 744 525 568 679 634 163 901 501 56 518 128 587 720 117 208 439 860 85 852 168 934 947 34 858 520 568 408 464 232 432 999 504 71 982 957 372 570 436 281 309 410 405 521 275 554 589 4 707 498 148 5...
output:
mark 6 306 942 648 718 316 156 413 371 631 284 214 797
input:
output:
result:
wrong answer Integer parameter [name=k] equals to 6, violates the range [0, 5]