QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353289#1825. The King's GuardsLaStataleBlueTL 20ms7716kbC++234.4kb2024-03-14 01:31:072024-03-14 01:31:07

Judging History

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

  • [2024-03-14 01:31:07]
  • 评测
  • 测评结果:TL
  • 用时:20ms
  • 内存:7716kb
  • [2024-03-14 01:31:07]
  • 提交

answer

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

using pi = pair<int,int>;
#define sz(x) int((x).size())
template<class F> struct Dinic {
    struct Edge { int to, rev; F cap; };
    int N; vector<vector<Edge>> adj;
    void init(int _N) { N = _N; adj.resize(N); } //0-based
    pi ae(int a, int b, F cap, F rcap = 0) {
        assert(min(cap,rcap) >= 0); // saved me > once
        adj[a].push_back({b,sz(adj[b]),cap});
        adj[b].push_back({a,sz(adj[a])-1,rcap});
        return {a,sz(adj[a])-1};
    }
    F edgeFlow(pi loc) { // get flow along original edge
        const Edge& e = adj.at(loc.first).at(loc.second);
        return adj.at(e.to).at(e.rev).cap;
    }
    vector<int> lev, ptr;
    bool bfs(int s,int t){//level=shortest dist from source
        lev = ptr = vector<int>(N);
        lev[s] = 1; queue<int> q({s});
        while (sz(q)) { int u = q.front(); q.pop();
            for(auto& e: adj[u]) if (e.cap && !lev[e.to]) {
                q.push(e.to), lev[e.to] = lev[u]+1;
                if (e.to == t) return 1;
            }
        }
        return 0;
    }
    F dfs(int v, int t, F flo) {
        if (v == t) return flo;
        for (int& i = ptr[v]; i < sz(adj[v]); i++) {
            Edge& e = adj[v][i];
            if (lev[e.to]!=lev[v]+1||!e.cap) continue;
            if (F df = dfs(e.to,t,min(flo,e.cap))) {
                e.cap -= df; adj[e.to][e.rev].cap += df;
                return df; } // saturated $\geq1$ one edge
        }
        return 0;
    }
    F maxFlow(int s, int t) {
        F tot = 0; while (bfs(s,t)) while (F df =
            dfs(s,t,numeric_limits<F>::max())) tot += df;
        return tot;
    }
};

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);
    vector<bool> req(n,false);
   
    auto calc = [&](){
        Dinic<int> mf;
        int source = g+n, sink = n+g+1;
        mf.init(n+g+2);
        
        for(int i=0;i<g;i++){
            for(auto x : opz[i]){
                mf.ae(i,g+ds.find(x),1);
            }
        }
        
        for(int i=0;i<g;i++)mf.ae(source,i,1);
        for(int i=0;i<n;i++)mf.ae(g+i,sink,1);
        
        if(mf.maxFlow(source,sink)!=g){
            cout<<"-1\n";
            exit(0);
        }
        
        vector vis(n+g,false);
        
        auto dfs = [&](auto &dfs,int nodo)->void{
            if(vis[nodo])return;
            vis[nodo]=true;
            
            for(auto [to,rev,cap] : mf.adj[nodo]){
                if(to!=sink && to!=source && cap==0)dfs(dfs,to);
            }
        };
        
        for(int i=0;i<n;i++){
            bool ok=true;
            for(auto [to,rev,cap] : mf.adj[g+i]){
                if(to!=sink && cap==1)ok=false;
            }
            
            if(ok)dfs(dfs,g+i);
        }
        
        for(int i=0;i<n;i++){
            if(vis[g+i]==false)req[i]=true;
            else req[i]=false;
        }
    };
    
    calc();
    
    
    int ans=0;
    for(auto [cost,x,y] : ed){
        if(!req[ds.find(x)] || !req[ds.find(y)]){
            if(ds.join(x,y))ans+=cost;
            calc();
        }
    }
    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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:

8

result:

ok answer is '8'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3876kb

input:

10 19 6
1 5 761
6 8 606
3 9 648
2 4 115
5 8 814
1 2 712
4 10 13
5 10 797
3 4 956
1 7 73
5 7 192
2 7 110
5 9 302
3 6 120
6 9 494
1 3 668
3 7 966
6 10 974
3 8 41
2 10 5
3 6 4 3
2 1 7
2 10 8
3 10 7 8
2 2 1

output:

429

result:

ok answer is '429'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

10 43 3
1 3 656
2 6 856
4 10 99
5 6 900
2 7 766
4 7 582
2 8 135
5 7 831
3 5 12
3 10 789
1 8 66
4 9 390
1 7 238
6 7 960
1 4 624
3 9 602
7 10 366
5 8 526
2 9 561
6 10 722
2 5 904
3 4 35
1 9 768
5 9 457
6 8 61
4 6 192
4 5 96
5 10 747
8 9 611
7 8 953
3 8 449
2 4 228
1 6 197
9 10 160
3 6 869
1 2 785
4 8 ...

output:

526

result:

ok answer is '526'

Test #4:

score: 0
Accepted
time: 14ms
memory: 3808kb

input:

277 9038 1
226 260 740
44 226 376
151 263 611
67 269 241
120 181 677
259 271 782
37 52 310
48 152 452
168 266 823
85 234 100
46 201 738
129 153 301
69 147 434
13 72 764
13 234 316
171 222 398
214 255 21
112 158 430
20 118 407
45 152 971
205 214 272
221 275 362
198 268 472
117 176 207
31 75 652
139 1...

output:

5375

result:

ok answer is '5375'

Test #5:

score: 0
Accepted
time: 20ms
memory: 3996kb

input:

297 27966 132
15 197 980
226 259 950
161 168 142
118 176 834
157 221 806
24 210 432
212 242 838
110 166 177
78 170 801
52 166 3
89 213 448
45 170 626
250 251 268
93 222 679
7 128 839
5 7 320
132 191 1
192 295 717
36 231 542
162 175 508
173 178 458
211 272 926
46 168 145
19 150 805
165 262 198
50 179...

output:

775

result:

ok answer is '775'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3808kb

input:

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

output:

17

result:

ok answer is '17'

Test #7:

score: 0
Accepted
time: 10ms
memory: 5328kb

input:

300 44850 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

1000

result:

ok answer is '1000'

Test #8:

score: 0
Accepted
time: 9ms
memory: 7716kb

input:

300 44850 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

1000

result:

ok answer is '1000'

Test #9:

score: -100
Time Limit Exceeded

input:

300 44850 150
1 2 4
1 3 4
1 4 4
1 5 3
1 6 10
1 7 4
1 8 8
1 9 5
1 10 4
1 11 9
1 12 9
1 13 1
1 14 3
1 15 5
1 16 7
1 17 5
1 18 2
1 19 6
1 20 4
1 21 10
1 22 5
1 23 7
1 24 2
1 25 2
1 26 4
1 27 4
1 28 5
1 29 8
1 30 10
1 31 9
1 32 1
1 33 7
1 34 5
1 35 4
1 36 6
1 37 8
1 38 1
1 39 2
1 40 1
1 41 1
1 42 1
1 43...

output:


result: