Submission #1353445
Source Code Expand
#include<bits/stdc++.h> using namespace std; #define REP(i,s,n) for(int i=s;i<n;++i) #define rep(i,n) REP(i,0,n) #define SORT(c) sort((c).begin(),(c).end()) #define IINF INT_MAX #define LLINF LLONG_MAX typedef long long ll; typedef pair<int, int> ii; struct UnionFind { vector<int> data; UnionFind(int v) : data(v, -1) {} int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -data[root(x)]; } void merge(int x, int y) { x = root(x), y = root(y); if (x != y) { if (size(y) > size(x)) swap(x, y); data[x] += data[y]; data[y] = x; } } }; int main() { int n, m; cin >> n >> m; UnionFind uni(n+1); rep(i, m){ int a, b; cin >> a >> b; uni.merge(a, b); } set<int> st; REP(i, 1, n+1){ st.insert(uni.root(i)); } cout << st.size() - 1 << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | B - 道路工事 |
User | letter |
Language | C++14 (GCC 5.4.1) |
Score | 100 |
Code Size | 998 Byte |
Status | AC |
Exec Time | 63 ms |
Memory | 7424 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 | 43 ms | 256 KB |
16.txt | AC | 21 ms | 5376 KB |
17.txt | AC | 21 ms | 5376 KB |
18.txt | AC | 21 ms | 7424 KB |
19.txt | AC | 63 ms | 1408 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 |