“玲瓏杯”ACM比賽 Round #7 B -- Capture(並查集+優先隊列)

來源:互聯網
上載者:User

標籤:set   ons   輸入   cto   stack   eps   class   node   char   

題意:初始時有個首都1,有n個操作

+V表示有一個新的城市串連到了V號城市

-V表示V號城市斷開了串連,同時V的子城市也會中斷連線

每次輸出在每次操作後到首都1距離最遠的城市編號,多個距離相同輸出編號最小的城市

輸入資料保證正確,每次添加與刪除的城市一定是與首都相連的

 

題解:每次都只需要知道最遠且編號最小的城市,所以直接使用優先佇列儲存體

如果是+V就使用並查集(不能路徑壓縮)添加上然後加入優先隊列,接著直接彈出首元素就是結果

如果是-V則把V指向0,接著彈出優先隊列的第一個元素

如果他與1相連就是答案,否則將他到0這條路上的點都連上0刪除他,繼續彈出

注意這兒有個trick就是如果沒有城市與1相連,答案就是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 namespace std;#define eps 1E-8/*注意可能會有輸出-0.000*/#define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x為兩個浮點數差的比較,注意返回整型#define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮點數轉化#define zero(x) (((x)>0?(x):-(x))<eps)//判斷是否等於0#define mul(a,b) (a<<b)#define dir(a,b) (a>>b)typedef long long ll;typedef unsigned long long ull;const int Inf=1<<28;const ll INF=1ll<<60;const double Pi=acos(-1.0);const int Mod=1e9+7;const int Max=2e5+7;struct node{    int key,value;//存編號與離1的距離    bool operator<(const node &c)const//距離大的編號小的優先    {        if(value==c.value)            return key>c.key;        return value<c.value;    }};priority_queue<struct node> ans;int fat[Max],val[Max];char str[Max];void Init(int n){    while(!ans.empty())        ans.pop();        node temp;//初始化一個元素1在隊列底部        temp.value=0;        temp.key=1;        ans.push(temp);    for(int i=0;i<=n;++i)    {        fat[i]=i;        val[i]=0;    }    return;}int Find(int x){    if(x==fat[x])    return fat[x];    return Find(fat[x]);}void Solveadd(int f,int s)//+{    node temp;    fat[s]=f;    val[s]=val[f]+1;    temp.key=s;    temp.value=val[s];    ans.push(temp);    return;}void Solvesub(int f)//-{    node temp;    int s;    fat[f]=0;    while(!ans.empty())    {        temp=ans.top();        if(Find(temp.key)==1)            return;            s=temp.key;        while(fat[s]!=0)//鏈上賦為0        {            fat[s]=0;        }        ans.pop();    }    return;}int main(){int t,n,m,coun;scanf("%d",&t);node temp;while(t--){    scanf("%d",&n);    Init(n+1);    coun=1;    for(int i=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;}

 

“玲瓏杯”ACM比賽 Round #7 B -- Capture(並查集+優先隊列)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.