-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathduong_di_dai_nhat.cpp
75 lines (61 loc) · 1.79 KB
/
duong_di_dai_nhat.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
#include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<int> > Tree;
const int MAX = 20;
int dem = 0;
/**
Y tuong tao cay : tao 1 vector 2 chieu de luu tru cay voi cac gia tri nhu sau
vt[0][*] : dai dien cho dinh 0, voi vt[0][0] = a => dinh 0 ke voi dinh a
vt[0][1] = b => dinh 0 ke voi dinh b
....
De don gian ta chi xet do thi lien thong, boi vi viec tim cac thanh phan lien thong cua do thi la rat don gian roi =))
*/
/*Danh dau cac canh da duoc di qua*/
bool danhDau[MAX][MAX]; // Vi du : canh {2;3} da di qua => danhDau[2][3] = danhDau[3][2] = true
int Max = 0;
/*Bat dau duyet*/
void duyetCanhDinh(Tree vt, int top)
{
for (int i=0; i<vt[top].size(); i++)
{
int iTop = vt[top][i]; // Dat lai cho gon, tai khong dung duoc for each
if (!danhDau[top][iTop])
{
danhDau[top][iTop] = true;
danhDau[iTop][top] = true;
dem++;
duyetCanhDinh(vt,iTop);
}
}
if (Max < dem) Max = dem;
if (dem > 0) dem--;
return;
}
int main()
{
int N, M;
cin >> N >> M;
Tree vt(N);
// Cac canh ban dau deu chua di qua
for (int i=0; i<MAX; i++)
for (int j=0; j<MAX; j++)
danhDau[i][j] = false;
for (int i=0; i<M; i++)
{
int temp1, temp2;
cin >> temp1 >> temp2;
vt[temp1].push_back(temp2); // Gan dinh temp2 ke voi dinh temp1
vt[temp2].push_back(temp1); // Gan dinh temp1 ke voi dinh temp2
}
for (int i=0; i<vt.size(); i++)
{
cout << i << ". ";
for (int j=0; j<vt[i].size(); j++)
cout << vt[i][j] << " ";
cout << endl;
}
duyetCanhDinh(vt,0);
cout << Max << endl;
return 0;
}