QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#319284#4829. Mark on a Graphduongnc0000 1ms4244kbC++205.9kb2024-02-02 12:14:132024-02-02 12:14:14

Judging History

你现在查看的是最新测评结果

  • [2024-02-02 12:14:14]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:4244kb
  • [2024-02-02 12:14:13]
  • 提交

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 <= 6; ++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 or 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: 0
Wrong Answer
time: 1ms
memory: 4244kb

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
3
733 310
880 733
880 310

input:

1000 3563
269 626
222 295
959 665
534 909
682 161
833 706
199 155
656 841
184 286
383 358
259 86
817 771
37 355
174 167
484 763
209 250
693 401
802 224
974 277
521 380
368 418
676 977
920 112
831 175
730 688
692 125
654 102
757 70
736 226
733 87
956 373
600 137
201 580
844 267
14 201
583 975
557 937...

output:

mark
2
978 353
978 669

result:

wrong answer Token "mark" doesn't correspond to pattern "ok"