답안 #107231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107231 2019-04-22T19:31:52 Z brcode Cities (BOI16_cities) C++14
0 / 100
622 ms 21796 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
using namespace std;

const int MAXN =1e5+5;

int fathers[MAXN];
map<pair<int,int>,int> m1;
vector<int> v1[MAXN];

int found[MAXN];
set<int> s1;
int ans;
vector<pair<int,pair<int,int>>> edges;
vector<int> leaves;
int anc(int x){
    if(fathers[x]==x){
       return x;
    }
    return fathers[x] = anc(fathers[x]);
    
}
void unite(int a, int b){
    int x = anc(a);
    int y = anc(b);
    fathers[x] = y;
}
void dfs(int curr,int par){
    bool ok = false;
    for(int x:v1[curr]){
        if(x!=par){
            dfs(x,curr);
            ok = true;
        }
    }
    if(!ok){
        leaves.push_back(curr);
    }
}
int main() {
    int n,m,k;
    cin>>n>>k>>m;
    for(int i=0;i<k;i++){
        int x;
        cin>>x;
        s1.insert(x);
        
    }
    for(int i=0;i<m;i++){
        int x,y,w;
        cin>>x>>y>>w;
        edges.push_back(make_pair(w,make_pair(x,y)));
        m1[make_pair(x,y)] = w;
    }
    sort(edges.begin(),edges.end());
    for(int i=1;i<=n;i++){
        fathers[i] = i;
    }
    for(auto x:edges){
        int w = x.first;
        int u = x.second.first;
        int v = x.second.second;
        //cout<<w<<endl;
        if(anc(u) == anc(v)){
            continue;
        }
        
        ans+=w;
        unite(u,v);
        v1[u].push_back(v);
        v1[v].push_back(u);

    }
    dfs(1,1);
    queue<int> q1;
    for(int x:leaves){
        q1.push(x);
    }
    
    while(!q1.empty()){
        int curr =q1.front();
        q1.pop();
        if(s1.find(curr) == s1.end()){
            continue;
        }
        for(int x:v1[curr]){
            ans-= m1[make_pair(curr,x)];
            q1.push(x);
        }
    }
    cout<<ans<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 622 ms 21668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 2944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 590 ms 21796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 566 ms 21556 KB Output isn't correct
2 Halted 0 ms 0 KB -