-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathla2817.cpp
81 lines (63 loc) · 1.62 KB
/
la2817.cpp
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
75
76
77
78
79
80
81
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
int N, M;
vector<vector<int> > AdjList;
bool visited[30010];
int dfs(int node) {
visited[node] = true;
int count = 0;
traverse(AdjList[node], it) {
int next = *it;
if(!visited[next]) {
count += dfs(next);
}
}
return 1 + count;
}
int main() {
while(true) {
cin >> N >> M;
if(N == 0 && M == 0) {
break;
}
AdjList.clear();
AdjList.resize(N);
int neighourNumber;
for(int i = 0; i < M; i++) {
cin >> neighourNumber;
vector<int> group;
int next;
for(int j = 0; j < neighourNumber; j++) {
cin >> next;
group.push_back(next);
}
if(group.size() >= 2) {
for(int i = 0; i < group.size() - 1; i++) {
AdjList[group[i]].push_back(group[i+1]);
AdjList[group[i+1]].push_back(group[i]);
}
}
int last = group.size() - 1;
AdjList[group[last]].push_back(group[0]);
AdjList[group[0]].push_back(group[last]);
}
memset(visited, false, sizeof(visited));
cout << dfs(0) << endl;
}
return 0;
}