-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday79.cpp
More file actions
74 lines (60 loc) · 1.56 KB
/
day79.cpp
File metadata and controls
74 lines (60 loc) · 1.56 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
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <set>
#include <queue>
using namespace std;
vector<set<int>> adj; // Adjacency list using sets for quick lookup
vector<bool> visited;
// Perform BFS to get the connected component
vector<int> getComponent(int start) {
vector<int> component;
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
component.push_back(node);
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
return component;
}
// Check if a component forms a complete group
bool isCompleteGroup(const vector<int>& component) {
int size = component.size();
int expectedEdges = (size * (size - 1)) / 2;
int actualEdges = 0;
for (int node : component) {
actualEdges += adj[node].size();
}
actualEdges /= 2; // Each edge was counted twice
return actualEdges == expectedEdges;
}
int main() {
int m, n;
cin >> m >> n;
adj.resize(n);
visited.assign(n, false);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].insert(v);
adj[v].insert(u);
}
int completeGroups = 0;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
vector<int> component = getComponent(i);
if (isCompleteGroup(component)) {
completeGroups++;
}
}
}
cout << completeGroups << endl;
return 0;
}