-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirus.cpp
More file actions
78 lines (52 loc) · 1.03 KB
/
Copy pathvirus.cpp
File metadata and controls
78 lines (52 loc) · 1.03 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
75
76
77
78
// Data_structure.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stdio.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int **make2array(int i) {
int **arr;
arr = (int**)calloc(i, sizeof(int *));
for (int j = 0; j < i; j++) {
arr[j] = (int*)calloc(i, sizeof(int));
}
return arr;
}
int main()
{
queue<int> Q;
int number, n, a,b;
scanf("%d", &number);
scanf("%d", &n);
int **arr;
int *boolarr;
arr = make2array(number);
boolarr = (int*)calloc(number,sizeof(int));
for (int k = 0; k < n; k++) {
scanf("%d %d", &a, &b);
arr[a-1][b-1] = 1;
arr[b - 1][a - 1] = 1;
}
int initial= 0;
int cnt = 0;
Q.push(initial);
boolarr[0] = 1;
while (Q.size()!=0) {
for (int i = 0; i < number; i++) {
if (arr[initial][i] == 1 && boolarr[i]!=1)
{
Q.push(i);
boolarr[i] = 1;
cnt++;
}
}
Q.pop();
if (Q.size() != 0) {
initial = Q.front();
}
}
printf("%d", cnt);
return 0;
}