Submission #419518

#TimeUsernameProblemLanguageResultExecution timeMemory
419518Emin2004Crocodile's Underground City (IOI11_crocodile)C++14
46 / 100
4 ms2892 KiB
#include "crocodile.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, ll> #define F first #define S second const int N = 100005; const int mod = 1e9+7; struct edge{ int from; int to; ll weight; }; edge e[N * 10]; set<int> ext; vector<pii> a[N]; int parent[N], rnk[N], ext_cnt[N]; bool cmp(edge a, edge b) { return a.weight < b.weight; } int get_par(int a){ if(parent[a] == a) return a; return parent[a] = get_par(parent[a]); } void uni(int a, int b){ if(rnk[a] > rnk[b]) swap(a, b); parent[a] = b; if(rnk[a] == rnk[b]) rnk[b]++; } ll DFS(int node, int par){ ll mn1 = LONG_MAX, mn2 = LONG_MAX; if(ext.find(node) != ext.end()) { ext_cnt[node] = 1; return 0; } for(pii i : a[node]){ if(i.F == par) continue; ll cur = DFS(i.F, node); if(ext_cnt[i.F] == 0 || cur == LONG_MAX) continue; cur += i.S; ext_cnt[node] += ext_cnt[i.F]; if(cur <= mn1){ mn2 = mn1; mn1 = cur; } else if(cur < mn2) mn2 = cur; } if(ext_cnt[node] == 0 || mn1 == LONG_MAX || mn2 == LONG_MAX) return LONG_MAX; return mn2; } int travel_plan(int n, int m, int r[][2], int l[], int k, int p[]){ for(int i = 0; i < m; i++){ e[i].from = r[i][0]; e[i].to = r[i][1]; e[i].weight = l[i] * 1ll; } for(int i = 0; i < k; i++) ext.insert(p[i]); for(int i = 0; i < n; i++) parent[i] = i; sort(e, e + m, cmp); for(int i = 0; i < m; i++){ if(ext.find(e[i].from) != ext.end() || ext.find(e[i].to) != ext.end()) { a[e[i].from].pb({e[i].to, e[i].weight}); a[e[i].to].pb({e[i].from, e[i].weight}); continue; } int x = get_par(e[i].from); int y = get_par(e[i].to); if(x != y){ uni(x, y); a[e[i].from].pb({e[i].to, e[i].weight}); a[e[i].to].pb({e[i].from, e[i].weight}); } } return DFS(0, 0); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...