-
Notifications
You must be signed in to change notification settings - Fork 0
/
120_F.cpp
96 lines (76 loc) · 1.23 KB
/
120_F.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
94
95
96
#include<iostream>
#include<vector>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
int a[101][101];
vector<int> findLeafNodes(int beads)
{
vector<int>leaf;
for(int i=0;i<beads;i++)
{
int count = 0;
for(int j=0;j<beads;j++)
{
if(a[i][j])
count++;
}
if(count==1)
leaf.push_back(i);
}
return leaf;
}
int maxPathHelper(int*c, int nodeN, int beads)
{
int maxm = 0;
c[nodeN]=1;
for(int i=0;i<beads;i++)
{
if(a[nodeN][i] && !c[i])
{
c[i] = 1;
maxm = max(maxm,1 + maxPathHelper(c,i,beads));
}
}
return maxm;
}
int maxPath(int nodeN, int beads)
{
int c[beads];
memset(c,0,sizeof(c));
int maxL = maxPathHelper(c,nodeN,beads);
return maxL;
}
int main()
{
int n;
ifstream fin("input.txt");
fin>>n;
int tot = 0;
for(int i=0;i<n;i++)
{
int beads;
fin>>beads;
memset(a,0,sizeof(a));
for(int j=0;j<beads-1;j++)
{
int t1,t2;
fin>>t1>>t2;
a[t1-1][t2-1] = 1;
a[t2-1][t1-1] = 1;
}
vector<int> leafNodes = findLeafNodes(beads);
int maxm = 0;
for(int j=0;j<(int)leafNodes.size();j++)
{
maxm = max(maxm,maxPath(leafNodes[j],beads));
}
tot +=maxm;
}
fin.close();
ofstream fout("output.txt");
fout<<tot<<endl;
fout.close();
return 0;
}