QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#348665#1825. The King's GuardsLaStataleBlueWA 0ms3796kbC++233.4kb2024-03-09 20:21:212024-03-09 20:21:22

Judging History

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

  • [2024-03-09 20:21:22]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3796kb
  • [2024-03-09 20:21:21]
  • 提交

answer

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

struct HopcroftKarp { //tested with $n,m\leq 10^5$ (75 ms)
    vector<int> g, l, r; int ans; // 0-based
    HopcroftKarp(int n, int m, const vector<pair<int,int>> &e)
    : g(e.size()), l(n, -1), r(m, -1), ans(0) {
        vector<int> deg(n + 1), a, p, q(n);
        for (auto &[x, y] : e) deg[x]++;
        for (int i = 1; i <= n; i++) deg[i] += deg[i - 1];
        for (auto &[x, y] : e) g[--deg[x]] = y;
        for (bool match=true; match;) {
            a.assign(n,-1), p.assign(n,-1); int t=0; match=false;
            for (int i = 0; i < n; i++)
                if (l[i] == -1) q[t++] = a[i] = p[i] = i;
            for (int i = 0; i < t; i++) {
                int x = q[i];
                if (~l[a[x]]) continue;
                for (int j = deg[x]; j < deg[x + 1]; j++) {
                    int y = g[j];
                    if (r[y] == -1) {
                        while (~y) r[y] = x, swap(l[x], y), x = p[x];
                        match = true; ans++; break;//ans=numero match
                    }
                    if(p[r[y]]==-1)q[t++]=y=r[y],p[y]=x,a[y]=a[x];
                }
            }
        }//nodi sx vengono matchati dando preferenza ai primi(?)
    }//nodi sx numerati da 0 a n-1, nodi dx da 0 a m-1
}; //l[i]= match del nodo sx i (-1 se non è matchato)

struct RollbackUF {
    vector<int> e; vector<pair<int,int>> st;
    RollbackUF(int n) : e(n, -1) {}
    int size(int x) { return -e[find(x)]; }
    int find(int x) { return e[x] < 0 ? x : find(e[x]); }
    int time() { return st.size(); }
    void rollback(int t) {
        for(int i=time();i-->t;) e[st[i].first] = st[i].second;
        st.resize(t);
    }
    bool join(int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return false;
        if (e[a] > e[b]) swap(a, b);
        st.push_back({a, e[a]}); st.push_back({b, e[b]});
        e[a] += e[b]; e[b] = a; return true;
    }
};

void solve([[maybe_unused]] int t){
    int n,r,g;
    cin>>n>>r>>g;
    
    vector<array<int,3>> ed;
    for(int i=0;i<r;i++){
        int a,b,c;
        cin>>a>>b>>c;
        a--;
        b--;
        ed.push_back({c,a,b});
    }
    sort(ed.begin(),ed.end());
    
    vector opz(g,vector<int>());
    for(int i=0;i<g;i++){
        int k;
        cin>>k;
        opz[i].resize(k);
        for(auto &j : opz[i]){
            cin>>j;
            j--;
        }
    }
    
    RollbackUF ds(n);
    auto check = [&]()->bool{
        vector<pair<int,int>> tmp;
        
        vector<set<int>> curr(g);
        for(int i=0;i<g;i++){
            for(auto j : opz[i]){
                tmp.emplace_back(i,ds.find(j));
            }
        }
        
        return HopcroftKarp(g,n,tmp).ans==g;
    };
    
    if(!check()){
        cout<<"-1\n";
    }else{
        int ans=0,comp=0;
        for(auto [cost,x,y] : ed){
            bool flag = ds.join(x,y);
            if(flag && !check()){
                ds.rollback(ds.time()-2);
            }else if(flag){
                ans+=cost;
                comp++;
                if(comp==g)break;
            }
        }
        cout<<ans<<"\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t=1;
    //cin>>t;
    for(int i=1;i<=t;i++)solve(i);
    
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3796kb

input:

5 6 2
1 2 1
1 3 4
2 4 2
2 5 5
3 4 7
4 5 3
2 1 2
2 2 4

output:

4

result:

wrong answer expected '8', found '4'