답안 #881080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881080 2023-11-30T14:23:29 Z irmuun Cities (BOI16_cities) C++17
0 / 100
491 ms 50572 KB
#include<bits/stdc++.h>

using namespace std;
 
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()

struct dsu{
    ll n;
    vector<ll>p,sz;
    dsu(ll n):n(n){
        p.resize(n+1);
        iota(all(p),0);
        sz.resize(n+1);
        fill(all(sz),1);
    }
    ll find(ll x){
        if(p[x]==x) return p[x];
        return p[x]=find(p[x]);
    }
    void merge(ll a,ll b){
        a=find(a);
        b=find(b);
        if(a!=b){
            if(sz[a]<sz[b]) swap(a,b);
            sz[a]+=sz[b];
            p[b]=a;
        }
    }
    bool same(ll a,ll b){
        a=find(a);
        b=find(b);
        return a==b;
    }
};

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,k,m;
    cin>>n>>k>>m;
    vector<bool>need(n+1,0);
    for(ll i=1;i<=k;i++){
        ll imp;
        cin>>imp;
        need[imp]=true;
    }
    set<ll>adj[n+1];
    vector<array<ll,3>>road(m);
    map<pair<ll,ll>,ll>mp;
    for(ll i=0;i<m;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        mp[{a,b}]=c;
        mp[{b,a}]=c;
        road[i]={c,a,b};
    }
    sort(all(road));
    dsu ds(n);
    ll ans=0;
    for(ll i=0;i<m;i++){
        auto [c,a,b]=road[i];
        if(ds.same(a,b)==false){
            adj[a].insert(b);
            adj[b].insert(a);
            ans+=c;
            ds.merge(a,b);
        }
    }
    queue<ll>q;
    for(ll i=1;i<=n;i++){
        if(adj[i].size()==1){
            q.push(i);
        }
    }
    while(!q.empty()){
        ll x=q.front();
        q.pop();
        if(need[x]==true){
            continue;
        }
        auto y=*adj[x].begin();
        ans-=mp[{x,y}];
        adj[x].erase(y);
        adj[y].erase(x);
        if(adj[y].size()==1){
            q.push(y);
        }
    }
    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 408 ms 50480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 442 ms 50416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 491 ms 50572 KB Output isn't correct
2 Halted 0 ms 0 KB -