QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#483463 | #2543. Edges, Colors and MST | GenshinImpactsFault | WA | 3ms | 15952kb | C++14 | 2.9kb | 2024-07-18 17:38:37 | 2024-07-18 17:38:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
int n, m;
int ex[N], ey[N], c[N];
vector<pair<int, int> > e[N];
bool used[N];
int cur, p[N];
int idx, dfn[N], sz[N], son[N], fa[N], top[N], rev[N], dep[N];
int id[N];
int cnt[N * 4];
int ans[N];
void dfs1(int u, int ff) {
fa[u] = ff, dep[u] = dep[ff] + 1, sz[u] = 1;
for(auto v : e[u]) if(v.first != ff) {
id[v.first] = v.second;
dfs1(v.first, u), sz[u] += sz[v.first];
if(!son[u] || sz[son[u]] < sz[v.first]) son[u] = v.first;
}
}
void dfs2(int u, int tp) {
top[u] = u, dfn[u] = ++idx, rev[idx] = u;
if(son[u]) dfs2(son[u], tp);
for(auto v : e[u]) if(!top[v.first]) dfs2(v.first, v.first);
}
void build(int u, int l, int r) {
if(l >= r) {
if(rev[l] != 1) cnt[u] = 1;
return ;
}
int mid = (l + r) >> 1;
build(u * 2, l, mid), build(u * 2 + 1, mid + 1, r);
cnt[u] = cnt[u * 2] + cnt[u * 2 + 1];
}
int qry(int u, int l, int r, int L, int R) {
if(L <= l && r <= R) return cnt[u];
int mid = (l + r) >> 1, res = 0;
if(L <= mid) res += qry(u * 2, l, mid, L, R);
if(R > mid) res += qry(u * 2 + 1, mid + 1, r, L, R);
return res;
}
int LCA(int x, int y) {
for(; top[x] != top[y];) {
if(dep[x] < dep[y]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
int qry(int x, int y) {
int lca = LCA(x, y), res = 0;
for(; top[x] != top[lca];) {
res += qry(1, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if(lca != x) res += qry(1, 1, n, dfn[lca], dfn[x]);
for(; top[y] != top[lca];) {
res += qry(1, 1, n, dfn[top[y]], dfn[y]);
y = fa[top[y]];
}
if(lca != y) res += qry(1, 1, n, dfn[lca], dfn[y]);
return res;
}
void mdy(int u, int l, int r, int L, int R) {
if(!cnt[u]) return ;
if(l >= r) {
p[id[rev[l]]] = cur + 1;
cnt[u] = 0; return ;
}
int mid = (l + r) >> 1;
if(L <= mid) mdy(u * 2, l, mid, L, R);
if(R > mid) mdy(u * 2 + 1, mid + 1, r, L, R);
cnt[u] = cnt[u * 2] + cnt[u * 2 + 1];
}
void mdy(int x, int y) {
int lca = LCA(x, y);
for(; top[x] != top[lca];) {
mdy(1, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if(lca != x) mdy(1, 1, n, dfn[lca], dfn[x]);
for(; top[y] != top[lca];) {
mdy(1, 1, n, dfn[top[y]], dfn[y]);
y = fa[top[y]];
}
if(lca != y) mdy(1, 1, n, dfn[lca], dfn[y]);
}
int main() {
ios::sync_with_stdio(0); cin.tie(nullptr);
cin >> n >> m;
for(int i = 1; i <= m; i++) {
cin >> ex[i] >> ey[i] >> c[i];
if(c[i] == 1) {
e[ex[i]].push_back({ey[i], i});
e[ey[i]].push_back({ex[i], i});
}
}
dfs1(1, 0), dfs2(1, 1);
build(1, 1, n);
for(int i = 1; i <= m; i++) {
if(c[i] == 0) {
int num = qry(ex[i], ey[i]);
mdy(ex[i], ey[i]);
cur += num + 1;
used[cur] = 1, ans[i] = cur;
}
}
for(int i = 1; i <= m; i++) {
if(c[i] == 1) {
for(; used[p[i]]; ++p[i]);
used[p[i]] = 1, ans[i] = p[i];
}
}
for(int i = 1; i <= m; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15944kb
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: 3ms
memory: 15952kb
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:
4 6 3 5 8 10 12 13 0 9 11 1 7 2 14
result:
wrong answer 1st numbers differ - expected: '1', found: '4'