/* * @Author: Tifa * @Description: From <https://github.com/Tiphereth-A/CP-archives> * !!! ATTENEION: All the context below is licensed under a * GNU Affero General Public License, Version 3. * See <https://www.gnu.org/licenses/agpl-3.0.txt>. */ #include<bits/stdc++.h> usingnamespace std; constint N = 2e5 + 5, M = 4e5 + 5; structEdge { int to, next; } e[M]; int head[N], cnt_e; voidaddEdge(int u, int v){ e[++cnt_e] = {v, head[u]}; head[u] = cnt_e; } #define _for_graph(i, now) \ for (int i = head[now], v = e[i].to; i; v = e[i = e[i].next].to) int fa[N]; int64_t sz[N]; intfind(int x){ if (x == fa[x]) return x; int rt = find(fa[x]); return fa[x] = rt; } voidmerge(int u, int v){ int fu = find(u), fv = find(v); if (fu == fv) return; fa[fu] = fv; sz[fv] += sz[fu]; } structNode { int u, v, w; } q[N]; bool vis[N]; int64_t sum[2]; int endp; bool res; voiddfs(int now, int fa, int idx){ if (vis[now]) { if (now != endp) return; res = !idx; return; } vis[now] = 1; sum[idx] += sz[now]; _for_graph(i, now) { if (v == fa) continue; dfs(v, now, idx ^ 1); } } intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; for (int i = 1; i <= m; ++i) cin >> q[i].u >> q[i].v >> q[i].w; int maxw = 0; for (int i = 1; i <= m; ++i) maxw = max(maxw, q[i].w); int64_t ans = 0; for (int dig = 30; dig >= 0; --dig) { int64_t now = 1ll << dig; if (now > maxw) continue; memset(head, 0, sizeof(head[0]) * (n + 1)); memset(vis, 0, sizeof(vis[0]) * (n + 1)); cnt_e = 0; for (int i = 1; i <= n; ++i) sz[fa[i] = i] = 1; for (int i = 1; i <= m; ++i) if (!(q[i].w & now)) merge(q[i].u, q[i].v); for (int i = 1; i <= m; ++i) if (q[i].w & now) { addEdge(find(q[i].u), find(q[i].v)); addEdge(find(q[i].v), find(q[i].u)); } for (int i = 1; i <= n; ++i) if (!vis[i]) { res = 1; dfs(endp = i, sum[0] = sum[1] = 0, 0); if (!res) { cout << "-1"; return0; } ans += min(sum[0], sum[1]) * now; } } cout << ans; }
/* * @Author: Tifa * @Description: From <https://github.com/Tiphereth-A/CP-archives> * !!! ATTENEION: All the context below is licensed under a * GNU Affero General Public License, Version 3. * See <https://www.gnu.org/licenses/agpl-3.0.txt>. */ #include<bits/stdc++.h> usingnamespace std; using i64 = int64_t; constint N = 1e5 + 5, M = 4e5 + 5; structEdge { int64_t w; int to, next; Edge(int64_t _w = 0, int _to = 0, int _next = 0) : w(_w), to(_to), next(_next) {} } e[M]; int head[N], cnt_edge; voidaddEdge(int x, int y, int64_t w = 1){ e[++cnt_edge] = Edge(w, y, head[x]); head[x] = cnt_edge; } #define _for_graph(head, e, i, now) \ for (int i = head[now], to = e[i].to; i; to = e[i = e[i].next].to) #define _for(i, l, r, vals...) \ for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i) i64 w; int low[N], dfn[N], cnt_dfs; int cnt[N]; voidtarjan(int u, int fa = 0){ low[u] = dfn[u] = ++cnt_dfs; cnt[u] = 0; _for_graph(head, e, i, u) { ++cnt[to]; if (to == fa) continue; if (!dfn[to]) { tarjan(to, u); low[u] = min(low[u], low[to]); cnt[u] += cnt[to]; } else low[u] = min(low[u], dfn[to]); if (low[to] > dfn[u]) { if (cnt[to] % 2 == 0) w = min(w, e[i].w); } else w = min(w, e[i].w); } } intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; int u, v; i64 sum = 0; if (m % 2 == 0) { _for(i, 1, m) { cin >> u >> v >> w; sum += w; } cout << sum; return0; } _for(i, 1, m) { cin >> u >> v >> w; addEdge(u, v, w); addEdge(v, u, w); sum += w; } w = INT64_MAX; tarjan(1); cout << sum - w; return0; }