Submission #17065226
Source Code Expand
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
int ans = 0;
vector<int> used(n);
function<void(int)> dfs = [&](int v) {
used[v] = 1;
for (int to : g[v])
if (!used[to])
dfs(to);
};
for (int i = 0; i < n; i++) {
if (!used[i]) {
ans++;
dfs(i);
}
}
cout << ans - 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#if defined(_DEBUG)
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
//cin >> q;
for (; q > 0; q--) {
solve();
//cout << endl;
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | C - Connect Cities |
| User | Zombie358 |
| Language | C++ (GCC 9.2.1) |
| Score | 300 |
| Code Size | 990 Byte |
| Status | AC |
| Exec Time | 56 ms |
| Memory | 10236 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 300 / 300 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, example0.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 23 ms | 5076 KiB |
| 001.txt | AC | 19 ms | 4688 KiB |
| 002.txt | AC | 13 ms | 5348 KiB |
| 003.txt | AC | 39 ms | 7528 KiB |
| 004.txt | AC | 41 ms | 7924 KiB |
| 005.txt | AC | 21 ms | 6428 KiB |
| 006.txt | AC | 40 ms | 8980 KiB |
| 007.txt | AC | 6 ms | 5416 KiB |
| 008.txt | AC | 45 ms | 9572 KiB |
| 009.txt | AC | 21 ms | 5708 KiB |
| 010.txt | AC | 47 ms | 10172 KiB |
| 011.txt | AC | 54 ms | 10184 KiB |
| 012.txt | AC | 48 ms | 10152 KiB |
| 013.txt | AC | 49 ms | 10236 KiB |
| 014.txt | AC | 47 ms | 10172 KiB |
| 015.txt | AC | 54 ms | 10164 KiB |
| 016.txt | AC | 56 ms | 10116 KiB |
| 017.txt | AC | 55 ms | 10176 KiB |
| 018.txt | AC | 53 ms | 10036 KiB |
| 019.txt | AC | 51 ms | 10104 KiB |
| example0.txt | AC | 2 ms | 3536 KiB |