結果

問題 No.3781 Erase One, Gain from the Other
コンテスト
ユーザー askr58
提出日時 2026-07-20 03:17:24
言語 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  
実行時間 225 ms / 2,000 ms
+ 602µs
コード長 1,045 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,792 ms
コンパイル使用メモリ 211,572 KB
実行使用メモリ 19,704 KB
最終ジャッジ日時 2026-10-30 20:51:12
合計ジャッジ時間 10,238 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <numeric>
#include <set>
#include <ranges>
#include <algorithm>
#include <vector>
#include <cassert>
#include <atcoder/dsu>
using namespace std;
using ll=long long;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int ttt;
	cin>>ttt;
	assert(1<=ttt&&ttt<=200000);
	ll nsum=0,msum=0;
	while(ttt--){
		int n,m;
		cin>>n>>m;
		assert(1<=n&&n<=200000);
		assert(0<=m&&m<=200000);
		vector<int> a(n);
		vector<array<int,3>> edges;
		ll ans=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			assert(0<=a[i]&&a[i]<=1000000000);
			edges.push_back({a[i],n,i});
			ans-=a[i];
		}
		set<pair<int,int>> s;
		for(int i=0;i<m;i++){
			int u,v;
			cin>>u>>v;
			assert(1<=u&&u<v&&v<=n);
			assert(!s.count(make_pair(u,v)));
			s.insert(make_pair(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;
	}
	assert(nsum<=200000);
	assert(msum<=200000);
}
		
0