-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeWeighting.cpp
112 lines (97 loc) · 2.47 KB
/
TreeWeighting.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef map<pair<int, int>, int> dataMapType;
typedef map<pair<int, int>, int>::iterator mapIter;
typedef set<int>::iterator setIter;
void printMap(dataMapType mdataMap) {
cout << "---------" << endl;
for(mapIter iter=mdataMap.begin();iter!=mdataMap.end();iter++) {
cout << (iter->first).first << " " << (iter->first).second << " "
<< iter->second << endl;
}
}
void printSet(set<int> mset) {
cout << "elements" << endl;
for(setIter iter=mset.begin();iter!=mset.end();iter++) {
cout << *iter << " ";
}
cout << endl;
}
void fillWeightMap(dataMapType &mWeightMap, int N) {
// cout << "fillWeightMap, N=" << N << endl;
for(int i=1;i<=N;i++) {
for(int j=i+1;j<=N;j++) {
pair<int, int> dataPair(i, j);
mWeightMap[dataPair]= -1;
}
}
}
void fill(dataMapType &mWeightMap, set<int> mset) {
cout << "fill: ";
printSet(mset);
if (mset.size()<=2) return;
else {
for(setIter iter=mset.begin();iter!=mset.end();iter++) {
cout << *iter << " ";
mset.erase(iter);
fill(mWeightMap, mset);
}
cout << endl;
}
}
/*
void fill(dataMapType &mWeightMap, set<int> mset) {
for(setIter iter=mset.begin();iter!=mset.end();iter++) {
for(setIter iter2=iter;iter2!=mset.end();iter2++) {
if(*iter != *iter2) {
pair<int, int> dataPair(*iter, *iter2);
// if(mWeightMap[dataPair]!= -1) {}
cout << "fill " << *iter << " " << *iter2 << " " << mWeightMap[dataPair] << endl;
}
}
}
}
*/
void algorithm(dataMapType &mWeightMap, dataMapType mdataMap) {
set<int> mset;
for(mapIter iter=mdataMap.begin();iter!=mdataMap.end();iter++) {
mWeightMap[iter->first]=iter->second;
mset.insert(iter->first.first);
mset.insert(iter->first.second);
printSet(mset);
if (mset.size()>2) fill(mWeightMap, mset);
}
}
int main(int argc, char** argv)
{
dataMapType dataMap;
dataMapType weightMap;
int tc, T, N, A, B, D, help;
freopen("sample_input.txt", "r", stdin);
cin >> T;
for(tc = 0; tc < 1; tc++)
{
cin >> N;
for(int i=0;i<N-1;i++) {
cin >> A;
cin >> B;
if(B<A) {
help=A;
A=B;
B=help;
}
pair<int, int> dataPair(A, B);
cin >> D;
dataMap[dataPair]=D;
}
printMap(dataMap);
fillWeightMap(weightMap, N);
printMap(weightMap);
algorithm(weightMap, dataMap);
printMap(weightMap);
}
return 0;
}