/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<stack> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; LL n; const LL h0=100000000; const LL inf=-h0*h0; class Node { public: LL num; LL parent; LL pass; LL w[4]; vector<LL> nei; vector<LL> wei; Node() {}; void addEdge(LL t, LL z) { nei.push_back(t); wei.push_back(z); }; }; Node nodes[210000]; void read_data() { cin >> n; FOR(i,1,n) nodes[i].num=i; LL u,v,z; FOR(k,1,n-1) { cin >> u; cin >> v; cin >> z; nodes[u].addEdge(v,z); nodes[v].addEdge(u,z); } } LL maxDegreeNode() { LL maxD=0; LL maxN=1; FOR(i,1,n) { LL deg=nodes[i].nei.size(); if (deg>maxD) { maxD=deg; maxN=i; } } return maxN; } void makeTreeFrom(LL root) { FOR(i,1,n) nodes[i].parent=0; queue<LL> toDo; FOREACH(nb,nodes[root].nei) { nodes[*nb].parent=root; toDo.push(*nb); } LL cur; while (!toDo.empty()) { cur=toDo.front(); toDo.pop(); FOREACH(nb,nodes[cur].nei) if ((*nb)!=nodes[cur].parent) { nodes[*nb].parent=cur; toDo.push(*nb); } } } void showTree(LL node, LL level) { FOR(i,1,level) cout << " "; cout << node << "\n"; FOREACH(nb,nodes[node].nei) if (*nb != nodes[node].parent) showTree(*nb, level+1); } bool isLeaf(LL node, LL root) { if (node==root) return nodes[node].nei.size()==0; else return nodes[node].nei.size()==1; } LL suma(LL v1, LL v2) { if ((v1==inf)||(v2==inf)) return inf; return v1+v2; } void calculate_for(LL node, LL child, LL weight) { //cout << "Calculating for node " << node << " and child " << child << " edgeWeight=" << weight << "\n"; LL v[4]; v[0]=max(nodes[child].w[0], suma(nodes[child].w[1],weight)); v[1]=suma(weight, nodes[child].w[2]); v[2]=suma(weight, nodes[child].w[3]); v[3]=suma(weight, nodes[child].w[0]); if (nodes[node].w[0]==inf) { // this is first child FOR(j,0,3) nodes[node].w[j]=v[j]; //cout << "Node " << node << " first child " << child << ": "; //FOR(j,0,3) cout << nodes[node].w[j] << " "; cout << "\n"; } else { // next child //cout << "Node " << node << " next child " << child << "\n"; //cout << " child values: "; FOR(j,0,3) cout << v[j] << " "; cout << "\n"; LL p1=suma(nodes[node].w[0],v[0]); LL p2=suma(nodes[node].w[1],v[3]); LL p3=suma(nodes[node].w[2],v[2]); LL p4=suma(nodes[node].w[3],v[1]); LL m0=p1; if (p2>m0) m0=p2; if (p3>m0) m0=p3; if (p4>m0) m0=p4; p1=suma(nodes[node].w[1],v[0]); p2=suma(nodes[node].w[0],v[1]); LL m1=p1; if (p2>m1) m1=p2; p1=suma(nodes[node].w[2],v[0]); p2=suma(nodes[node].w[0],v[2]); LL m2=p1; if (p2>m2) m2=p2; p1=suma(nodes[node].w[3],v[0]); p2=suma(nodes[node].w[0],v[3]); //cout << " p1=" << p1 << " p2=" << p2 << "\n"; LL m3=p1; if (p2>m3) m3=p2; nodes[node].w[0]=m0; nodes[node].w[1]=m1; nodes[node].w[2]=m2; nodes[node].w[3]=m3; //cout << " calculated values: "; //FOR(j,0,3) cout << nodes[node].w[j] << " "; cout << "\n"; } } void calculate(LL node, LL root) { if (isLeaf(node, root)) { nodes[node].w[0]=0; nodes[node].w[1]=inf; nodes[node].w[2]=inf; nodes[node].w[3]=inf; } else { FOR(i,0,nodes[node].nei.size()-1) if (nodes[node].nei[i]!=nodes[node].parent) calculate_for(node, nodes[node].nei[i], nodes[node].wei[i]); } } void go(LL root) { FOR(i,1,n) { nodes[i].pass=0; FOR(j,0,3) nodes[i].w[j]=inf; }; stack<LL> toDo; toDo.push(root); LL cur; while (!toDo.empty()) { cur=toDo.top(); if (nodes[cur].pass==0) { // first time nodes[cur].pass=1; FOREACH(nb,nodes[cur].nei) if (*nb!=nodes[cur].parent) toDo.push(*nb); } else { // second time toDo.pop(); calculate(cur, root); } } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); read_data(); LL root = maxDegreeNode(); makeTreeFrom(root); //showTree(root,0); go(root); cout << nodes[root].w[0]; return 0; };
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<stack> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; LL n; const LL h0=100000000; const LL inf=-h0*h0; class Node { public: LL num; LL parent; LL pass; LL w[4]; vector<LL> nei; vector<LL> wei; Node() {}; void addEdge(LL t, LL z) { nei.push_back(t); wei.push_back(z); }; }; Node nodes[210000]; void read_data() { cin >> n; FOR(i,1,n) nodes[i].num=i; LL u,v,z; FOR(k,1,n-1) { cin >> u; cin >> v; cin >> z; nodes[u].addEdge(v,z); nodes[v].addEdge(u,z); } } LL maxDegreeNode() { LL maxD=0; LL maxN=1; FOR(i,1,n) { LL deg=nodes[i].nei.size(); if (deg>maxD) { maxD=deg; maxN=i; } } return maxN; } void makeTreeFrom(LL root) { FOR(i,1,n) nodes[i].parent=0; queue<LL> toDo; FOREACH(nb,nodes[root].nei) { nodes[*nb].parent=root; toDo.push(*nb); } LL cur; while (!toDo.empty()) { cur=toDo.front(); toDo.pop(); FOREACH(nb,nodes[cur].nei) if ((*nb)!=nodes[cur].parent) { nodes[*nb].parent=cur; toDo.push(*nb); } } } void showTree(LL node, LL level) { FOR(i,1,level) cout << " "; cout << node << "\n"; FOREACH(nb,nodes[node].nei) if (*nb != nodes[node].parent) showTree(*nb, level+1); } bool isLeaf(LL node, LL root) { if (node==root) return nodes[node].nei.size()==0; else return nodes[node].nei.size()==1; } LL suma(LL v1, LL v2) { if ((v1==inf)||(v2==inf)) return inf; return v1+v2; } void calculate_for(LL node, LL child, LL weight) { //cout << "Calculating for node " << node << " and child " << child << " edgeWeight=" << weight << "\n"; LL v[4]; v[0]=max(nodes[child].w[0], suma(nodes[child].w[1],weight)); v[1]=suma(weight, nodes[child].w[2]); v[2]=suma(weight, nodes[child].w[3]); v[3]=suma(weight, nodes[child].w[0]); if (nodes[node].w[0]==inf) { // this is first child FOR(j,0,3) nodes[node].w[j]=v[j]; //cout << "Node " << node << " first child " << child << ": "; //FOR(j,0,3) cout << nodes[node].w[j] << " "; cout << "\n"; } else { // next child //cout << "Node " << node << " next child " << child << "\n"; //cout << " child values: "; FOR(j,0,3) cout << v[j] << " "; cout << "\n"; LL p1=suma(nodes[node].w[0],v[0]); LL p2=suma(nodes[node].w[1],v[3]); LL p3=suma(nodes[node].w[2],v[2]); LL p4=suma(nodes[node].w[3],v[1]); LL m0=p1; if (p2>m0) m0=p2; if (p3>m0) m0=p3; if (p4>m0) m0=p4; p1=suma(nodes[node].w[1],v[0]); p2=suma(nodes[node].w[0],v[1]); LL m1=p1; if (p2>m1) m1=p2; p1=suma(nodes[node].w[2],v[0]); p2=suma(nodes[node].w[0],v[2]); LL m2=p1; if (p2>m2) m2=p2; p1=suma(nodes[node].w[3],v[0]); p2=suma(nodes[node].w[0],v[3]); //cout << " p1=" << p1 << " p2=" << p2 << "\n"; LL m3=p1; if (p2>m3) m3=p2; nodes[node].w[0]=m0; nodes[node].w[1]=m1; nodes[node].w[2]=m2; nodes[node].w[3]=m3; //cout << " calculated values: "; //FOR(j,0,3) cout << nodes[node].w[j] << " "; cout << "\n"; } } void calculate(LL node, LL root) { if (isLeaf(node, root)) { nodes[node].w[0]=0; nodes[node].w[1]=inf; nodes[node].w[2]=inf; nodes[node].w[3]=inf; } else { FOR(i,0,nodes[node].nei.size()-1) if (nodes[node].nei[i]!=nodes[node].parent) calculate_for(node, nodes[node].nei[i], nodes[node].wei[i]); } } void go(LL root) { FOR(i,1,n) { nodes[i].pass=0; FOR(j,0,3) nodes[i].w[j]=inf; }; stack<LL> toDo; toDo.push(root); LL cur; while (!toDo.empty()) { cur=toDo.top(); if (nodes[cur].pass==0) { // first time nodes[cur].pass=1; FOREACH(nb,nodes[cur].nei) if (*nb!=nodes[cur].parent) toDo.push(*nb); } else { // second time toDo.pop(); calculate(cur, root); } } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); read_data(); LL root = maxDegreeNode(); makeTreeFrom(root); //showTree(root,0); go(root); cout << nodes[root].w[0]; return 0; }; |