#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define int long long
#define itr ::iterator
typedef pair<int,int> pii;
const int MAX=1e6;
pair<int,pii> edge[MAX];
vector<pii> vec[MAX];
int N,K,M,res,arr[MAX],mark[MAX],height[MAX],Parent[MAX];
int par[MAX],ranks[MAX];
void init()
{
for(int A=1;A<=N;A++)
{
par[A]=A;
ranks[A]=0;
}
return ;
}
int root(int X)
{
if(par[X]==X)
return X;
return par[X]=root(par[X]);
}
void unions(int X,int Y)
{
int u=root(X),v=root(Y);
if(ranks[u]>ranks[v])
swap(u,v);
par[u]=v;
ranks[u]+=(ranks[u]==ranks[v]);
return ;
}
void dfs(int node,int par,int h)
{
height[node]=h;
for(auto A:vec[node])
{
if(A.first==par)
continue;
Parent[A.first]=node;
dfs(A.first,node,h+1);
}
return ;
}
void dfs(int node,int par)
{
for(auto A:vec[node])
{
if(A.first==par)
continue;
if(mark[A.first])
res+=A.second;
dfs(A.first,node);
}
return ;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin>>N>>K>>M;
for(int A=1;A<=K;A++)
cin>>arr[A];
for(int A=1;A<=M;A++)
cin>>edge[A].second.first>>edge[A].second.second>>edge[A].first;
sort(edge+1,edge+M+1);
init();
for(int A=1;A<=M;A++)
{
if(root(edge[A].second.first)!=root(edge[A].second.second))
{
unions(edge[A].second.first,edge[A].second.second);
vec[edge[A].second.first].pb(mp(edge[A].second.second,edge[A].first));
vec[edge[A].second.second].pb(mp(edge[A].second.first,edge[A].first));
}
}
dfs(1,0,0);
int u,v;
for(int A=1;A<=K;A++)
{
for(int B=A+1;B<=K;B++)
{
u=arr[A];
v=arr[B];
if(height[u]>height[v])
swap(u,v);
while(height[v]>height[u])
{
mark[v]=1;
v=Parent[v];
}
while(u!=v)
{
mark[u]=1;
mark[v]=1;
u=Parent[u];
v=Parent[v];
}
}
}
dfs(1,0);
cout<<res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
23928 KB |
Output is correct |
2 |
Correct |
25 ms |
24028 KB |
Output is correct |
3 |
Incorrect |
28 ms |
24032 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
284 ms |
39472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
39472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
278 ms |
41572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
234 ms |
43584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |