玲珑杯-Round #7.B-1072-Capture

                                         1072 - Capture

                                          

Time Limit:15s Memory Limit:1024MByte

Submissions:396Solved:133

DESCRIPTION

In a city capture game, Mr. Van owns a Empire with a capital city ( marked 1 ). Initially, Mr. Van's Empire had the capital only. All other cities were captured by him in the game and each next captured city was marked the next natural number (2, 3, ...).

In order to make his Empire connected, each newly captured city was connected by a road to exactly one of the cities of his Empire at the moment of capture.

Some of the cities were disconnecting from his Empire and were never captured again. In order to disconnect from the empire, enemies destroy the road that was built to connect them with the Empire. Some other cities also may become disconnected from the Empire at that moment and the Empire loses them too.

To protect his cities more effectively, Mr.Van reconstructed the whole history of his Empire and represented it as a sequence of events: captures and disconnections. After each event he needs to know the number of the city which is the most remote from his capital. The distance from one city to another is measured as the minimal amount of roads that one need to pass in order to get from one city to another. If there are several most remote cities, he take the smallest number one.

Given the history of his Empire, your job is to tell which city is the most remote after each event.

INPUT
The first line is a single integer TT, indicating the number of test cases.For each test case:
The first line of the input contains the integer number N (1N100000)N (1≤N≤100000) number of historical events. The description of events in chronological order follows starting from the second line. Each event is written in a single line. If the event is 'capture', then it's written as "+V""+V" where V is the number of the city in the Empire that new city got connected to (the new city marks next integer number in the sequence of city numbers). Disconnections are written as "V""−V" which means that the city V is disconnected from the Empire.
All input data is correct, only a city that is connected to the Empire can be disconnected, new cities will be connected only to the ones that belong to the Empire, the capital is never disconnected.
OUTPUT
For each event from the input file, print the city's marked number which is the most remote from the capital. In case several solutions exist, output the minimal one.
SAMPLE INPUT
15+1+2+1-2+4
SAMPLE OUTPUT
23345

思路:+v 代表 在编号为v的城市后加一个城市,-v 代表 将标号为v的城市去掉,用维护一个set容器,模拟即可。

#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
#define N 100005
struct no
{
   int num,deep;                                  //deep 深度(即到Capital的距离)
   bool friend operator <(no a,no b){             
   if(a.deep == b.deep) return a.num < b.num;     //deep 深度优先  ,num代表城市编号
   return a.deep > b.deep;
   }
}now;
set<no>se;  
vector<int>v[N];  
int has[N];      //是否已经删除
int d[N];        //记录城市的深度(即到Capital的距离)
void  dfs(int n)
{
      has[n]=1;  
      now.num= n;
      now.deep=d[n];
      se.erase(now);
      int len = v[n].size();
      for(int i=0;i<len;i++){
         if(has[v[n][i]]) continue;
         dfs(v[n][i]);
      }
}
int main()
{
   int t;
   scan(t);
   set<no>::iterator it;
   while(t--){
      d[1]=0;           //Capital的深度为0
      se.clear();       //注意清空set 容器
      int n;       
      mst(has,0);  
      scan(n);
      now.deep=0;
      now.num =1;
      se.insert(now);
      int cot = 2;      //新加入的城市编号从2开始递增
      for(int i=0;i<n;i++){
         v[i].clear();  //注意要清空 vector 容器
      }
      REP(i,0,n){
      char sh[20];
      scanf("%s",sh);
      int num=0;
      int len = strlen(sh);
      REP(j,1,len){
      num = num*10+sh[j]-'0';
      }
      if(has[num]==0){  //如果未删除
      if(sh[0]=='+'){   //新增
      d[cot++]=d[num]+1;
      v[num].push_back(cot-1);
      now.num=cot-1;
      now.deep=d[cot-1];
      se.insert(now);
      }else{             //删去
      dfs(num);
      }
      }
   /*for(it=se.begin();it!=se.end();it++){
      cout<<it->num<<" "<<it->deep<<endl;
   }*/
      it = se.begin();
      cout<<it->num<<endl;
      }
   }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值