Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
37710 Sakuzyh 【BJ】T2 C++ 通过 100 239 MS 36420 KB 2770 2025-05-02 14:31:25

Tests(31/31):


// Author: Aquizahv #include <bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f #define lnf 0x3f3f3f3f using namespace std; const int N = 2e5 + 5, M = 4e5 + 5; int n, m, tmp[M], tmppos, ans; int head[N], pos = 1; struct Edge { int u, v, w, nxt; } e[M << 1]; void addEdge(int u, int v, int w) { // cout << u << ' ' << v << endl; e[++pos] = {u, v, w, head[u]}; head[u] = pos; } bool spare[M]; // a color that is free to delete int dfncnt, dfn[N], bcccnt; bool mark[M << 1]; vector<int> g[M + N]; void AddEdge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } pair<int, int> dfs(int u, int fa) { // cout << u << endl; dfn[u] = ++dfncnt; pair<int, int> res = {0, 0}; for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v) { if (v == fa || dfn[v] > dfn[u]) continue; if (dfn[v]) { res = {i, ++bcccnt}; mark[i] = mark[i ^ 1] = true; AddEdge(tmppos + bcccnt, e[i].w); // printf("{%d %d} %d\n", e[i].u, e[i].v, res.second); continue; } // cout << u << "->" << v << endl; auto ret = dfs(v, u); if (ret.first) { mark[i] = mark[i ^ 1] = true; AddEdge(tmppos + ret.second, e[i].w); // printf("{%d %d} %d\n", e[i].u, e[i].v, res.second); if (e[ret.first].v != u) res = ret; } } return res.first && u == e[res.first].v ? make_pair(0, 0) : res; } bool Vis[M + N], flag; void Dfs(int u, int fa) { if (u <= tmppos && spare[u]) flag = true; Vis[u] = true; bool visfa = false; for (auto v : g[u]) { if (v == fa && !visfa) { visfa = false; continue; } if (Vis[v]) flag = true; else Dfs(v, u); } } int main() { cin >> n >> m; int u, v, w; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w), tmp[i] = w; addEdge(u, v, w), addEdge(v, u, w); } sort(tmp + 1, tmp + m + 1); tmppos = unique(tmp + 1, tmp + m + 1) - tmp - 1; for (int i = 2; i <= pos; i++) e[i].w = lower_bound(tmp + 1, tmp + tmppos + 1, e[i].w) - tmp; for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i, 0); for (int i = 2; i <= pos; i += 2) if (!mark[i]) spare[e[i].w] = true; for (int i = 1; i <= bcccnt; i++) if (!Vis[tmppos + i]) { flag = false; Dfs(tmppos + i, 0); ans += !flag; } cout << tmppos - ans << endl; return 0; }


测评信息: