QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#23682#2543. Edges, Colors and MSTSortingWA 1ms28156kbC++203.2kb2022-03-18 18:49:292022-04-30 03:51:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-30 03:51:22]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:28156kb
  • [2022-03-18 18:49:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
template<class T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<class T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
#define all(x) (x).begin(), (x).end()

const int N = 2e5 + 3;
const int LOGN = 20;

int n, m;
array<int, 3> edges[N];
vector<pair<int, int>> adj[N];

int up[LOGN][N], par[N], d[N];
int idx_par_edge[N];

int col[N], counter = 0;

struct DSU{
    int p[N], sz[N], highest[N];

    void init(){
        for(int i = 1; i <= n; ++i)
            p[i] = i, sz[i] = 1, highest[i] = i;
    }

    int getP(int x){
        if(p[x] == x) return x;
        return p[x] = getP(p[x]);
    }

    bool unite(int a, int b){
        a = getP(a), b = getP(b);
        if(a == b) return false;
        if(sz[a] < sz[b]) swap(a, b);

        sz[a] += sz[b];
        p[b] = a;
        if(d[highest[b]] < d[highest[a]])
            highest[a] = highest[b];

        return true;
    }
} dsu;

void getEdges(int u, int lca, vector<int> &edges){
    while(d[u] > d[lca]){
        u = dsu.highest[dsu.getP(u)];
        if(d[u] <= d[lca]) break;

        edges.push_back(idx_par_edge[u]);
        dsu.unite(u, par[u]);
        u = par[u];
    }
}

int lca(int u, int v){
    if(d[u] != d[v]){
        if(d[u] < d[v]) swap(u, v);
        int diff = d[u] - d[v];
        for(int i = LOGN - 1; i >= 0; --i)
            if(diff & (1 << i))
                u = up[i][u];
    }

    if(u == v)
        return u;

    for(int i = LOGN - 1; i >= 0; --i)
        if(up[i][u] != up[i][v]){
            u = up[i][u];
            v = up[i][v];
        }

    return par[u];
}

void dfsPar(int u, int p){
    par[u] = p;
    for(auto [to, idx]: adj[u]){
        if(to == p) continue;
        idx_par_edge[to] = idx;
        d[to] = d[u] + 1;
        dfsPar(to, u);
    }
}

void initLCA(){
    idx_par_edge[1] = -1;
    dfsPar(1, 1);

    for(int i = 1; i <= n; ++i)
        up[0][i] = par[i];

    for(int j = 1; j < LOGN; ++j){
        for(int i = 1; i <= n; ++i){
            up[j][i] = up[j - 1][up[j - 1][i]];
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    for(int i = 0; i < m; ++i){
        for(int j = 0; j < 3; ++j)
            cin >> edges[i][j];

        if(edges[i][2]){
            adj[edges[i][0]].push_back({edges[i][1], i});
            adj[edges[i][1]].push_back({edges[i][0], i});
        }
    }

    initLCA();
    dsu.init();

    for(int i = 0; i < m; ++i){
        if(edges[i][2]){
            if(!col[i]){
                col[i] = ++counter;
                auto [u, v] = pair{edges[i][0], edges[i][1]};
                dsu.unite(u, v);
            }
        }
        else{
            auto [u, v] = pair{edges[i][0], edges[i][1]};

            int lca_u_v = lca(u, v);
            vector<int> edges;

            getEdges(u, lca_u_v, edges);
            getEdges(v, lca_u_v, edges);

            sort(all(edges));

            col[i] = ++counter;
        }
    }

    for(int i = 0; i < m; ++i)
        cout << col[i] << " ";
    cout << "\n";
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 28156kb

input:

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

output:

1 2 3 4 5 

result:

wrong answer 1st numbers differ - expected: '3', found: '1'