#include <bits/stdc++.h> using namespace std; #define LL long long #define DBG(X) struct Edge { int u,v; LL cash; Edge(int u, int v, int cash) : u(u), v(v), cash(cash) { } string asString() { return "(" + to_string(u) + "," + to_string(v) + ")@" + to_string(cash); } }; struct Taxman { int path[5]; int mmax, mmin; LL cash; int reverseId; Taxman() { for (int i=0; i < 5; i++) path[i] = 0; cash = 0; } Taxman(int _path[5], int _cash) { for (int i=0; i < 5; i++) path[i] = _path[i]; cash = _cash; pre(); } void pre() { mmax = -1; mmin = 1000000000; for (int i=0; i < 5;i++) { if (path[i] > mmax) mmax = path[i]; if (path[i] < mmin) mmin = path[i]; } } string asString() const { string res = "T" + to_string(reverseId) + "("; for (int i=0; i < 5; i++) { if (i) res += ","; res += to_string(path[i]+1); } res += ")@" + to_string(cash); return res; } bool independentOf(const Taxman &b) const { //printf("Testing if %s is independentOf taxman %s -> ", this->asString().c_str(), b.asString().c_str()); bool res = false; if (mmax < b.mmin) { //printf("yes\n"); return true; } if (b.mmax < mmin) { //printf("yes\n"); return true; } for (int i=1; i < 5; i++) { for (int j=1; j < 5; j++) { if (path[i] == b.path[j] && path[i-1] == b.path[j-1]) { //printf("no\n"); return false; } } } //printf("yes\n"); return true; } }; struct BajtogradTree { const int n; const vector<vector<pair<int, Edge*> > > adj; vector<bool> visited; BajtogradTree(vector<vector<pair<int, Edge*> > > adj) : adj(adj), n(adj.size()), visited(n) { } void shortDfs(int u, int level, int *path, LL cash, vector<Taxman> &ts) { //printf("shortDfs u=%d, level=%d, cash=%lld\n", u, level, cash); path[level] = u; visited[u] = true; if (level == 4) { if (cash>0) ts.push_back(Taxman(path, cash)); visited[u] = false; return; } for (int j=0; j < adj[u].size(); j++) { pair<int, Edge*> pr = adj[u][j]; if (visited[pr.first]) continue; int v = pr.first; Edge* edge = adj[u][j].second; shortDfs(v, level+1, path, cash + edge->cash, ts); } visited[u] = false; } vector<Taxman> allPossibleTaxmans() { vector<Taxman> res; for (int u=0; u < n; u++) { int path[5] = {-1,-1,-1,-1,-1}; path[0] = u; visited[u] = true; for (int j=0; j < adj[u].size(); j++) { int v = adj[u][j].first; Edge* edge = adj[u][j].second; shortDfs(v, 1, path, edge->cash, res); } visited[u] = false; } sort(res.begin(), res.end(), [](const Taxman & t1, const Taxman & t2) { return t1.cash > t2.cash; }); for (int i=0; i < res.size(); i++) res[i].reverseId = i; return res; } }; struct HMRCNetwork { const int n; const vector<Taxman> &ts; vector<vector<int> > adj; vector<bool> visited; struct HashFunction { size_t operator()(const pair<int,int>& p) const { //return std::hash<int>(p.first) * 101161 + std::hash<int>(p.second); /*size_t hash; int a1 = 101161, a2 = 197; int b = 100999; return (a1 * p.first + a2*p.second + b);*/ auto hash1 = hash<int>{}(p.first); auto hash2 = hash<int>{}(p.second); return hash1 ^ hash2; } }; unordered_map<pair<int,int>, int, HashFunction> visitedEdges; HMRCNetwork(const vector<Taxman> &ts) : n(ts.size()), ts(ts), adj(n), visited(n) { } bool processAllOriginalEdges(const Taxman &t, bool add, bool check, bool remove) { for (int i=0; i < 4; i++) { int b = t.path[i]; int e = t.path[i+1]; if (b > e) swap(b,e); pair<int, int> p(b,e); if (add) { visitedEdges[p] = 1; } if (check) { if (visitedEdges.find(p) != visitedEdges.end()) { return true; } } if (remove) { if (visitedEdges.find(p) == visitedEdges.end()) { printf("error at hashmap\n"); fflush(stdout); } visitedEdges.erase(visitedEdges.find(p)); } } return false; } void deepGo(int u, vector<LL> &dp, LL cash) { DBG(printf("deepGo->%d, cash=%lld, taxman=%s\n", u, cash, ts[u].asString().c_str())); visited[u] = true; dp[u] = max(dp[u], cash); processAllOriginalEdges(ts[u], true, false, false); for (int v=u+1; v < n; v++) { if (visited[v]) continue; bool shouldSkip = processAllOriginalEdges(ts[v], false, true, false);; if (shouldSkip) continue; deepGo(v, dp, cash + ts[v].cash); } visited[u] = false; processAllOriginalEdges(ts[u], false, false, true); } LL findLongestPath() { vector<LL> dp(n,0); for (int u=0; u < n; u++) { DBG(printf("brand new dfs at %d\n", u)); deepGo(u, dp, ts[u].cash); } LL ans = 0; for (int u=0; u < n; u++) { ans = max(ans, dp[u]); } return ans; } }; int main() { int n; scanf("%d", &n); vector<vector<pair<int, Edge*> > > adj(n); for (int i=0; i < n-1; i++) { int ai,bi,cash; scanf("%d%d%d", &ai,&bi,&cash); --ai; --bi; Edge* e = new Edge(ai,bi,cash); adj[ai].push_back(make_pair(bi, e)); adj[bi].push_back(make_pair(ai, e)); } BajtogradTree tree(adj); vector<Taxman> ts = tree.allPossibleTaxmans(); /*for (Taxman &t : ts) { printf("%s\n", t.asString().c_str()); }*/ HMRCNetwork hmrc(ts); printf("%lld\n", hmrc.findLongestPath()); 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #include <bits/stdc++.h> using namespace std; #define LL long long #define DBG(X) struct Edge { int u,v; LL cash; Edge(int u, int v, int cash) : u(u), v(v), cash(cash) { } string asString() { return "(" + to_string(u) + "," + to_string(v) + ")@" + to_string(cash); } }; struct Taxman { int path[5]; int mmax, mmin; LL cash; int reverseId; Taxman() { for (int i=0; i < 5; i++) path[i] = 0; cash = 0; } Taxman(int _path[5], int _cash) { for (int i=0; i < 5; i++) path[i] = _path[i]; cash = _cash; pre(); } void pre() { mmax = -1; mmin = 1000000000; for (int i=0; i < 5;i++) { if (path[i] > mmax) mmax = path[i]; if (path[i] < mmin) mmin = path[i]; } } string asString() const { string res = "T" + to_string(reverseId) + "("; for (int i=0; i < 5; i++) { if (i) res += ","; res += to_string(path[i]+1); } res += ")@" + to_string(cash); return res; } bool independentOf(const Taxman &b) const { //printf("Testing if %s is independentOf taxman %s -> ", this->asString().c_str(), b.asString().c_str()); bool res = false; if (mmax < b.mmin) { //printf("yes\n"); return true; } if (b.mmax < mmin) { //printf("yes\n"); return true; } for (int i=1; i < 5; i++) { for (int j=1; j < 5; j++) { if (path[i] == b.path[j] && path[i-1] == b.path[j-1]) { //printf("no\n"); return false; } } } //printf("yes\n"); return true; } }; struct BajtogradTree { const int n; const vector<vector<pair<int, Edge*> > > adj; vector<bool> visited; BajtogradTree(vector<vector<pair<int, Edge*> > > adj) : adj(adj), n(adj.size()), visited(n) { } void shortDfs(int u, int level, int *path, LL cash, vector<Taxman> &ts) { //printf("shortDfs u=%d, level=%d, cash=%lld\n", u, level, cash); path[level] = u; visited[u] = true; if (level == 4) { if (cash>0) ts.push_back(Taxman(path, cash)); visited[u] = false; return; } for (int j=0; j < adj[u].size(); j++) { pair<int, Edge*> pr = adj[u][j]; if (visited[pr.first]) continue; int v = pr.first; Edge* edge = adj[u][j].second; shortDfs(v, level+1, path, cash + edge->cash, ts); } visited[u] = false; } vector<Taxman> allPossibleTaxmans() { vector<Taxman> res; for (int u=0; u < n; u++) { int path[5] = {-1,-1,-1,-1,-1}; path[0] = u; visited[u] = true; for (int j=0; j < adj[u].size(); j++) { int v = adj[u][j].first; Edge* edge = adj[u][j].second; shortDfs(v, 1, path, edge->cash, res); } visited[u] = false; } sort(res.begin(), res.end(), [](const Taxman & t1, const Taxman & t2) { return t1.cash > t2.cash; }); for (int i=0; i < res.size(); i++) res[i].reverseId = i; return res; } }; struct HMRCNetwork { const int n; const vector<Taxman> &ts; vector<vector<int> > adj; vector<bool> visited; struct HashFunction { size_t operator()(const pair<int,int>& p) const { //return std::hash<int>(p.first) * 101161 + std::hash<int>(p.second); /*size_t hash; int a1 = 101161, a2 = 197; int b = 100999; return (a1 * p.first + a2*p.second + b);*/ auto hash1 = hash<int>{}(p.first); auto hash2 = hash<int>{}(p.second); return hash1 ^ hash2; } }; unordered_map<pair<int,int>, int, HashFunction> visitedEdges; HMRCNetwork(const vector<Taxman> &ts) : n(ts.size()), ts(ts), adj(n), visited(n) { } bool processAllOriginalEdges(const Taxman &t, bool add, bool check, bool remove) { for (int i=0; i < 4; i++) { int b = t.path[i]; int e = t.path[i+1]; if (b > e) swap(b,e); pair<int, int> p(b,e); if (add) { visitedEdges[p] = 1; } if (check) { if (visitedEdges.find(p) != visitedEdges.end()) { return true; } } if (remove) { if (visitedEdges.find(p) == visitedEdges.end()) { printf("error at hashmap\n"); fflush(stdout); } visitedEdges.erase(visitedEdges.find(p)); } } return false; } void deepGo(int u, vector<LL> &dp, LL cash) { DBG(printf("deepGo->%d, cash=%lld, taxman=%s\n", u, cash, ts[u].asString().c_str())); visited[u] = true; dp[u] = max(dp[u], cash); processAllOriginalEdges(ts[u], true, false, false); for (int v=u+1; v < n; v++) { if (visited[v]) continue; bool shouldSkip = processAllOriginalEdges(ts[v], false, true, false);; if (shouldSkip) continue; deepGo(v, dp, cash + ts[v].cash); } visited[u] = false; processAllOriginalEdges(ts[u], false, false, true); } LL findLongestPath() { vector<LL> dp(n,0); for (int u=0; u < n; u++) { DBG(printf("brand new dfs at %d\n", u)); deepGo(u, dp, ts[u].cash); } LL ans = 0; for (int u=0; u < n; u++) { ans = max(ans, dp[u]); } return ans; } }; int main() { int n; scanf("%d", &n); vector<vector<pair<int, Edge*> > > adj(n); for (int i=0; i < n-1; i++) { int ai,bi,cash; scanf("%d%d%d", &ai,&bi,&cash); --ai; --bi; Edge* e = new Edge(ai,bi,cash); adj[ai].push_back(make_pair(bi, e)); adj[bi].push_back(make_pair(ai, e)); } BajtogradTree tree(adj); vector<Taxman> ts = tree.allPossibleTaxmans(); /*for (Taxman &t : ts) { printf("%s\n", t.asString().c_str()); }*/ HMRCNetwork hmrc(ts); printf("%lld\n", hmrc.findLongestPath()); return 0; } |