QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398696 | #2543. Edges, Colors and MST | haze# | WA | 2ms | 7944kb | C++23 | 3.5kb | 2024-04-25 16:35:50 | 2024-04-25 16:35:50 |
Judging History
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, eg;
irep(i, 1, m)avi.insert(i);
vector<int>ans(m);
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);
while(u != z){
merge(u, up[u]);
u = find(u);
eg.insert(*avi.begin());
avi.erase(avi.begin());
}
while(v != z){
merge(v, up[v]);
v = find(v);
eg.insert(*avi.begin());
avi.erase(avi.begin());
}
ans[i] = *avi.begin();
avi.erase(avi.begin());
}
else{
if(find(u) == find(v)){
if(eg.empty()){
eg.insert(*avi.begin());
avi.erase(avi.begin());
}
ans[i] = *eg.begin();
eg.erase(eg.begin());
}
else{
merge(u, v);
ans[i] = *avi.begin();
avi.erase(avi.begin());
}
}
// cout << i << ':' << endl;
// for(auto i1 : avi)cout << i1 << ' ';
// cout << endl;
// for(auto i1 : eg)cout << i1 << ' ' ;
// cout << endl << endl;
// cerr << ans[i] << endl;
}
irep(i, 0, m - 1){
cout << ans[i] << ' ';
}
}
/*
4 5
1 2 0
2 3 1
3 4 1
2 4 0
1 3 1
3 2
1 2 1
2 3 1
*/
int main() {
// IOS
int T = 1;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7700kb
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: 2ms
memory: 7944kb
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 3 4 7 9 11 15
result:
wrong answer 10th numbers differ - expected: '9', found: '3'