-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path赛码-认老乡.cpp
50 lines (48 loc) · 1.2 KB
/
赛码-认老乡.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
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
int main() {
int n, m;
while(cin >> n >> m) {
if(n == 0 && m == 0)
break;
vector<vector<int> > vv(n+1);
int flag[n+1];
memset(flag, 0, sizeof(flag));
int a, b;
for(int i=0; i<m; i++) {
cin >> a >> b;
if(a > b) {
a=a^b;
b=a^b;
a=a^b;
}
vv[a].push_back(b);
}
if(vv[1].size() == 0)
cout << 0 << endl;
else {
int sum = 0;
queue<int> que;
que.push(1);
flag[1] = 1;
while(!que.empty()) {
int top = que.front();
que.pop();
if(!vv[top].empty()) {
for(int j=0; j<vv[top].size(); j++) {
if(flag[vv[top][j]] == 0) {
flag[vv[top][j]] = 1;
sum++;
que.push(vv[top][j]);
}
}
}
}
cout << sum << endl;
}
}
return 0;
}