QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#539420 | #8941. Even or Odd Spanning Tree | ucup-team4702# | WA | 75ms | 24340kb | C++17 | 5.8kb | 2024-08-31 14:45:59 | 2024-08-31 14:45:59 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <tuple>
#include <vector>
#include <bitset>
#define tupl tuple <int, int, int>
#define ll long long
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(ll x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
bool _stmer;
const int N = 2e5 + 5, M = 1e6 + 5, inf = 1e9;
namespace Uni {
array <int, N> fa, siz;
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
bool merge(int x, int y) {
int fx = find(x),
fy = find(y);
if (fx == fy) return 0;
siz[fy] += siz[fx], fa[fx] = fy;
return 1;
}
} //namespace Uni
namespace G {
array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;
void add(int x, int y) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
fir[x] = cnt;
}
} //namespace G
namespace Hpt {
array <int, N> siz, dep, son, fa;
void dfs1(int x) {
siz[x] = 1;
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (G::to[i] == fa[x]) continue;
fa[G::to[i]] = x;
dep[G::to[i]] = dep[x] + 1;
dfs1(G::to[i]);
siz[x] += siz[G::to[i]];
if (siz[G::to[i]] > siz[son[x]]) son[x] = G::to[i];
}
}
array <int, N> top, dfn, idx;
int cnt;
void dfs2(int x, int Mgn) {
cnt++;
dfn[x] = cnt;
idx[cnt] = x;
top[x] = Mgn;
if (son[x]) dfs2(son[x], Mgn);
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (G::to[i] == fa[x] || G::to[i] == son[x]) continue;
dfs2(G::to[i], G::to[i]);
}
}
} //namespace Hpt
namespace Sgt {
array <int, N * 4> edge, tag;
void pushup(int x) { edge[x] = min(edge[x * 2], edge[x * 2 + 1]); }
void pushdown(int x) {
if (tag[x] == inf) return;
edge[x * 2] = min(edge[x * 2], tag[x]);
edge[x * 2 + 1] = min(edge[x * 2 + 1], tag[x]);
tag[x * 2] = min(tag[x * 2], tag[x]);
tag[x * 2 + 1] = min(tag[x * 2 + 1], tag[x]);
tag[x] = inf;
}
void build(int x, int l, int r) {
edge[x] = tag[x] = inf;
if (l == r) return;
int mid = (l + r) >> 1;
build(x * 2, l, mid);
build(x * 2 + 1, mid + 1, r);
}
void modify(int x, int l, int r, int L, int R, int y) {
if (L > r || R < l) return;
if (L <= l && R >= r) {
edge[x] = min(edge[x], y);
tag[x] = min(tag[x], y);
return;
}
pushdown(x);
int mid = (l + r) >> 1;
if (L <= mid) modify(x * 2, l, mid, L, R, y);
if (R > mid) modify(x * 2 + 1, mid + 1, r, L, R, y);
pushup(x);
}
int query(int x, int l, int r, int y) {
if (l == r) return edge[x];
pushdown(x);
int mid = (l + r) >> 1;
if (y <= mid) return query(x * 2, l, mid, y);
else return query(x * 2 + 1, mid + 1, r, y);
}
} //namespace Sgt
namespace Hpt {
void modify(int x, int y, int k, int n) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
Sgt::modify(1, 1, n, dfn[top[x]], dfn[x], k);
x = fa[top[x]];
}
if (dfn[x] > dfn[y]) swap(x, y);
Sgt::modify(1, 1, n, dfn[x], dfn[y], k);
}
} //namespamce Hpt
bitset <M> vis;
void init(int n, int m) {
G::cnt = 1, Hpt::cnt = 0;
for (int i = 1; i <= n; i++)
G::fir[i] = Hpt::son[i] = 0, Uni::siz[Uni::fa[i] = i] = 1;
for (int i = 1; i <= m; i++) vis[i] = 0;
}
array <tupl, M> qrl;
void solve() {
int n = read(), m = read();
ll res = 0;
init(n, m);
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), z = read();
qrl[i] = make_tuple(x, y, z);
}
sort(qrl.begin() + 1, qrl.begin() + 1 + m, [](tupl x, tupl y) {
return get <2>(x) < get <2>(y);
});
for (int i = 1; i <= m; i++) {
int x = 0, y = 0, z = 0; tie(x, y, z) = qrl[i];
if (Uni::merge(x, y))
res += z, G::add(x, y), G::add(y, x), vis[i] = 1;
}
if (Uni::siz[Uni::find(1)] != n) res = inf;
Hpt::dfs1(1), Hpt::dfs2(1, 0);
Sgt::build(1, 1, n);
ll ans = inf;
for (int i = 1; i <= m; i++) {
int x = 0, y = 0, z = 0; tie(x, y, z) = qrl[i];
if (!((z & 1) ^ (res & 1)) && !vis[i])
Hpt::modify(x, y, z, n);
}
for (int i = 1; i <= m; i++) {
int x = 0, y = 0, z = 0; tie(x, y, z) = qrl[i];
int pos = Hpt::dfn[x] < Hpt::dfn[y] ? y : x;
if (((z & 1) ^ (res & 1)) && vis[i])
ans = min(ans, res - z + Sgt::query(1, 1, n, Hpt::dfn[pos]));
}
Sgt::build(1, 1, n);
for (int i = 1; i <= m; i++) {
int x = 0, y = 0, z = 0; tie(x, y, z) = qrl[i];
if (((z & 1) ^ (res & 1)) && !vis[i])
Hpt::modify(x, y, z, n);
}
for (int i = 1; i <= m; i++) {
int x = 0, y = 0, z = 0; tie(x, y, z) = qrl[i];
int pos = Hpt::dfn[x] < Hpt::dfn[y] ? y : x;
if (!((z & 1) ^ (res & 1)) && vis[i])
ans = min(ans, res - z + Sgt::query(1, 1, n, Hpt::dfn[pos]));
}
if (res & 1) swap(ans, res);
if (ans == inf) ans = -1;
if (res == inf) res = -1;
write(res), putchar(32);
write(ans), puts("");
}
bool _edmer;
int main() {
cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
int T = read();
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 22244kb
input:
3 2 1 1 2 5 3 1 1 3 1 4 4 1 2 1 1 3 1 1 4 1 2 4 2
output:
-1 5 -1 -1 4 3
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 75ms
memory: 24340kb
input:
10000 16 50 1 2 649251632 2 3 605963017 3 4 897721528 4 5 82446151 5 6 917109168 6 7 79647183 7 8 278566178 7 9 573504723 3 10 679859251 8 11 563113083 1 12 843328546 10 13 594049160 11 14 997882839 7 15 569431750 2 16 244494799 16 7 960172373 13 4 317888322 13 3 446883598 9 3 678142319 5 8 43208692...
output:
3140067932 -1 1262790434 -1 1263932600 -1 -1 1180186165 2248358640 -1 -1 3738936375 -1 1058677117 -1 3402127725 -1 1187873655 1395482806 -1 3456885934 -1 3943951826 -1 2479987500 -1 2909126794 -1 2483103706 -1 -1 1824129583 3769162572 -1 562142376 537074351 -1 2475481185 1133556832 -1 -1 3120149981 ...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '-1'