QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#503415#7069. FarmDecadeWA 748ms79832kbC++174.7kb2024-08-03 18:32:152024-08-03 18:32:20

Judging History

This is the latest submission verdict.

  • [2024-08-03 18:32:20]
  • Judged
  • Verdict: WA
  • Time: 748ms
  • Memory: 79832kb
  • [2024-08-03 18:32:15]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
mt19937 rng;
uniform_int_distribution <ull> dist(0,1ull<<63);
//#define mp make_pair
inline void print(__int128 x){//输出模板 
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
inline int read()
{
    int X = 0;
    bool flag = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            flag = 0;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        X = (X << 1) + (X << 3) + ch - '0';
        ch = getchar();
    }
    if (flag)
        return X;
    return ~(X - 1);
}
const ll mod = 1e9 + 7,INF = 0x3f3f3f3f;
ll qpow(ll a, ll b)
{
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1)
    {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
const int N = 500010,M = 500010;

struct edge
{
    int u,v,w;
    bool operator<(const edge x) const
    {
        return w<x.w;
    }
}e[M];

bool cmp(int x,int y)
{
    return e[x].w<e[y].w;
}

int vis[N];
int n,m,ans,res,id[N],st[N];
vector<int> p;
PII que[20];
map<PII,int> mp;

struct mst
{
    int fa[N];
    void init()
    {
        for(int i=1;i<=n;i++) fa[i] = i;
    }
    int find(int x)
    {
        if(fa[x]!=x) return fa[x] = find(fa[x]);
        return fa[x];
    }    
    void merge(int x,int y,int c)
    {
        int xx = find(x),yy = find(y);
        if(xx!=yy)
        {
            fa[xx] = yy;
            res += c;
        }
    }
}fa;


void solve()
{
    cin>>n>>m;
    fa.init();
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        e[i] = {a,b,c};
        id[i] = i;
        if(mp.count({a,b})) mp[{a,b}] = min(mp[{a,b}],c);
        else mp[{a,b}] = c;
        swap(a,b);
        if(mp.count({a,b})) mp[{a,b}] = min(mp[{a,b}],c);
        else mp[{a,b}] = c;
    }
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int x,y;
        cin>>x>>y;
        vis[x] = 1,vis[y] = 1;
        que[i] = {x,y};
        p.push_back(e[x].u),p.push_back(e[x].v);
        fa.merge(e[x].u,e[x].v,0);
        p.push_back(e[y].u),p.push_back(e[y].v);
        fa.merge(e[y].u,e[y].v,0);
    }
    sort(p.begin(),p.end());
    p.erase(unique(p.begin(),p.end()),p.end());
    sort(id+1,id+1+m,cmp);
    for(int i=1;i<=m;i++)
    {
        if(vis[id[i]]) continue;
        int u = e[id[i]].u,v = e[id[i]].v,w = e[id[i]].w;
        fa.merge(u,v,w);
    }

    int c = 0;
    for(int i=1;i<=n;i++) 
    {
        if(fa.fa[i]==i)
        {
            c++;
        } 
    }
    if(c>1)
    {
        cout<<-1<<'\n';
        return;
    }
    fa.init();
    int rest = res;
    ans = INF;
    for(int i=0;i<(1<<q);i++)
    {
        res = rest;
        vector<edge> vc;
        for(auto it:p) fa.fa[it] = it;
        for(int j=0;j<q;j++) st[que[j].first] = st[que[j].second] = 0;
        for(int j=0;j<q;j++)
        {
            if((i>>j)&1)
            {
                if(st[que[j].first]) continue;
                vc.push_back(e[que[j].first]),st[que[j].first] = 1;
            }
            else
            {
                if(st[que[j].second]) continue;
                vc.push_back(e[que[j].second]),st[que[j].second] = 1;
            }
        }
        for(auto it:vc)
        {
            int u = it.u,v = it.v,w = it.w;
            u = fa.find(u),v = fa.find(v);
            if(u!=v) fa.fa[v] = u; 
            res += w;
        }
        vc.clear();
        for(int j=0;j<q;j++)
        {
            if(!((i>>j)&1))
            {
                if(st[que[j].first]) continue;
                vc.push_back(e[que[j].first]),st[que[j].first] = 1;
            }
            else
            {
                if(st[que[j].second]) continue;
                vc.push_back(e[que[j].second]),st[que[j].second] = 1;
            }
        }
        sort(vc.begin(),vc.end());
        for(auto it:vc)
        {
            int u = it.u,v = it.v,w = it.w;
            u = fa.find(u),v = fa.find(v);
            if(u!=v)
            {
                fa.fa[u] = v;
                res += mp[{u,v}];
            }
        }
        ans = min(ans,res);
    }
    cout<<ans<<'\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    //cin>>t;
    while (t--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 9712kb

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:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 747ms
memory: 79660kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 748ms
memory: 79832kb

input:

100000 500000
34497 87538 658
69862 2776 861
93620 16992 904
77910 81200 149
83935 83752 880
17602 75791 259
85887 53289 710
4200 79358 181
8518 19264 737
94665 47462 822
50632 51994 143
55224 59127 656
615 92858 150
48450 9465 58
35713 45287 140
64861 32248 517
70296 45113 153
11189 90316 809
40673...

output:

12148224

result:

ok single line: '12148224'

Test #4:

score: 0
Accepted
time: 115ms
memory: 15576kb

input:

1 500000
1 1 963
1 1 349
1 1 157
1 1 6
1 1 312
1 1 377
1 1 783
1 1 42
1 1 18
1 1 327
1 1 499
1 1 824
1 1 343
1 1 798
1 1 193
1 1 667
1 1 378
1 1 641
1 1 692
1 1 622
1 1 584
1 1 590
1 1 324
1 1 858
1 1 914
1 1 601
1 1 734
1 1 61
1 1 559
1 1 681
1 1 825
1 1 888
1 1 585
1 1 55
1 1 818
1 1 190
1 1 278
1...

output:

1605

result:

ok single line: '1605'

Test #5:

score: 0
Accepted
time: 139ms
memory: 16072kb

input:

5 500000
5 1 817
2 1 273
3 5 674
1 5 15
5 2 872
3 4 728
3 2 807
5 3 28
2 5 96
1 5 100
4 2 224
4 4 980
5 5 727
2 2 520
4 1 29
2 1 142
4 2 963
4 4 118
4 4 615
4 3 719
5 3 200
5 2 746
4 2 68
5 4 859
1 3 182
3 4 286
3 1 229
4 1 895
2 1 730
1 2 622
2 4 913
2 1 697
5 5 130
4 5 507
5 2 425
2 4 716
2 1 884
...

output:

3097

result:

ok single line: '3097'

Test #6:

score: 0
Accepted
time: 153ms
memory: 16268kb

input:

10 500000
3 8 138
10 7 593
4 3 8
7 5 516
10 4 49
3 8 601
6 7 481
8 5 429
6 4 241
1 6 504
6 2 252
7 1 656
5 1 350
5 9 485
7 8 669
5 8 630
9 9 324
1 3 427
1 2 309
5 10 236
4 6 926
8 7 34
5 1 336
7 5 581
4 5 228
10 3 909
2 9 726
4 2 444
10 1 55
1 2 244
5 8 261
2 7 556
10 2 165
6 3 657
7 5 580
7 1 827
1...

output:

1533

result:

ok single line: '1533'

Test #7:

score: -100
Wrong Answer
time: 384ms
memory: 16288kb

input:

100 500000
10 46 133
79 13 987
26 2 743
8 47 390
79 19 737
11 64 197
16 65 207
73 9 944
77 58 841
50 3 245
81 100 293
21 12 713
60 65 155
89 87 865
88 67 278
9 15 920
46 52 704
26 26 731
44 98 525
20 68 346
14 95 932
84 19 697
41 21 290
83 24 750
3 71 369
54 80 396
20 70 208
25 55 456
40 22 938
90 1...

output:

2264

result:

wrong answer 1st lines differ - expected: '2209', found: '2264'