***玲珑1072- Capture
来源:互联网 发布:gta5低配优化补丁3dm 编辑:程序博客网 时间:2024/05/21 01:42
Time Limit:15s Memory Limit:1024MByte
Submissions:539Solved:178
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.
The first line of the input contains the integer number N (1≤N≤100000)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.
#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;}
- 玲珑oj 1072 - Capture
- ***玲珑1072- Capture
- 玲珑杯-Round #7.B-1072-Capture
- 玲珑杯 1072 - Capture(DFS序+线段树)
- 玲珑杯 1072 - Capture (树的操作)
- 玲珑学院OJ 1072 Capture【并查集+优先队列】
- 【玲珑杯R7 B】Capture
- 玲珑学院1072 【DFS】
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
- Capture
- [ifrog 1072 Capture] 优先队列/线段树
- 玲珑龟
- 玲珑龟
- 【玲珑】1099
- 玲珑 1100
- 玲珑OJ1102
- 【玲珑】1106
- 玲珑 1137
- 关于Android换行符的注意
- git//只是为了自己了解一下
- Android开发工具类--LogUtils
- centos6.5 yum安装redis
- androd things 开篇之唠叨
- ***玲珑1072- Capture
- Android中ContextImpl源码分析(二)
- 微信小程序—新闻阅读器
- 多线程——总结在多线程中几种释放锁和不释放锁的操作
- 图像算法之十二:非局部均值滤波及其Matlab实现
- JS中的Promise 使用
- 关于oracle 11.2.0.4 使用asm磁盘组条件下对应用数据表空间文件的rename alias操作
- XML文件的相关配置
- ssm相对标准配置(Spring+springmvc+mybatis)