***玲珑1072- Capture

来源:互联网 发布:gta5低配优化补丁3dm 编辑:程序博客网 时间:2024/05/21 01:42
1072 - Capture

Time Limit:15s Memory Limit:1024MByte

Submissions:539Solved:178

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
SOLUTION
“玲珑杯”ACM比赛 Round #7
题意:主人公有一个帝国,刚开始只有一个首都,标号为1,随后占领其他的城市,标号陆续为2,3...每次占领一个城市,建立联系,也有的城市会反叛脱离联系。+V表示在城市V后面增加一个城市,-V表示删除城市V,输出距离首都最远的城市,并且字典序最小。(了解了set,vector的用法)
code:
#include<cstdio>#include<vector>#include<cstring>#include<set>#include<algorithm>using namespace std;const int N=100100;vector<int>vec[N];//向量边 set<int>s[N];//集合 int num[N],vis[N];void dfs(int rt){s[num[rt]].erase(rt);//删除距离为num[rt]的集合中的点rtfor(int i=0;i<vec[rt].size();i++){int tmp=vec[rt][i];if(!vis[tmp]) dfs(tmp);//删除与rt相连的其他城市  } }int main(){int t,T,j;scanf("%d",&T);while(T--){int n;scanf("%d",&n);for(int i=0;i<n;i++){s[i].clear();vec[i].clear(); vis[i]=0;}memset(num,0,sizeof(num));num[1]=0;//到首都的距离为0int cnt=1;//代表城市的数字int Max=0;//最远距离 for(int i=0;i<n;i++){ scanf("%d",&t); if(t>0){//增加一个城市  cnt++;//城市数+1 vec[t].push_back(cnt);// t后面插入cnt; num[cnt]=num[t]+1;//城市cnt到首都的距离  s[num[cnt]].insert(cnt); //元素插入 表示距离为num[cnt]的城市集合  Max=max(Max,num[cnt]);//记录最远距离  for(j=Max;j>0;j--){ if(s[j].size()) break;//找出距离最远的集合  } if(j==0) printf("1\n"); else printf("%d\n",*s[j].begin()); //集合中字典序最小的  } else{//减少一个城市  t=-t; dfs(t);  vis[t]=1;//标记为删除的 for(j=Max;j>0;j--) { if(s[j].size()) break;//寻找距离最远的点的集合   }   Max=j;  if(j==0) printf("1\n");  else printf("%d\n",*s[j].begin());//距离为j的集合中字典序最小点  }   }}return 0;}


0 0
原创粉丝点击