Submission #107232

# Submission time Handle Problem Language Result Execution time Memory
107232 2019-04-22T19:42:52 Z brcode Cities (BOI16_cities) C++14
0 / 100
6000 ms 263168 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
using namespace std;

const int MAXN =2e5+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(v1[curr].size() == 1){
       // cout<<curr<<endl;
        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;
        m1[make_pair(y,x)] = 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;
        }
        // cout<<u<<" "<<v<<" "<<w<<endl;
        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)];
            //cout<<curr<<" "<<x<<endl;
            q1.push(x);
        }
    }
    cout<<ans<<endl;
}

Compilation message

cities.cpp: In function 'void dfs(int, int)':
cities.cpp:33:10: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
     bool ok = false;
          ^~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Runtime error 1901 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6077 ms 244332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2934 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6090 ms 219964 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6044 ms 224892 KB Time limit exceeded
2 Halted 0 ms 0 KB -