Title: Click here
Test instructions
There is a very grumpy person who wants to travel by car n a city. Connecting the city is a total of M-road (bidirectional). He was uncomfortable sitting in the car, with a maximum of X minutes at a time. But at every stop he can rest (re-timer). There are a total of Q queries. Ask how many roads there are on the way he can not erupt. A to B and B to a are different roads. A and B must be different.
Analysis:
and check Set
1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 Const intM = 2e4+5;7 8 structEdge {//Save node Information9 int from, to, cost;Ten BOOL operator< (ConstEdge x)Const{returnCost <X.cost;} One} es[m*5]; A - structQuery {//Save problem Information - intVal, id; the BOOL operator< (ConstQuery x)Const{returnVal <X.val;}; -} que[5005]; - - intN, M, q; + intPRE[M];//and check Set - intANS[M];//Answer + intSUM[M];//record and check the height of the set tree A at intFindintx) { - returnPRE[X] = = x? X:PRE[X] =find (Pre[x]); - } - voidMergeintXinty) { -x = Find (x); y =find (y); - if(Sum[x] > Sum[y]) {Sum[x] + = Sum[y]; Pre[y] =x;} in Else{Sum[y] + = sum[x]; Pre[x] =y;} - } to voidsolve () { +scanf"%d%d%d", &n, &m, &q); - for(intI=0; i<m; i++ ) thescanf"%d%d%d", &es[i]. from, &es[i].to, &es[i].cost); *Sort (es, es+m); $ for(intI=0; i<q; i++ ) {Panax Notoginsengscanf"%d", &que[i].val); -Que[i].id =i; the } +Sort (que, que+q); A for(intI=0; i<=n; i++) {Pre[i] = i; sum[i] =1; } the intj =0; + intres =0; - for(intI=0; i<q; i++ ) { $ for(; j<m && que[i].val>=es[j].cost; J + + ) { $ intx = Find (Es[j]. from ); - inty =find (es[j].to); - if(x = = y)Continue; theRes + = sum[x] *Sum[y]; -Merge (Es[j]. from, es[j].to);Wuyi } theAns[que[i].id] =Res; - } Wu for(intI=0; i<q; i++ ) -printf"%d\n", ans[i]<<1 ); About } $ - intMain () { - intT scanf"%d", &t); - while(t-- ) { A solve (); + } the - return 0; $}
HDU OJ 5441 Travel 2015online E