QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#398667 | #2543. Edges, Colors and MST | haze# | WA | 2ms | 7644kb | C++23 | 3.2kb | 2024-04-25 16:22:43 | 2024-04-25 16:22:44 |
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;
irep(i, 1, m)avi.insert(i);
vector<int>ans(m);
int tim = 0, 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);
while(u != z){
// cerr << u << ' ' << z << endl;
merge(u, up[u]);
u = find(u);
++ tim;
}
while(v != z){
merge(v, up[v]);
v = find(v);
++ tim;
}
// auto it = avi.lower_bound(tim);
// ans[i] = *it;
// avi.erase(it);
ans[i] = tim;
}
else{
if(find(u) == find(v)){
++ clock;
// auto it = avi.lower_bound(clock);
// ans[i] = *it;
// clock = *it;
ans[i] = clock;
}
else ans[i] = ++ tim;
}
}
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;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 7644kb
input:
4 5 1 2 0 2 3 1 3 4 1 2 4 0 1 3 1
output:
2 1 3 4 2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'