-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRR2012 2.12 LIEN THONG.cpp
More file actions
63 lines (52 loc) · 1.27 KB
/
TRR2012 2.12 LIEN THONG.cpp
File metadata and controls
63 lines (52 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 105;
int n;
int a[MAX][MAX]; // Ma trận kề
bool visited[MAX]; // Mảng đánh dấu đỉnh đã thăm
void bfs(int start, vector<int>& component) {
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
component.push_back(u);
for (int v = 1; v <= n; ++v) {
if (a[u][v] && !visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
}
int main() {
ifstream fin("TK.INP");
ofstream fout("TK.OUT");
fin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
fin >> a[i][j];
vector<vector<int>> components;
for (int i = 1; i <= n; ++i) {
if (!visited[i]) {
vector<int> comp;
bfs(i, comp);
sort(comp.begin(), comp.end());
components.push_back(comp);
}
}
fout << components.size() << endl;
for (const auto& comp : components) {
for (int v : comp)
fout << v << " ";
fout << endl;
}
fin.close();
fout.close();
return 0;
}