QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#550339#8941. Even or Odd Spanning Treezhazha21#TL 1ms7712kbC++142.8kb2024-09-07 11:41:482024-09-07 11:41:48

Judging History

你现在查看的是最新测评结果

  • [2024-09-07 11:41:48]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7712kb
  • [2024-09-07 11:41:48]
  • 提交

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(N == 16) cout << w << " " << u << " " << v << " " << root(u) << " " << root(v) << endl;
        if (merge(u, v))
        {
            y += w;
            xn++;
            if (w & 1)
                y += delta, x++;
        }
    }
    if (xn < N - 1)
        return {x, 1e9};
    return {x, y};
}
int getAns(int k)
{
    int l = -1e9, r = 1e9, ans = 1e9;
    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 == -1e9 ? 1e9 : 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 >= 1e9)
            y0 = -1;
        if (y1 >= 1e9)
            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: 7712kb

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
Time Limit Exceeded

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:

63072242 12 5 12 5
73380277 12 4 5 4
79647183 6 7 6 7
82446151 4 5 4 4
99371435 10 14 10 14
115622469 12 2 4 2
125311298 8 5 8 2
161438316 11 2 11 2
166324652 11 7 2 7
195885746 3 1 3 1
195969247 1 2 1 7
244494799 2 16 7 16
266837753 13 7 13 16
278566178 7 8 16 16
317888322 13 4 16 16
379828633 4 9 ...

result: