QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#111090#6560. Broken Minimum Spanning TreeKhNURE_KIVI#WA 2ms3520kbC++203.6kb2023-06-05 19:53:322023-06-05 19:53:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-05 19:53:33]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3520kb
  • [2023-06-05 19:53:32]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
inline bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
inline bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL

const int max_n = 2023, inf = 1000111222;

struct edge {
    int l, r, c, id;
    bool operator < (const edge &x) const {
        return c < x.c || c == x.c && id < x.id;
    }
};

struct dsu {
public:
    int n;
    vector <int> p, cnt;

    inline void make_set (int v) {
        p[v] = v;
    }

    dsu (int n) : n(n) {
        p.resize(n);
        cnt.assign(n, 1);
        for (int i = 0; i < n; i++) {
            make_set(i);
        }
    }

    inline int get (int v) {
        if (p[v] == v) return v;
        return p[v] = get(p[v]); /// compressing path
    }

    inline bool unite (int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) return false;
        if (cnt[a] > cnt[b]) {
            swap(a, b);
        }
        p[a] = b;
        cnt[b] += cnt[a];
        return true;
    }
};

set <pii> g[max_n];

vector <int> path;

inline bool get_path (int a, int b, int p = -1) {
    if (a == b) {
        return true;
    }
    for (auto &i : g[a]) {
        if (i.first == p) {
            continue;
        }
        path.pb(i.second);
        if (get_path(i.first, b, a)) {
            return true;
        }
        path.pop_back();
    }
    return false;
}


int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector <edge> e(m);
    for (int i = 0; i < m; i++) {
        cin >> e[i].l >> e[i].r >> e[i].c;
        e[i].id = i;
        --e[i].l;
        --e[i].r;
        g[e[i].l].insert({e[i].r, i});
        g[e[i].r].insert({e[i].l, i});
    }
    auto E = e;
    sort(all(e));
    dsu t(n);
    vector <edge> id;
    vector <int> need(m);
    for (int i = 0; i < m; i++) {
        if (t.unite(e[i].l, e[i].r)) {
            id.pb(e[i]);
            need[e[i].id] = 1;
        }
    }
    vector <pii> ans;
    for (auto &i : id) {
        if (i.id >= n - 1) {
            path.clear();
            get_path(i.l, i.r);
            bool ok = false;
            for (auto &j : path) {
                if (!need[j]) {
                    ok = true;
                    ans.pb({j, i.id});
                    g[E[j].l].erase({E[j].r, E[j].id});
                    g[E[j].r].erase({E[j].l, E[j].id});
                    g[i.l].insert({i.r, i.id});
                    g[i.r].insert({i.l, i.id});
                    break;
                }
            }
            assert(ok);
        }
    }
    cout << len(ans) << '\n';
    for (auto &i : ans) {
        cout << i.first + 1 << ' ' << i.second + 1 << '\n';
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3516kb

input:

4 4
1 2 10
2 3 3
3 4 1
1 4 4

output:

1
1 4

result:

ok correct!

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3520kb

input:

6 8
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
1 3 1
4 6 1

output:

2
2 7
6 8

result:

wrong answer bad swap 6 8