QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626266#2543. Edges, Colors and MSTXiao_NanniangWA 0ms3856kbC++232.9kb2024-10-10 01:44:052024-10-10 01:44:06

Judging History

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

  • [2024-10-10 01:44:06]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3856kb
  • [2024-10-10 01:44:05]
  • 提交

answer

#include <iostream>
#include <vector>
#include <deque>
#include <numeric>

#define pii pair<int, int>

using namespace std;

int n;
int m;
vector<int> a;
vector<int> b;
deque<bool> c;
vector<int> p;
// vector<int> sz;
vector<int> res;

vector<vector<pii>> graph;
vector<pii> fa;
vector<int> h;

int P(int a) {
    if (p[a] == a) {
        return a;
    }
    return p[a] = P(p[a]);
}

void MergeNoCheckSimple(int a, int b) {
    a = P(a);
    b = P(b);
    p[b] = a;
}

// void Merge(int a, int b) {
//     a = P(a);
//     b = P(b);
//     if (a != b) {
//         if (sz[a] < sz[b]) {
//             swap(a, b);
//         }
//         sz[a] += sz[b];
//         p[b] = a;
//     }
// }

void Dfs(int x) {
    // cout << x << endl;
    for (const auto& [y, v] : graph[x]) {
        if (y != fa[x].first) {
            fa[y] = { x, v };
            h[y] = h[x] + 1;
            Dfs(y);
        }
    }
}

void Solve() {
    cin >> n >> m;
    a.resize(m);
    b.resize(m);
    c.resize(m);
    p.resize(n + 1);
    // sz.assign(n + 1, 1);
    res.resize(m);
    graph.resize(n + 1);
    fa.resize(n + 1);
    h.resize(n + 1);
    iota(p.begin() + 1, p.end(), 1);
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        if (c[i]) {
            graph[a[i]].push_back({ b[i], i });
            graph[b[i]].push_back({ a[i], i });
        }
    }
    // cout << "DFS begin" << endl;
    Dfs(1);
    // cout << "DFS end" << endl;
    int cc = 1;
    for (int i = 0; i < m; i++) {
        // cout << i << endl;
        if (!res[i]) {
            if (c[i]) {
                if (fa[a[i]].first == b[i]) {
                    MergeNoCheckSimple(b[i], a[i]);
                }
                else {
                    MergeNoCheckSimple(a[i], b[i]);
                }
                res[i] = cc;
                cc++;
            }
            else {
                int x = P(a[i]);
                int y = P(b[i]);
                vector<int> cur;
                while (x != y) {
                    // cout << i << ',' << x << ',' << y << endl;
                    // cout << "Add " << x << endl;
                    if (h[x] < h[y]) {
                        swap(x, y);
                    }
                    // cout << x << ',' << fa[x].second << endl;
                    cur.push_back(fa[x].second);
                    // res[fa[x].second] = cc;
                    // cc++;
                    MergeNoCheckSimple(fa[x].first, x);
                    x = P(x);
                }
                for (const auto& j : cur) {
                    res[j] = cc;
                    cc++;
                }
                res[i] = cc;
                cc++;
            }
        }
        cout << res[i] << ' ';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3856kb

input:

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

output:

3 1 4 5 2 

result:

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

Test #2:

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

input:

9 15
1 4 1
3 5 1
3 9 0
1 3 0
2 5 0
5 8 0
6 9 0
8 9 0
1 7 1
1 8 1
6 8 1
4 9 1
2 4 1
3 4 1
4 6 0

output:

1 2 5 6 8 10 12 13 14 9 11 4 7 3 15 

result:

wrong answer 12th numbers differ - expected: '3', found: '4'