#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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
4 ms |
2892 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
4 ms |
2892 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |