Submission #1160296


Source Code Expand

use std::io::{self, Stdin};
use std::str::{self, FromStr};
use std::error::Error;
use std::collections::VecDeque;
fn exec() {
    let mut sc = Scanner::new();
    let (n, m): (usize, usize) = (sc.ne(), sc.ne());
    let mut uf = UnionFind::new(n);
    let mut edge = Vec::new();
    let mut adj = vec![Vec::new(); n];
    for _ in 0..m {
        let u = sc.ne::<usize>() - 1;
        let v = sc.ne::<usize>() - 1;
        edge.push((u, v));
        uf.unite(u, v);
        adj[u].push(v);
        adj[v].push(u);
    }
    let mut cnt = vec![0i32; n];
    for &(u, _) in &edge {
        cnt[uf.root(u)] += 1;
    }
    let (mut ans1, mut ans2, mut ans3) = (0i64, 0i64, 0i64);
    let mut used = vec![vec![false; 2]; n];
    let mut is_oddcycle = |i: usize| -> bool {
        let mut que = VecDeque::new();
        que.push_back((i, 0));
        used[i][0] = true;;
        while let Some(cur) = que.pop_front() {
            for &nxt in &adj[cur.0] {
                if used[nxt][cur.1 ^ 1] {
                    continue;
                }
                used[nxt][cur.1 ^ 1] = true;
                que.push_back((nxt, cur.1 ^ 1));
            }
        }
        used[i][1]
    };
    for i in 0..n {
        if uf.root(i) != i {
            continue;
        }
        if uf.size(i) == 1 {
            ans3 += 1;
            continue;
        }
        if cnt[uf.root(i)] >= uf.size(i) {
            if is_oddcycle(i) {
                ans2 += 1;
            } else {
                ans1 += 1;
            }
        } else {
            ans1 += 1;
        }
    }
    let mut ans = 0i64;
    ans += (n as i64) * ans3 * 2 - ans3 * ans3;
    ans += 2 * ans1 * ans1;
    ans += ans2 * ans2;
    ans += 2 * ans1 * ans2;
    println!("{}", ans);
}

#[allow(dead_code)]
struct UnionFind {
    data: Vec<i32>,
    group: usize,
}
#[allow(dead_code)]
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            data: vec![-1i32; n],
            group: n,
        }
    }

    //経路圧縮のみ
    fn unite(&mut self, x: usize, y: usize) -> bool {
        let x = self.root(x);
        let y = self.root(y);
        if x == y {
            return false;
        }
        self.data[x] += self.data[y];
        self.data[y] = x as i32;
        self.group -= 1;
        true
    }

    fn root(&mut self, x: usize) -> usize {
        if self.data[x] < 0 {
            x
        } else {
            let par = self.data[x] as usize;
            let res = self.root(par);
            self.data[x] = res as i32;
            res
        }
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    fn size(&mut self, x: usize) -> i32 {
        let par = self.root(x);
        -self.data[par]
    }
}

fn main() {
    const STACK: usize = 16 * 1024 * 1024;
    let _ = std::thread::Builder::new()
        .stack_size(STACK)
        .spawn(|| { exec(); })
        .unwrap()
        .join()
        .unwrap();
}

#[allow(dead_code)]
struct Scanner {
    stdin: Stdin,
    id: usize,
    buf: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            stdin: io::stdin(),
            id: 0,
            buf: Vec::new(),
        }
    }
    fn next_line(&mut self) -> Option<String> {
        let mut res = String::new();
        match self.stdin.read_line(&mut res) {
            Ok(0) => None,
            Ok(_) => Some(res),
            Err(why) => panic!("error in read_line: {}", why.description()),
        }
    }
    fn next<T: FromStr>(&mut self) -> Option<T> {
        while self.buf.len() == 0 {
            self.buf = match self.next_line() {
                Some(r) => {
                    self.id = 0;
                    r.trim().as_bytes().to_owned()
                }
                None => return None,
            };
        }
        let l = self.id;
        assert!(self.buf[l] != b' ');
        let n = self.buf.len();
        let mut r = l;
        while r < n && self.buf[r] != b' ' {
            r += 1;
        }
        let res = match str::from_utf8(&self.buf[l..r]).ok().unwrap().parse::<T>() {
            Ok(s) => Some(s),
            Err(_) => {
                panic!("parse error, {:?}",
                       String::from_utf8(self.buf[l..r].to_owned()))
            }
        };
        while r < n && self.buf[r] == b' ' {
            r += 1;
        }
        if r == n {
            self.buf.clear();
        } else {
            self.id = r;
        }
        res
    }
    fn ne<T: FromStr>(&mut self) -> T {
        self.next::<T>().unwrap()
    }
}

Submission Info

Submission Time
Task C - Squared Graph
User mio_h1917
Language Rust (1.15.1)
Score 800
Code Size 4788 Byte
Status AC
Exec Time 95 ms
Memory 29040 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 2
AC × 32
Set Name Test Cases
Sample sample1.txt, sample2.txt
All sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
in1.txt AC 3 ms 8572 KB
in10.txt AC 10 ms 16764 KB
in11.txt AC 3 ms 8572 KB
in12.txt AC 28 ms 20860 KB
in13.txt AC 28 ms 20860 KB
in14.txt AC 47 ms 22908 KB
in15.txt AC 14 ms 18812 KB
in16.txt AC 37 ms 20860 KB
in17.txt AC 32 ms 20860 KB
in18.txt AC 63 ms 24952 KB
in19.txt AC 53 ms 22908 KB
in2.txt AC 3 ms 8572 KB
in20.txt AC 54 ms 22908 KB
in21.txt AC 95 ms 29040 KB
in22.txt AC 62 ms 18720 KB
in23.txt AC 49 ms 16688 KB
in24.txt AC 10 ms 16764 KB
in25.txt AC 92 ms 29040 KB
in26.txt AC 3 ms 8572 KB
in27.txt AC 3 ms 8572 KB
in28.txt AC 93 ms 29036 KB
in3.txt AC 3 ms 8572 KB
in4.txt AC 3 ms 8572 KB
in5.txt AC 63 ms 27000 KB
in6.txt AC 13 ms 18812 KB
in7.txt AC 13 ms 18812 KB
in8.txt AC 17 ms 18812 KB
in9.txt AC 33 ms 20860 KB
sample1.txt AC 3 ms 8572 KB
sample2.txt AC 3 ms 8572 KB