#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; #define FOR(k,a,b) for(LL k(a); k < (b); ++k) #define FORD(k,a,b) for(int k(a-1); k >= (b); --k) #define REP(k,a) for(int k=0; k < (a); ++k) #define ABS(a) ((a)>0?(a):-(a)) struct MT { int D; int A; int B; bool operator<(const MT& o) const { return D < o.D; } }; typedef vector::iterator MTIT; const int DM = 50002; int TA[200002]; int AA[DM]; int RR[DM]; int BB[DM]; map am; int calc(int A) { while (A != RR[A]) { A = RR[A]; } return A; } void update(int A, int v) { int tmp; while (v != RR[A]) { tmp = RR[A]; RR[A] = v; A = tmp; } } void calc(const MTIT& st, const MTIT& en) { am.clear(); for(MTIT act=st; actA; const int& B = act->B; if(!am.count(A)) { int tmp = am.size(); am[A] = tmp; BB[tmp] = A; RR[tmp] = tmp; } if(!am.count(B)) { int tmp = am.size(); am[B] = tmp; BB[tmp] = B; RR[tmp] = tmp; } int rA =calc( am[A]), rB = calc( am[B]); int mi = min(rA, rB); update(am[A],mi); update(am[B],mi); } REP(i, am.size()) { int tmp = calc(i); update(i,tmp); AA[BB[tmp]]=max(AA[BB[tmp]],AA[BB[i]]); } REP(i, am.size()) { AA[BB[i]]=AA[BB[RR[i]]]; } } int main(int argv, char** argc) { int T,N,M; LL res; scanf("%d",&T); vector mt; assert(00 && TA[i]<=5e4); } mt.clear(); mt.resize(M); REP(i,M) { scanf("%d %d %d",&mt[i].D,&mt[i].A,&mt[i].B); } sort(mt.begin(),mt.end()); reverse(mt.begin(),mt.end()); MTIT st = mt.begin(), en = mt.begin(); while(st!=mt.end()) { while(en != mt.end() && st->D == en->D) { ++en; } calc(st,en); st = en; } res = 0; REP(i,N) res += AA[TA[i]]; printf("%lld\n",res); } return 0; }