Submission #1697749
Source Code Expand
#include <assert.h> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <algorithm> #include <numeric> #include <functional> #include <iostream> #include <string> #include <array> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <complex> #include <bitset> typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) using namespace std; // Union-Find木 template <typename T> struct UnionFind { int n; vector<T> par; vector<T> rank; explicit UnionFind(int n) { this-> n = n; par = vector<T>(n, 0); iota(par.begin(), par.end(), 0); rank = vector<T>(n, 0); } // 木の根を求める T root(T x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } // x と y が同じ集合に属するか調べる bool same(T x, T y) { return root(x) == root(y); } // x と y が属する集合を併合する void unite(T x, T y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } // 連結成分の数を数える int groups() { int ans = 0; for (int i = 0; i < n; i++) { if (par[i] == i) ans++; } return ans; } }; template <typename T> vector<T> uniquify(vector<T> values) { sort(values.begin(), values.end()); values.erase(unique(values.begin(), values.end()), values.end()); return values; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; UnionFind<int> u(N); REP(i, M) { int a, b; cin >> a >> b; a--; b--; u.unite(a, b); } cout << u.groups() - 1 << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | B - 道路工事 |
User | sinhrks |
Language | C++14 (GCC 5.4.1) |
Score | 100 |
Code Size | 1983 Byte |
Status | AC |
Exec Time | 21 ms |
Memory | 1024 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample1.txt, sample2.txt |
All | 0.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0.txt | AC | 1 ms | 256 KB |
1.txt | AC | 1 ms | 256 KB |
10.txt | AC | 1 ms | 256 KB |
11.txt | AC | 1 ms | 256 KB |
12.txt | AC | 1 ms | 256 KB |
13.txt | AC | 1 ms | 256 KB |
14.txt | AC | 1 ms | 256 KB |
15.txt | AC | 15 ms | 256 KB |
16.txt | AC | 2 ms | 1024 KB |
17.txt | AC | 2 ms | 1024 KB |
18.txt | AC | 2 ms | 1024 KB |
19.txt | AC | 21 ms | 1024 KB |
2.txt | AC | 1 ms | 256 KB |
3.txt | AC | 1 ms | 256 KB |
4.txt | AC | 1 ms | 256 KB |
5.txt | AC | 1 ms | 256 KB |
6.txt | AC | 1 ms | 256 KB |
7.txt | AC | 1 ms | 256 KB |
8.txt | AC | 1 ms | 256 KB |
9.txt | AC | 1 ms | 256 KB |
sample1.txt | AC | 1 ms | 256 KB |
sample2.txt | AC | 1 ms | 256 KB |