题意
给定n个点和m条边,有三种情况
- 这条边是最小生成树所必须的边
- 这条边存在于不只一个最小生成树中
- 这条边不包含在任何一个最小生成树中
询问所有边的情况。
分析
如果一条边可以替换别的边成为最小生成树的边,那么它们的权值肯定是相同的,因此我们可以考虑所有权值相等的边。我们将这些边所在的连通块相连,如果存在一条边是桥,那么它一定是不可或缺的,所以答案记为any,否则为at least one。然后就是正常的最小生成树做法,如果这当前边的两个点都已经在连通块中了,那么这条边一定是none。
AC Code
#include <bits/stdc++.h>
#define ACM_LOCAL
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10, M = 5e5 + 10, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, m, k, idx, fa[N], vis[N], dfn[N], low[N], bridge[N];
int cnt, h[N], ans[N];
int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
struct Edge {
int u, v, w, id;
bool operator < (const Edge &rhs) const {
return w < rhs.w;
}
}e[N];
struct edge {
int to, next, id;
}ed[N];
void add(int u, int v, int id) {
ed[cnt].to = v;
ed[cnt].next = h[u];
ed[cnt].id = id;
h[u] = cnt++;
}
void tarjan(int x, int edge) {
low[x] = dfn[x] = ++idx;
for (int i = h[x]; ~i; i = ed[i].next) {
int v = ed[i].to;
if (!dfn[v]) {
tarjan(v, i);
low[x] = min(low[x], low[v]);
if (low[v] > dfn[x]) ans[ed[i].id] = 1;
} else if (i != (edge^1))
low[x] = min(low[x], dfn[v]);
}
}
void solve() {
cin >> n >> m; for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) cin >> e[i].u >> e[i].v >> e[i].w, e[i].id = i;
sort(e+1, e+m+1);
for (int i = 1; i <= m; i++) {
int l = i, r = i;
while (e[l].w == e[r].w) r++;
for (int k = l; k < r; k++) {
int u = find(e[k].u), v = find(e[k].v);
if (u != v) {
dfn[u] = dfn[v] = low[u] = low[v] = idx = cnt = 0;
h[u] = h[v] = -1;
}
}
for (int k = l; k < r; k++) {
int u = find(e[k].u), v = find(e[k].v);
if (u != v) {
add(u, v, e[k].id), add(v, u, e[k].id);
ans[e[k].id] = 2;
}
}
for (int k = l; k < r; k++) {
if (!dfn[find(e[k].u)])
tarjan(find(e[k].u), 0);
}
for (int k = l; k < r; k++)
if (find(e[k].u) != find(e[k].v))
fa[find(e[k].u)] = find(e[k].v);
i = r - 1;
}
for (int i = 1; i <= m; i++) {
if (ans[i] == 1) printf("any\n");
else if (ans[i] == 2) printf("at least one\n");
else printf("none\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
return 0;
}