-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj1.cpp
93 lines (83 loc) · 2.13 KB
/
proj1.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
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio> /* printf e scanf */
using namespace std;
class Node{
private:
int value;
Node* next;
public:
Node(int v, Node* n): value(v), next(n){ }
int getValue() const{ return value; }
Node* getNext() const{ return next; }
~Node(){ delete next; }
};
class Graph{
private:
int nVertices;
vector<int> distances;
vector<Node*> adjacencyList;
int getMaxErdosNumber() const{
int max=0;
for (int i = 0; i < nVertices; i++){
if(distances[i] > max)
max = distances[i];
}
return max;
}
public:
Graph(int Vertices): nVertices(Vertices), distances(Vertices), adjacencyList(Vertices){ }
int getVertices() const{ return nVertices; }
vector<Node*> getAdjacencyList() const{ return adjacencyList; }
vector<int> getDistances() const{ return distances; }
void insertEdge(int v, int w){
adjacencyList[v-1] = new Node(w, adjacencyList[v-1]);
adjacencyList[w-1] = new Node(v, adjacencyList[w-1]);
}
void BreadthFirstSearch(int s){
int w,v;
queue<int> Q;
Node* aux;
Q.push(s);
distances[s-1] = 0;
while(!Q.empty()){
v = Q.front();
Q.pop();
aux = adjacencyList[v-1];
while(aux){ /* Vamos iterar a lista de adjacências do vértice v */
w = aux->getValue();
if(!distances[w-1] && w != s){ /* Os tempos de descoberta servem como flag. Sendo s o vértice por onde começamos, sabemos que já foi visitado */
distances[w-1] = distances[v-1] + 1;
Q.push(w);
}
aux = aux->getNext();
}
}
}
void displayQuantities(){
int M = getMaxErdosNumber();
vector<int> quantities(M);
printf("%d\n", M);
for(int i = 0; i < nVertices; i++){
if(distances[i] != 0) /* Para não aceder ao indice -1 */
quantities[distances[i]-1] ++ ;
}
for(int i = 0; i < M; i++)
printf("%d\n", quantities[i]);
}
};
int main(){
int N,C,Erdos,u,v;
scanf("%d %d", &N, &C);
scanf("%d", &Erdos);
Graph* G = new Graph(N);
for(int i = 0; i<C; i++){
scanf("%d %d", &u, &v);
G->insertEdge(u,v);
}
G->BreadthFirstSearch(Erdos);
G->displayQuantities();
delete G;
return 0;
}