Submission #107233

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

const long long MAXN =2e5+5;
bool visited[MAXN];
long long fathers[MAXN];
map<pair<long long,long long>,long long> m1;
vector<long long> v1[MAXN];

long long found[MAXN];
set<long long> s1;
long long ans;
vector<pair<long long,pair<long long,long long>>> edges;
vector<long long> leaves;
long long anc(long long x){
    if(fathers[x]==x){
       return x;
    }
    return fathers[x] = anc(fathers[x]);
    
}
void unite(long long a, long long b){
    long long x = anc(a);
    long long y = anc(b);
    fathers[x] = y;
}
void dfs(long long curr,long long par){
    bool ok = false;
    for(long long 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() {
    long long n,m,k;
    cin>>n>>k>>m;
    for(long long i=0;i<k;i++){
        long long x;
        cin>>x;
        s1.insert(x);
        
    }
    for(long long i=0;i<m;i++){
        long long 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(long long i=1;i<=n;i++){
        fathers[i] = i;
    }
    for(auto x:edges){
       
        long long w = x.first;
        long long u = x.second.first;
        long long 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<long long> q1;
    for(long long x:leaves){
        //cout<<x<<endl;
        q1.push(x);
    }
    
    while(!q1.empty()){
        long long curr =q1.front();
        q1.pop();
        if(s1.find(curr) != s1.end()){
            continue;
        }
        if(visited[curr]){
            continue;
        }
        visited[curr] = true;
       // cout<<ans<<" "<<curr<<endl;
        for(long long x:v1[curr]){
            if(visited[x]){
                continue;
            }
            ans-= m1[make_pair(curr,x)];
            //cout<<curr<<" "<<x<<endl;
            q1.push(x);
        }
    }
    cout<<ans<<endl;
}

Compilation message

cities.cpp: In function 'void dfs(long long int, long long 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 6 ms 4992 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Incorrect 6 ms 4992 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 804 ms 40312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 5376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 914 ms 40216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 925 ms 40232 KB Output isn't correct
2 Halted 0 ms 0 KB -