QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#398656#2543. Edges, Colors and MSThaze#WA 1ms3568kbC++233.0kb2024-04-25 16:17:362024-04-25 16:17:37

Judging History

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

  • [2024-04-25 16:17:37]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3568kb
  • [2024-04-25 16:17:36]
  • 提交

answer

/*

Author: Haze

2024/4/25

*/

#include <bits/stdc++.h>

#define irep(i, l, r) for(int i = (l); i <= (r); ++ i)
#define drep(i, r, l) for(int i = (r); i >= (l); -- i)
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr);
using namespace std;
typedef long long ll;

inline ll read() {
    ll s = 0;
    bool fl = false;
    char ch = (char) getchar();
    while (!isdigit(ch)) {
        if (ch == '-')fl = true;
        ch = (char) getchar();
    }
    while (isdigit(ch)) {
        s = s * 10 + (ch ^ 48);
        ch = (char) getchar();
    }
    return fl ? -s : s;
}

const int mod = 1000000000 + 7;
const int itinf = 1000000999;
const ll llinf = 2e18;
const int N = 500099;

vector<int>adj[N];

int fa[N][22], d[N], f[N], up[N];

void dfs(int u, int par,int dp){
    d[u] = dp;
    for(auto v : adj[u]){
        if(v == par)continue;
        fa[v][0] = u, up[v] = u;
        dfs(v, u, dp + 1);
    }
}

int lca(int x, int y){
    if(d[x] < d[y])swap(x, y);
    drep(i, 21, 0){
        if(d[fa[x][i]] >= d[y]){
            x = fa[x][i];
        }
    }
    if(x == y)return x;
    drep(i, 21, 0){
        if(fa[x][i] != fa[y][i]){
            x = fa[x][i], y = fa[y][i];
        }
    }
    return fa[x][0];
}

int find(int x){
    if(f[x] == x)return x;
    f[x] = find(f[x]);
    return f[x];
}
void merge(int x, int y){
    x = find(x), y = find(y);
    if(d[x] > d[y])swap(x, y);
//    up[x] = y;
    f[y] = x;
}

void solve() {
    int n = read(), m = read();
    vector<array<int, 3>>e(m);

    irep(i, 0, m - 1){
        int u = read(), v = read(), c = read();
        e[i] = {u - 1, v - 1, c};
        if(c == 1){
            adj[u - 1].push_back(v - 1);
            adj[v - 1].push_back(u - 1);
        }
    }
    dfs(0, -1, 0);
    irep(r, 1, 21){
        irep(i, 0, n - 1){
            fa[i][r] = fa[fa[i][r - 1]][r -1];
        }
    }
    irep(i, 0, n - 1)f[i] = i;
    set<int>avi;
    irep(i, 1, m)avi.insert(i);
    vector<int>ans(m);
    int tim = 1, clock = 0;
    irep(i, 0, m - 1){
        auto [u, v, c] = e[i];
        if(c == 0){
            int z = lca(u, v);
            u = find(u), v = find(v), z = find(z);
//            cerr << u << ' ' << v << ' ' << z << endl;
            while(u != z){
                cerr << u << ' ' << z << endl;
                merge(u, up[u]);
                u = find(u);
                ++ tim;
            }
            while(v != z){

//                cerr << v << ' ' << z << endl;
                merge(v, up[v]);
                v = find(v);
                ++ tim;
            }
            auto it = avi.lower_bound(tim);
            ans[i] = *it;
            avi.erase(it);
        }
        else{
            if(find(u) == find(v)){
                ans[i] = ++ clock;
            }
            else ans[i] = ++ tim;
        }
    }
    irep(i, 0, m - 1){
        cout << ans[i] << ' ';
    }
}

int main() {
    // IOS
    int T = 1;
    while (T--) {
        solve();
    }
    return 0;
}

详细

Test #1:

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

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: 1ms
memory: 3516kb

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:

2 3 5 6 8 9 10 11 11 1 2 3 4 5 12 

result:

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