1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| #include<cstdio> #include<algorithm> using namespace std; #define re register #define int long long const int MAXn = 1e5; const int MAXm = 3e5; const int INF = 0x3f3f3f3f3f3f3f3f;
template <class T> inline void read(T &a) { register char c;while (c = getchar(), (c < '0' || c > '9') && c != '-');register bool f = c == '-';register T x = f ? 0 : c - '0';while (c = getchar(), c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48);}a = f ? -x : x; }
int n, m, root = 1; int head[MAXn + 10], cntnex, nex[MAXm * 2 + 10], to[MAXm * 2 + 10], wei[MAXm * 2 + 10]; void Insert(int u, int v, int w) { nex[++cntnex] = head[u]; head[u] = cntnex; to[cntnex] = v; wei[cntnex] = w; }
int fat[MAXn + 10]; int anc(int x) { return fat[x] = fat[x] == x ? x : anc(fat[x]); } void Merge(int x, int y) { if (anc(x) != anc(y)) { fat[anc(x)] = y; } } bool SameAnc(int x, int y) { return anc(x) == anc(y); } void Init(int top) { for (re int i = 1; i <= top; ++i) { fat[i] = i; } }
struct Edge { int u, v, w; Edge():u(0), v(0), w(0){} Edge(int u_, int v_, int w_):u(u_), v(v_), w(w_){} inline bool operator<(Edge x) { return this->w < x.w; } }edge[MAXm + 10]; bool intree[MAXm + 10]; int Kruskal() { int ans = 0; sort(edge + 1, edge + 1 + m); Init(n); for (re int i = 1; i <= m; ++i) { if (!SameAnc(edge[i].u, edge[i].v)) { ans += edge[i].w; Merge(edge[i].u, edge[i].v); intree[i] = 1; } } return ans; }
void mergemax(int &ansmax, int &anscmx, int max1, int cmx1, int max2, int cmx2) { if (max1 > max2) { ansmax = max1; anscmx = max(max2, cmx1); } else if (max1 < max2) { ansmax = max2; anscmx = max(max1, cmx2); } else { ansmax = max1; anscmx = max(cmx1, cmx2); } } int le[MAXn * 4 + 10], ri[MAXn * 4 + 10], maxx[MAXn * 4 + 10], cmax[MAXn * 4 + 10]; void pushup(int id) { mergemax(maxx[id], cmax[id], maxx[id << 1], cmax[id << 1], maxx[(id << 1) + 1], cmax[(id << 1) + 1]); } void BuildUseArr(int id, int l, int r, int *a) { le[id] = l; ri[id] = r; if (l == r) { maxx[id] = a[l]; cmax[id] = -INF; } else { int mid = (l + r) >> 1; BuildUseArr(id << 1, l, mid, a); BuildUseArr((id << 1) + 1, mid + 1, r, a); pushup(id); } } pair<int, int> Eva(int id, int l, int r) { if (le[id] >= l && ri[id] <= r) { return make_pair(maxx[id], cmax[id]); } else { int mid = (le[id] + ri[id]) >> 1; if (l <= mid && r > mid) { int ansmax, anscmx; pair<int, int> left, right; left = Eva(id << 1, l, r); right = Eva((id << 1) + 1, l, r); mergemax(ansmax, anscmx, left.first, left.second, right.first, right.second); return make_pair(ansmax, anscmx); } else if (l <= mid) { return Eva(id << 1, l, r); } else { return Eva((id << 1) + 1, l, r); } } }
int ndwei[MAXn + 10], idxwei[MAXn + 10]; int fa[MAXn + 10], dep[MAXn + 10], siz[MAXn + 10], hson[MAXn + 10]; void Dfs1(int cur) { dep[cur] = dep[fa[cur]] + 1; siz[cur] = 1; int mx = -INF; for (re int i = head[cur]; i; i = nex[i]) { if (!intree[i >> 1]) continue; if (to[i] == fa[cur]) continue; fa[to[i]] = cur; ndwei[to[i]] = wei[i]; Dfs1(to[i]); siz[cur] += siz[to[i]]; if (siz[to[i]] > mx) { mx = siz[to[i]]; hson[cur] = to[i]; } } } int cntdfs, nddfs[MAXn + 10], idxdfs[MAXn + 10], top[MAXn + 10]; void Dfs2(int cur) { nddfs[cur] = ++cntdfs; idxdfs[cntdfs] = cur; if (hson[cur]) { top[hson[cur]] = top[cur]; Dfs2(hson[cur]); } for (re int i = head[cur]; i; i = nex[i]) { if (!intree[i >> 1]) continue; if (to[i] == hson[cur] || to[i] == fa[cur]) continue; top[to[i]] = to[i]; Dfs2(to[i]); } } pair<int, int> TreePathEva(int x, int y) { int ansmax = -INF, anscmx = -INF; int tmpansmax, tmpanscmx; pair<int, int> tmp; while (top[x] != top[y]) { if (dep[top[x]] > dep[top[y]]) { swap(x, y); } tmp = Eva(1, nddfs[top[y]], nddfs[y]); mergemax(tmpansmax, tmpanscmx, ansmax, anscmx, tmp.first, tmp.second); ansmax = tmpansmax; anscmx = tmpanscmx; y = fa[top[y]]; } if (dep[x] > dep[y]) { swap(x, y); } if (x == y) { return make_pair(ansmax, anscmx); } else { tmp = Eva(1, nddfs[x] + 1, nddfs[y]); mergemax(tmpansmax, tmpanscmx, ansmax, anscmx, tmp.first, tmp.second); ansmax = tmpansmax; anscmx = tmpanscmx; return make_pair(ansmax, anscmx); } }
int ans, diff = INF; signed main() { cntnex = 1; read(n), read(m); int tmp = 0; for (re int i = 1, u, v, w; i <= m; ++i) { read(u), read(v), read(w); if (u == v) { ++tmp; continue; } edge[i - tmp] = Edge(u, v, w); } m -= tmp; ans = Kruskal(); for (re int i = 1; i <= m; ++i) { Insert(edge[i].u, edge[i].v, edge[i].w); Insert(edge[i].v, edge[i].u, edge[i].w); } Dfs1(root); Dfs2(root); for (re int i = 1; i <= n; ++i) { idxwei[i] = ndwei[idxdfs[i]]; } BuildUseArr(1, 1, n, idxwei); for (re int i = 1; i <= m; ++i) { if (!intree[i]) { pair<int, int> tmp = TreePathEva(edge[i].u, edge[i].v); if (tmp.first == edge[i].w) { diff = min(diff, edge[i].w - tmp.second); } else { diff = min(diff, edge[i].w - tmp.first); } } } printf("%lld\n", ans + diff); }
|