QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#503392#7069. Farmlllllu5454WA 1ms5804kbC++203.8kb2024-08-03 18:19:012024-08-03 18:19:07

Judging History

This is the latest submission verdict.

  • [2024-08-03 18:19:07]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 5804kb
  • [2024-08-03 18:19:01]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 100, mod = 998244353;
class edge
{
public:
    int x, y, w;
    bool operator<(const edge x) const
    {
        return w < x.w;
    }
} e[N];
vector<edge> v1, v2;
int f[N];
struct DSU
{
    std::vector<int> f, siz;
    DSU() {}
    DSU(int n)
    {
        init(n);
    }
    void init(int n)
    {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }
    int find(int x)
    {
        while (x != f[x])
        {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    bool same(int x, int y)
    {
        return find(x) == find(y);
    }
    bool merge(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y)
        {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    int size(int x)
    {
        return siz[find(x)];
    }
} a, c;
pair<int, int> pa[1000];
void solve()
{
    int n, m;
    cin >> n >> m;
    a.init(n + 1);
    c.init(n + 1);
    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].x >> e[i].y >> e[i].w;
    }
    int q;
    cin >> q;
    int u, v;
    for (int i = 0; i < q; i++)
    {
        cin >> u >> v;
        pa[i] = {u, v};
        auto [x2, y2, z2] = e[u];
        auto [x1, y1, z1] = e[v];
        a.merge(x1, y1);
        a.merge(x2, y2);
        f[u] = 1;
        f[v] = 1;
    }
    for (int i = 1; i <= m; i++)
    {
        if (f[i])
            v2.push_back(e[i]);
        else
            v1.push_back(e[i]);
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    int ans = 0;
    for (auto tt : v1)
    {
        if (!a.same(tt.x, tt.y))
        {
            c.merge(tt.x, tt.y);
            a.merge(tt.x, tt.y);
            ans += tt.w;
        }
        if (a.size(1) == n)
            break;
    }
    if ((a.size(1) != n))
    {
        cout << -1 << endl;
        return;
    }
    int mm = 0;
    for (int i = 1; i <= n; i++)
    {
        // cout << c.find(i) << ' ';
        if (c.find(i) == i)
            mm++;
    }
    //  cout << endl;
    DSU b;
    b.init(n + 1);
    int minn = 1e9;
    set<int> st;
    map<int, int> ma;
    for (int i = 0; i < 1 << q; i++)
    {
        int sum = 0;
        for (int j = 0; j < q; j++)
        {
            auto [xx, yy] = pa[j];
            auto [x2, y2, z2] = e[yy];
            auto [x1, y1, z1] = e[xx];
            b.f[x1] = c.find(x1);
            b.f[y1] = c.find(y1);
            b.f[x2] = c.find(x2);
            b.f[y2] = c.find(y2);
        }
        for (int j = 0; j < q; j++)
        {
            auto [xx, yy] = pa[j];
            auto [x2, y2, z2] = e[yy];
            auto [x1, y1, z1] = e[xx];
            if ((i >> j) & 1)
            {

                b.merge(x2, y2);
                if (ma[yy])
                    sum += z2;
                ma[yy] = 1;
            }
            else
            {
                b.merge(x1, y1);
                if (ma[xx])
                    sum += z1;
                ma[xx] = 1;
            }
        }
        int ttt = e[pa[0].first].x;
        for (auto tt : v2)
        {
            //  cout << b.f[tt.x] << ' ' << b.f[tt.y] << endl;
            //  cout << tt.x << ' ' << tt.y << endl;
            if (!b.same(tt.x, tt.y))
            {

                b.merge(tt.x, tt.y);
                sum += tt.w;
            }
        }
        //  cout << sum << endl;
        minn = min(minn, sum);
    }
    cout << ans + minn << endl;
}
signed main()
{

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    // cin>>T;
    while (T--)
        solve();
}
/*
 */

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5804kb

input:

4 6
1 1 2
2 4 3
1 1 4
2 4 4
3 2 4
1 3 4
1
1 2

output:

8

result:

wrong answer 1st lines differ - expected: '11', found: '8'