結果

問題 No.3781 Erase One, Gain from the Other
コンテスト
ユーザー askr58
提出日時 2026-09-21 05:16:02
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 82 ms / 2,000 ms
+ 769µs
コード長 687 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,611 ms
コンパイル使用メモリ 201,772 KB
実行使用メモリ 11,400 KB
最終ジャッジ日時 2026-10-30 20:55:17
合計ジャッジ時間 8,271 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <ranges>
#include <algorithm>
#include <vector>
#include <atcoder/dsu>
using namespace std;
using ll=long long;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int ttt;
	cin>>ttt;
	while(ttt--){
		int n,m;
		cin>>n>>m;
		vector<int> a(n);
		vector<array<int,3>> edges;
		ll ans=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			edges.push_back({a[i],n,i});
			ans-=a[i];
		}
		for(int i=0;i<m;i++){
			int u,v;
			cin>>u>>v;
			u--;v--;
			edges.push_back({a[u]+a[v],u,v});
		}
		ranges::sort(edges,greater{});
		atcoder::dsu uf(n+1);
		for(auto[w,u,v]:edges){
			if(!uf.same(u,v)){
				ans+=w;
				uf.merge(u,v);
			}
		}
		cout<<ans<<endl;
	}
}
		
0