QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#322826#7415. Fast Spanning TreethangthangWA 1ms3784kbC++173.5kb2024-02-07 20:15:452024-02-07 20:15:46

Judging History

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

  • [2024-02-07 20:15:46]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3784kb
  • [2024-02-07 20:15:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 5;

int n, m;
int w[N], kk[N];

struct edge{
    int a, b, s, idx;
    edge(int _a, int _b, int _s, int _idx){
        a = _a;
        b = _b;
        s = _s;
        idx = _idx;
    }
};

struct handle{
    int a, b, idx;
    handle(int _a, int _b, int _idx){
        a = _a;
        b = _b;
        idx = _idx;
    }
};

struct Comparehandle {
    bool operator()(handle const& p1, handle const& p2)
    {
        return p1.idx > p2.idx;
    }
};

struct event{
    int val, idx, to;
    event(int _val, int _to, int _idx){
        val = _val;
        to = _to;
        idx = _idx;
    }
};

struct Compareevent {
    bool operator()(event const& p1, event const& p2)
    {
        return p1.val > p2.val;
    }
};

struct DSU{
    const int inf = 1e6;

    void add(int &a, int b){
        a += b;
        if (a > inf) a = inf;
    }

    vector <int> par, wei, sz;
    vector <priority_queue <event, vector<event>, Compareevent>> cur;
    priority_queue <handle, vector <handle>, Comparehandle> qu;
    DSU(int n){
        par.resize(n + 1);
        wei.resize(n + 1);
        sz.resize(n + 1);
        cur.resize(n + 1);
        for (int i = 1; i <= n; ++ i) par[i] = i, wei[i] = w[i], sz[i] = 1;
    }

    int find(int u){
        if (u == par[u]) return u;
        return par[u] = find(par[u]);
    }

    int half(int s){
        return (s + 1) / 2;
    }

    void add(edge p){
        if (w[p.a] + w[p.b] >= p.s){
            qu.push(handle(p.a, p.b, p.idx));
            return;
        }

        cur[p.a].push(event(half(p.s - w[p.a] - w[p.b]) + w[p.a], p.b, p.idx));
        cur[p.b].push(event(half(p.s - w[p.a] - w[p.b]) + w[p.b], p.a, p.idx));
    }

    void ask(int u){
        while (!cur[u].empty()){
            int v = find(cur[u].top().to);
            int lm = cur[u].top().val;
            int idx = cur[u].top().idx;
            cur[u].pop();
            if (u == v) continue;
            if (wei[u] < lm) break;
            if (wei[u] + wei[v] >= kk[idx]){
                qu.push(handle(u, v, idx));
            }
            else {
                cur[u].push(event(half(kk[idx] - wei[u] - wei[v]) + wei[u], v, idx));
                cur[v].push(event(half(kk[idx] - wei[u] - wei[v]) + wei[v], u, idx));
            }
        }
    }

    bool joint(int u, int v){
        u = find(u);
        v = find(v);
        if (u == v) return false;

        if (sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        add(wei[u], wei[v]);
        while (!cur[v].empty()){
            cur[u].push(cur[v].top());
            cur[v].pop();
        }
        ask(u);

        return true;
    }

    vector <int> ord;

    void process(){
        while (!qu.empty()){
            handle k = qu.top();
            qu.pop();
            if (joint(k.a, k.b)) ord.push_back(k.idx);
        }
    }

    void answer(){
        cout << ord.size() << '\n';
        for (int v : ord) cout << v << ' ';
    }
};

void solve(){
    cin >> n >> m;
    for (int i = 1; i <= n; ++ i) cin >> w[i];
    DSU dsu = DSU(n);
    for (int i = 1; i <= m; ++ i){
        int a, b;
        cin >> a >> b >> kk[i];
        dsu.add(edge(a, b, kk[i], i));
    }

    dsu.process();
    dsu.answer();
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3612kb

input:

5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

output:

4
2 3 1 4 

result:

ok 5 number(s): "4 2 3 1 4"

Test #2:

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

input:

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6

output:

2
3 5 

result:

ok 3 number(s): "2 3 5"

Test #3:

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

input:

10 20
4 6 6 1 7 9 1 8 7 9
5 3 2
1 10 10
5 6 7
9 6 9
3 1 1
6 8 1
5 7 0
9 5 4
10 3 4
8 6 8
3 10 6
5 3 8
3 7 9
1 9 10
10 1 0
5 7 6
6 10 1
6 5 9
5 8 2
9 2 4

output:

8
1 2 3 4 5 6 7 20 

result:

ok 9 numbers

Test #4:

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

input:

10 20
0 10 4 6 2 0 2 0 2 8
5 10 8
7 1 6
10 5 0
9 8 0
5 8 4
5 1 8
10 3 6
5 4 3
9 2 6
10 3 6
4 3 1
3 10 3
6 1 3
3 2 5
6 9 2
4 2 5
10 6 5
8 6 3
2 1 0
2 3 6

output:

9
1 4 5 6 2 7 8 9 13 

result:

ok 10 numbers

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3640kb

input:

100 200
49 21 70 93 66 51 36 59 56 62 10 4 46 73 22 48 89 17 72 60 29 64 19 56 32 54 55 0 77 86 34 35 56 17 55 2 98 80 73 71 64 37 61 22 89 96 99 28 0 35 56 45 74 81 30 3 66 96 28 16 29 43 60 61 60 95 83 5 73 1 28 47 73 44 8 4 91 34 100 23 4 93 44 87 72 5 13 88 100 52 56 61 100 80 14 30 59 97 51 43
...

output:

96
1 2 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 9 20 21 22 23 25 26 3 27 28 29 30 31 32 33 35 36 37 38 39 40 42 44 45 46 47 48 49 50 51 52 53 34 54 55 56 58 59 60 61 62 63 64 41 66 67 68 69 70 71 72 74 76 78 80 81 82 87 89 91 94 95 96 100 101 24 84 103 107 109 110 117 119 129 142 144 169 170 195 

result:

wrong answer 40th numbers differ - expected: '41', found: '42'