"Linglong Cup" ACM competition Round #7 B--Capture (and check + priority queue)

Source: Internet
Author: User

Test instructions: Initially there was a capital of 1, with n operations

+v says there's a new city connected to the city of V,

-V means the city of V is disconnected and the sub-city of V is disconnected

Number of cities with the lowest number of outputs with the same output numbers per output after each operation to the capital 1 farthest from the city

The input data is guaranteed to be correct, each time the city added and deleted must be connected to the capital.

Puzzle: Only need to know the furthest and least numbered cities each time, so use priority queue storage directly

If it is +v use and check set (not path compression) to add and then join the priority queue, and then directly pop the first element is the result

If it is-V Point v to 0, then the first element of the priority queue pops up

If he is connected with 1 is the answer, otherwise will he to 0 this road point all connected 0 delete him, continue to pop

Note that there is a trick that if no city is connected to 1, the answer is 1.

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<iomanip>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCVS (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Constll inf=1ll<< -;Const DoublePi=acos (-1.0);Const intmod=1e9+7;Const intmax=2e5+7;structnode{intKey,value;//storage number and distance from 1    BOOL operator< (ConstNode &c)Const//The distance of the large number of small priority    {        if(value==c.value)returnKey>C.key; returnvalue<C.value; }};p Riority_queue<structNode>ans;intFat[max],val[max];CharStr[max];voidInit (intN) {     while(!ans.empty ()) Ans.pop (); node temp;//Initializes an element 1 at the bottom of the queueTemp.value=0; Temp.key=1;    Ans.push (temp);  for(intI=0; i<=n;++i) {Fat[i]=i; Val[i]=0; }    return;}intFind (intx) {    if(x==Fat[x])returnFat[x]; returnFind (fat[x]);}voidSolveadd (intFintS//+{Node temp; Fat[s]=F; Val[s]=val[f]+1; Temp.key=s; Temp.value=Val[s];    Ans.push (temp); return;}voidSolvesub (intF//-{Node temp; ints; FAT[F]=0;  while(!Ans.empty ()) {Temp=Ans.top (); if(Find (temp.key) = =1)            return; S=Temp.key;  while(fat[s]!=0)//The chain is assigned 0.{Fat[s]=0;    } ans.pop (); }    return;}intMain () {intt,n,m,coun;scanf ("%d",&t); node temp; while(t--) {scanf ("%d",&N); Init (n+1); Coun=1;  for(intI=0; i<n;++i) {GetChar (); scanf ("%c%d",&str[i],&m); if(str[i]=='+') {Solveadd (M,++Coun); }        Else{solvesub (M); } temp=Ans.top (); printf ("%d\n", Temp.key); }}return 0;}

"Linglong Cup" ACM competition Round #7 B--Capture (and check + priority queue)

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.