-
Notifications
You must be signed in to change notification settings - Fork 0
/
1076.cpp
81 lines (63 loc) · 1.25 KB
/
1076.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#define fr(i, a, c) for (int i = a; i < c; i++)
#define sc1(a) scanf("%lld", &a)
#define sc2(a, b) scanf("%lld %lld", &a, &b)
#define LLI long long int
#define ULL unsigned long int;
using namespace std;
class Graph{
int V;
list<int> *adj;
public:
Graph(int V);
void addAresta(int s, int c);
int BFS(int raiz);
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void Graph::addAresta(int s, int c){
adj[s].push_back(c);
}
int Graph::BFS(int raiz){
int count = 0;
bool *visitado = new bool[V];
for (int i = 0; i < V; ++i) visitado[i] = false;
list<int> fila;
visitado[raiz] = true;
fila.push_back(raiz);
list<int>::iterator i;
while(!fila.empty()){
raiz = fila.front();
fila.pop_front();
for (i = adj[raiz].begin(); i != adj[raiz].end(); ++i)
{
count++;
if(!visitado[*i]){
visitado[*i] = true;
fila.push_back(*i);
}
}
}
return count;
}
int main(int argc, char const *argv[])
{
int n,raiz,vert, arest, s,c;
scanf("%d", &n);
fr(i, 0, n){
scanf("%d", &raiz);
scanf("%d %d", &vert, &arest);
Graph g(vert);
fr(j,0,arest){
scanf("%d %d", &s, &c);
g.addAresta(s, c);
}
printf("%d\n", g.BFS(raiz)*2);
}
return 0;
}