QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#558595 | #8932. Bingo | Yarema# | RE | 0ms | 0kb | C++20 | 3.0kb | 2024-09-11 16:59:57 | 2024-09-11 16:59:57 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;
const int N = 200'477;
struct DSU
{
int n;
VI p, sz;
DSU(int _n = 0)
{
n = _n;
p.resize(n);
iota(ALL(p), 0);
sz.assign(n, 1);
}
int find(int v)
{
if (v == p[v])
return v;
return p[v] = find(p[v]);
}
bool unite(int u, int v)
{
u = find(u);
v = find(v);
if (u == v)
return false;
if (sz[u] > sz[v])
swap(u, v);
p[u] = v;
sz[v] += sz[u];
return true;
}
};
const LL LINF = 1e18;
const int LOG = 18;
vector<PII> g[N];
int up[N][LOG];
int h[N];
LL cost[2][N][LOG];
int tin[N], tout[N];
int T = 0;
void dfs(int v, int p, LL c, int d = 0)
{
tin[v] = T++;
up[v][0] = p;
cost[c & 1][v][0] = c;
h[v] = d;
FOR (i, 1, LOG)
{
up[v][i] = up[up[v][i - 1]][i - 1];
cost[c & 1][v][i] = max(cost[c & 1][v][i - 1], cost[c & 1][up[v][i - 1]][i - 1]);
}
for (auto [to, w] : g[v])
{
if (to != p)
dfs(to, v, w, d + 1);
}
tout[v] = T;
}
bool isAnc(int p, int v)
{
return tin[p] <= tin[v] && tout[v] <= tout[p];
}
int lca(int u, int v)
{
if (isAnc(u, v))
return u;
RFOR (i, LOG, 0)
{
int nu = up[u][i];
if (!isAnc(nu, v))
u = nu;
}
return up[u][0];
}
void solve()
{
int n, m;
cin >> n >> m;
FOR (i, 0, n)
{
FOR (j, 0, LOG)
cost[0][i][j] = cost[1][i][j] = -LINF;
}
DSU d(n);
vector<pair<int, PII>> e;
FOR (i, 0, m)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
e.PB({c, {a, b}});
}
sort(ALL(e));
vector<pair<int, PII>> ed;
LL ans = 0;
FOR (i, 0, m)
{
auto [w, p] = e[i];
auto [u, v] = p;
if (d.unite(u, v))
{
ans += w;
g[u].PB({v, w});
g[v].PB({u, w});
}
else
ed.PB(e[i]);
}
if (d.sz[d.find(0)] != n)
{
FOR (i, 0, n)
g[i].clear();
cout << -1 << ' ' << -1 << '\n';
return;
}
LL add = LINF;
dfs(0, 0, 0, 0);
for (auto [w, p] : ed)
{
VI v = {p.F, p.S};
int lc = lca(p.F, p.S);
cerr << "EDGE\n";
cerr << v[0] << ' ' << v[1] << ' ' << w << '\n';
FOR (t, 0, 2)
{
int u = v[t];
if (u != lc)
{
int dh = h[u] - h[lc];
cerr << u << ' ' << lc << ' ' << dh << '\n';
FOR (i, 0, LOG)
{
if (dh & (1 << i))
add = min(add, w - cost[(w & 1) ^ 1][u][i]);
}
}
}
}
VL res(2, -1);
res[ans & 1] = ans;
res[(ans & 1) ^ 1] = (ans + add >= LINF || ans + LINF < 0 ? -1 : ans + add);
cout << res[0] << ' ' << res[1] << '\n';
FOR (i, 0, n)
{
g[i].clear();
}
T = 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1