#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]);
}
int uni(int a, int b){
if(rnk[a] > rnk[b]) swap(a, b);
parent[a] = b;
if(rnk[a] == rnk[b]) rnk[b]++;
}
int 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) + 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 INT_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];
}
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);
}
Compilation message
crocodile.cpp: In function 'int uni(int, int)':
crocodile.cpp:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
38 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
229 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
229 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
229 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |