QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#241428#6560. Broken Minimum Spanning TreeMtaylorWA 4ms18064kbC++203.4kb2023-11-06 06:19:452023-11-06 06:19:45

Judging History

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

  • [2023-11-06 06:19:45]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:18064kb
  • [2023-11-06 06:19:45]
  • 提交

answer

// Mtaylor
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define endl '\n';
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
    cerr << ' ' << H;
    dbg_out(T...);
}
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
typedef long long ll;
const int N = 3e5 + 5;
const int E = 1e6 + 5;

#define neig(u, v, e, g) \
    for (int e = g.head[u], v; ~e && ((v = g.to[e]), 1); e = g.nxt[e])

class ADJ {
   public:
    int head[E], to[E], nxt[E], n, edgcnt = 0;
    int cst[E];

    void addEdge(int a, int b, int c = 0) {
        nxt[edgcnt] = head[a];
        head[a] = edgcnt;
        to[edgcnt] = b;
        cst[edgcnt] = c;
        edgcnt++;
    }

    void addBiEdge(int a, int b, int c = 0) {
        addEdge(a, b, c);
        addEdge(b, a, c);
    }
    void init(int _n) {
        n = _n;
        memset(head, -1, n * sizeof(head[0]));
        edgcnt = 0;
    }
} adj, adj2;

struct DSUGraph {
    vector<int> id, sz;
    void init(int n) {
        id.assign(n, 0);
        sz.assign(n, 0);
        for (int i = 0; i < n; i++) {
            id[i] = i;
            sz[i] = 1;
        }
    }
    int getid(int u) { return (u == id[u] ? u : id[u] = getid(id[u])); }
    bool sameSet(int u, int v) { return getid(u) == getid(v); }
    void uni(int u, int v) {
        v = getid(v);
        u = getid(u);
        if (u == v) return;
        id[u] = v;
        sz[v] += sz[u];
    }
} dsu;
int u[N], v[N], w[N];
int n, m;
int target;
int r = 0;
void dfs(int u, int cur = 0, int p = -1) {
    if (u == target) {
        r = cur;
        return;
    }
    neig(u, v, e, adj) {
        if (v == p) continue;
        dfs(v, max(cur, adj.cst[e]), u);
    }
}

bool ok[N];
bool needed[N];
void test_case() {
    cin >> n >> m;
    adj.init(n);
    vector<int> ord;
    for (int i = 0; i < m; i++) {
        cin >> u[i] >> v[i] >> w[i];
        --u[i], --v[i];
        ord.pb(i);
    }
    sort(all(ord), [](int &x, int &y) { return w[x] < w[y]; });
    dsu.init(n);
    for (auto &i : ord) {
        if (dsu.sameSet(u[i], v[i]) == 0) {
            needed[i] = 1;
            dsu.uni(u[i], v[i]);
            adj.addBiEdge(u[i], v[i], w[i]);
        }
    }
    for (int i = 0; i < n - 1; i++) {
        ok[i] = 1;
    }
    vector<pair<int, int> > ans;
    for (int i = n - 1; i < m; i++) {
        if (!needed[i]) continue;
        int a = u[i];
        int b = v[i];
        target = b;
        r = 0;
        dsu.init(n);
        for (auto &j : ord) {
            if (ok[j]) {
                dsu.uni(u[j], v[j]);
                if (dsu.sameSet(a, b)) {
                    ok[j] = 0;
                    ok[i] = 1;
                    ans.pb({j, i});
                    break;
                }
            }
        }
    }
    cout << ans.size() << endl;
    for (auto &u : ans) {
        cout << u.fi + 1 << " " << u.se + 1 << endl;
    }
}

int main() {
    // freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    while (T--) {
        test_case();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 18008kb

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: 0
Accepted
time: 0ms
memory: 17940kb

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
5 8

result:

ok correct!

Test #3:

score: 0
Accepted
time: 2ms
memory: 18064kb

input:

2000 1999
1262 1505 968661582
323 1681 787089412
1132 129 88786681
1909 587 762050278
979 1371 230688681
1686 521 980519364
975 191 887826021
869 461 899130441
1433 259 961154249
1718 547 721696188
1254 1042 458319755
1779 267 85751052
1170 813 283230029
309 20 971682908
224 417 255325364
1084 986 7...

output:

0

result:

ok correct!

Test #4:

score: 0
Accepted
time: 2ms
memory: 17968kb

input:

1999 1998
1757 1820 444157563
1757 395 754598547
1757 1571 432619009
1757 1009 456234067
1757 824 935569725
1757 1698 476714469
1757 1420 901765343
1757 1175 225295107
1757 1512 721959801
1757 1585 955067704
1757 1739 635181418
1757 1686 891225461
1757 84 132683224
1757 1696 48915557
1757 1623 42602...

output:

0

result:

ok correct!

Test #5:

score: 0
Accepted
time: 0ms
memory: 17872kb

input:

1999 1998
1345 647 232183406
40 837 279910457
819 857 137486924
255 1378 517489941
827 1565 894953662
1556 1545 898170464
965 877 72248541
1631 298 635713424
895 197 366305735
966 1160 515776809
1870 1638 220711661
1736 220 716014108
1914 1609 759121968
1293 153 272816132
1936 1433 263859075
985 460...

output:

0

result:

ok correct!

Test #6:

score: -100
Wrong Answer
time: 4ms
memory: 17968kb

input:

500 998
105 1 1
105 2 1
105 3 1
105 4 1
105 5 1
105 6 1
105 7 1
105 8 1
105 9 1
105 10 1
105 11 1
105 12 1
105 13 1
105 14 1
105 15 1
105 16 1
105 17 1
105 18 1
105 19 1
105 20 1
105 21 1
105 22 1
105 23 1
105 24 1
105 25 1
105 26 1
105 27 1
105 28 1
105 29 1
105 30 1
105 31 1
105 32 1
105 33 1
105 ...

output:

499
1 500
474 501
3 502
4 503
5 504
6 505
7 506
8 507
9 508
10 509
11 510
12 511
13 512
14 513
15 514
16 515
2 516
18 517
19 518
20 519
21 520
22 521
23 522
24 523
25 524
26 525
27 526
28 527
29 528
30 529
31 530
17 531
32 532
34 533
35 534
36 535
37 536
38 537
39 538
40 539
41 540
42 541
43 542
44 ...

result:

FAIL participant's plan is better than jury!