結果

問題 No.748 yuki国のお財布事情
ユーザー math957963math957963
提出日時 2018-10-19 21:58:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,484 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 75,368 KB
実行使用メモリ 8,764 KB
最終ジャッジ日時 2023-08-12 00:54:33
合計ジャッジ時間 4,021 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,640 KB
testcase_01 AC 3 ms
4,572 KB
testcase_02 AC 3 ms
4,592 KB
testcase_03 AC 3 ms
6,620 KB
testcase_04 AC 3 ms
4,592 KB
testcase_05 AC 3 ms
4,560 KB
testcase_06 AC 3 ms
4,592 KB
testcase_07 AC 2 ms
4,652 KB
testcase_08 AC 3 ms
6,604 KB
testcase_09 AC 2 ms
4,596 KB
testcase_10 AC 3 ms
6,596 KB
testcase_11 AC 3 ms
6,576 KB
testcase_12 AC 2 ms
4,600 KB
testcase_13 AC 9 ms
4,968 KB
testcase_14 AC 12 ms
6,752 KB
testcase_15 AC 9 ms
4,944 KB
testcase_16 AC 20 ms
5,348 KB
testcase_17 AC 44 ms
7,796 KB
testcase_18 AC 47 ms
6,832 KB
testcase_19 AC 47 ms
6,340 KB
testcase_20 AC 42 ms
7,088 KB
testcase_21 AC 46 ms
7,628 KB
testcase_22 AC 2 ms
4,524 KB
testcase_23 AC 3 ms
6,596 KB
testcase_24 AC 3 ms
4,484 KB
testcase_25 AC 43 ms
8,588 KB
testcase_26 AC 48 ms
8,764 KB
testcase_27 AC 48 ms
7,908 KB
testcase_28 AC 31 ms
5,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 200000

int p[N];
int d[N];

void init(void){
	f(i, N){
		p[i] = i;
		d[i] = 1;
	}
	return;
}

int pa(int x){
	if (p[x] == x)return x;
	else return p[x] = pa(p[x]);
}


void uni(int x, int y){
	int px = pa(x);
	int py = pa(y);
	if (px == py)return;
	if (d[px] < d[py]){
		p[px] = py;
		d[py] = max(d[py], d[px] + 1);
	}
	else{
		p[py] = px;
		d[px] = max(d[px], d[py] + 1);
	}
	return;
}

bool jud(int x, int y){
	int px = pa(x);
	int py = pa(y);
	if (px == py)return true;
	else return false;
}


int main(){
	int n,m,k;
	int x, y, z;
	int a[N];
	int b[N];
	long long c[N];
	bool used[N];
	long long s, ans;
	bool v = true;
	vector < pair<long long, pair<int, int> > >pe;
	ans = 0;
	init();
	scanf("%d %d %d", &n, &m, &k);
	f(i, m){
		scanf("%d %d %lld", &a[i], &b[i], &c[i]);
		a[i]--;
		b[i]--;
		used[i] = false;
	}
	f(i, k){
		scanf("%d", &x);
		x--;
		used[x] = true;
		uni(a[x], b[x]);
	}
	f(i, m){
		if (!used[i]){
			pe.push_back(make_pair(c[i], make_pair(a[i], b[i])));
		}
	}
	sort(pe.begin(), pe.end());
	f(i, pe.size()){
		if (jud(pe[i].second.first, pe[i].second.second)){
			ans += pe[i].first;
		}
		else{
			uni(pe[i].second.first, pe[i].second.second);
		}
	}
	printf("%lld\n", ans);


	return 0;
}
0