3804 Query on a tree

Source: Internet
Author: User

The question was so painful that I didn't want to write it again.

 

The idea of the question is very simple. Find the maximum edge weight from node 1 to a node x path not greater than Y

 

Offline operations: mutiset stores negative Edge Weight Values. lower_bound can be used to conveniently obtain the maximum value not greater than Y in the set.

The disgusting part of the question is that you cannot use recursive deep search. You can only write non-recursive versions. The first time you write a non-recursive deep search, you have been writing it for a long time, but a is very happy.

 

# Include <cstdio> <br/> # include <set> <br/> # include <vector> <br/> # include <map> <br/> using namespace STD; <br/> # define maxn 100001 <br/> struct node {<br/> int V, W; // v ID of the next node, W edge weight <br/> node () {}< br/> node (int av, int AW): V (AV), w (AW) {}< br/>}; </P> <p> struct quenode {// because it is an offline operation, therefore, this struct is required to record the output sequence <br/> int V; <br/> int bound; <br/> }; <br/> struct dpstype {// struct used in non-recursive Deep Search <br/> int V, CID; <br/> int insw; <B R/>}; <br/> vector <node> adj [maxn]; <br/> Map <int, int> qmp [maxn]; <br/> vector <quenode> qseq; <br/> Multiset <int> MS; <br/> int vis [maxn]; <br/> int t, n, m; </P> <p> dpstype vsta [maxn]; <br/> void solve () {<br/> int DPT = 0; <br/> vsta [DPT]. cid =-1; <br/> vsta [DPT]. V = 1; <br/> vis [vsta [DPT]. v] = 1; <br/> while (DPT! =-1) {<br/> const dpstype cNode = vsta [DPT]; <br/> If (cNode. CID + 1 <adj [cNode. v]. size () {// if this layer does not exceed the boundary, continue the query. <br/> vsta [DPT]. cid = cNode. CID + 1; <br/> int Cu = cNode. v; <br/> int NV = adj [Cu] [vsta [DPT]. CID]. v; <br/> If (! Vis [NV]) {<br/> int insval =-(adj [Cu] [vsta [DPT]. CID]. w); <br/> MS. insert (insval); <br/> ++ DPT; <br/> vsta [DPT]. V = NV; <br/> vsta [DPT]. insw = insval; <br/> vis [NV] = 1; <br/> for (Map <int, int>: iterator iter = qmp [NV]. begin (); iter! = Qmp [NV]. end (); ++ ITER) {<br/> Multiset <int >:: iterator rs = Ms. lower_bound (-(ITER-> first); <br/> If (RS! = Ms. end () {<br/> ITER-> second =-(* RS ); <br/>}< br/>} else {// beyond the boundary, backtracking <br/> vsta [DPT]. cid =-1; <br/> Multiset <int>: iterator EPOS = Ms. find (vsta [DPT]. insw); <br/> If (EPOs! = Ms. end () <br/> MS. erase (EPOs); <br/> -- DPT; <br/>}< br/> void INI (int n) {<br/> MS. clear (); <br/> qseq. clear (); <br/> for (INT I = 0; I <= N; ++ I) {<br/> adj [I]. clear (); <br/> qmp [I]. clear (); <br/> vsta [I]. cid =-1; <br/> vis [I] = 0; <br/>}< br/> int main () {<br/> scanf ("% d", & T); <br/> while (t --) {<br/> scanf ("% d ", & N); <br/> INI (n); <br/> for (INT I = 1; I <= N; ++ I) adj [I]. clear (); <br/> for (INT I = 0; I <n-1; ++ I) {<br/> int U, V, W; <br/> scanf ("% d", & U, & V, & W); <br/> adj [u]. push_back (node (V, W); <br/> adj [v]. push_back (node (u, W); <br/>}< br/> scanf ("% d", & M); <br/> quenode on; <br/> for (INT I = 0; I <m; ++ I) {<br/> int V, B; <br/> scanf ("% d", & V, & B); <br/> On. V = V; <br/> On. bound = B; <br/> qseq. push_back (on); <br/> qmp [v]. insert (pair <int, int> (B,-1); <br/>}< br/> solve (); <br/> for (size_t I = 0; I <qseq. size (); ++ I) {<br/> quenode TMP = qseq [I]; <br/> printf ("% d/N", qmp [TMP. v]. find (TMP. bound)-> second); <br/>}< br/> return 0; <br/>}< br/>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.