結果
| 問題 | No.3781 Erase One, Gain from the Other |
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2026-10-30 23:15:28 |
| 言語 | Rust (1.99.0 + proconio + num + itertools + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 56 ms / 2,000 ms |
| + 103µs | |
| コード長 | 4,213 bytes |
| 記録 | |
| コンパイル時間 | 4,337 ms |
| コンパイル使用メモリ | 195,756 KB |
| 実行使用メモリ | 24,016 KB |
| 最終ジャッジ日時 | 2026-10-30 23:15:45 |
| 合計ジャッジ時間 | 10,918 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge7_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 34 |
コンパイルメッセージ
warning: use of deprecated constant `std::i32::MAX`: replaced by the `MAX` associated constant on this type
--> src/main.rs:103:31
|
103 | assert!(n < std::i32::MAX as usize);
| ^^^
|
= note: `#[warn(deprecated)]` on by default
warning: type alias `Map` is never used
--> src/main.rs:84:6
|
84 | type Map<K, V> = BTreeMap<K, V>;
| ^^^
|
= note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
warning: type alias `Set` is never used
--> src/main.rs:85:6
|
85 | type Set<T> = BTreeSet<T>;
| ^^^
warning: type alias `Deque` is never used
--> src/main.rs:86:6
|
86 | type Deque<T> = VecDeque<T>;
| ^^^^^
ソースコード
fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
let t: u32 = sc.next();
for _ in 0..t {
let n: usize = sc.next();
let m: usize = sc.next();
let mut a = sc.next_vec::<i64>(n);
a.insert(0, 0);
let mut e = vec![(0, 0, 0); m];
for e in e.iter_mut() {
let x = sc.next::<usize>();
let y = sc.next::<usize>();
*e = (a[x] + a[y], x, y);
}
e.extend((1..=n).map(|i| (a[i], 0, i)));
e.sort();
let mut dsu = DSU::new(n + 1);
let mut ans = -a.iter().sum::<i64>();
for (w, a, b) in e.into_iter().rev() {
if dsu.unite(a, b).is_some() {
ans += w;
}
}
writeln!(out, "{}", ans).ok();
}
}
/*
誤読
黒くするのとは逆の頂点の値だけの点が入る
木の場合は解ける?
辺に向きつける、出次数*頂点の値の分だけ点が入る
本当か?
-> o <-
という形もng
木を塗り切るのは可能
根一つだけではない、サンプル1
ある木のスコア
A_v + A_u の和 - sum A + max A
max?
超頂点0作ってA_0=0とすれば最大全域木になってる?
その場合はmaxはいらない
*/
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
use std::str::FromStr;
pub struct Scanner<'a> {
it: std::str::SplitWhitespace<'a>,
}
impl<'a> Scanner<'a> {
pub fn new(s: &'a String) -> Scanner<'a> {
Scanner {
it: s.split_whitespace(),
}
}
pub fn next<T: FromStr>(&mut self) -> T {
self.it.next().unwrap().parse::<T>().ok().unwrap()
}
pub fn next_bytes(&mut self) -> Vec<u8> {
self.it.next().unwrap().bytes().collect()
}
pub fn next_chars(&mut self) -> Vec<char> {
self.it.next().unwrap().chars().collect()
}
pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
(0..len).map(|_| self.next()).collect()
}
}
}
// ---------- end scannner ----------
use std::io::Write;
use std::collections::*;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
fn main() {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let mut sc = scanner::Scanner::new(&s);
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
run(&mut sc, &mut out);
}
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_find ----------
akakimidori