QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#550346 | #8941. Even or Odd Spanning Tree | zhazha21# | WA | 454ms | 7684kb | C++14 | 2.7kb | 2024-09-07 11:49:04 | 2024-09-07 11:49:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 500005;
struct Ordered
{
tuple<int, int, int> black[MAXN], white[MAXN];
int i = 0, j = 0, n = 0, m = 0, d = 0;
void init(int _n, int _m)
{
n = _n, m = _m;
sort(black, black + n);
sort(white, white + m);
}
bool empty()
{
return i == n && j == m;
}
tuple<int, int, int> getNext()
{
if (j >= m || i < n && get<0>(black[i]) + d < get<0>(white[j]))
return black[i++];
else
return white[j++];
}
} o;
int fa[MAXN];
int root(int x)
{
return fa[x] == x ? x : (fa[x] = root(fa[x]));
}
bool merge(int x, int y)
{
x = root(x), y = root(y);
return x != y && (fa[x] = y, 1);
}
int N;
pair<int, int> work(int delta)
{
o.i = o.j = 0;
o.d = delta;
int x = 0, y = 0, xn = 0;
for (int i = 1; i <= N; i++)
fa[i] = i;
while (!o.empty())
{
int w, u, v;
tie(w, u, v) = o.getNext();
if (merge(u, v))
{
y += w;
xn++;
if (w & 1)
y += delta, x++;
}
}
if (xn < N - 1)
return {x, 1e18};
return {x, y};
}
int getAns(int k)
{
int l = -1e10, r = 1e10, ans = 1e18;
while (r > l)
{
int m = (l + r) >> 1;
int x, y;
tie(x, y) = work(m);
// cout << l << " " << m << " " << r << " " << x << " " << y << endl;
if (x > k)
l = m + 1;
else if (x <= k)
r = m, ans = y - m * x;
}
return r == -1e10 ? 1e18 : ans;
}
signed main()
{
int T;
cin >> T;
while (T--)
{
int n, m;
cin >> n >> m;
N = n;
int b = 0, w = 0;
for (int i = 0; i < m; i++)
{
int u, v, W;
cin >> u >> v >> W;
if (W & 1)
o.black[b++] = {W, u, v};
else
o.white[w++] = {W, u, v};
}
o.init(b, w);
int x0, y0;
tie(x0, y0) = work(0);
int y1 = min(getAns(x0 - 1), getAns(x0 + 1));
if (y0 >= 1e18)
y0 = -1;
if (y1 >= 1e18)
y1 = -1;
if (x0 & 1)
cout << y1 << " " << y0 << endl;
else
cout << y0 << " " << y1 << endl;
// cout << getAns(x0 + 1) << endl;
// cout << x0 << " " << y0 << " " << getAns(x0 - 1) << " " << getAns(x0 + 1) << endl;
// int y1 = max(x0 && x0 - 1 + w >= n - 1 ? getAns(x0 - 1) : -1e9, x0 + 1 <= b && x0 + 1 <= n - 1 ? getAns(x0 + 1) : -1e9);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7684kb
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: 454ms
memory: 7676kb
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 3159441841 1262790434 1309454727 1263932600 1307926149 1189242112 1180186165 2248358640 2261370885 3776889482 3738936375 1093500444 1058677117 3433711014 3402127725 1201099898 1187873655 1395482806 1410162043 3456885934 3486092007 3943951826 3988876469 2479987500 2535532661 2909126794 293...
result:
wrong answer 1086th numbers differ - expected: '1004056589', found: '1048368791'