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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int N = 100000 + 100;
vector<int>vec[N];
set<int>s[N];
int num[N],vis[N];
void dfs (int rt)
{
s[num[rt]].erase(rt);
for(int i=0;i<vec[rt].size();i++) {
int tmp=vec[rt][i];
if(!vis[tmp]) dfs(tmp);
}
}
int main()
{
int T,n,t,i,j,cnt,Max;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i=0;i<=n;i++) {
s[i].clear();
vec[i].clear();
vis[i]=0;
}
memset(num,0,sizeof(num));
num[1]=0;
cnt=1;
Max=0;
for(int _=1;_<=n;_++) {
scanf("%d",&t);
if(t>0) {
cnt++;
vec[t].push_back(cnt);
num[cnt]=num[t]+1;
s[num[cnt]].insert(cnt);
Max=max(Max,num[cnt]);
for(i=Max;i>0;i--) {
if(s[i].size()) break;
}
Max=i;
if(i==0) printf("1\n");
else printf("%d\n",*s[i].begin());
}
else {
t=-t;
dfs(t);
vis[t]=1;
for(i=Max;i>0;i--) {
if(s[i].size()) break;
}
Max=i;
if(i==0) printf("1\n");
else printf("%d\n",*s[i].begin());
}
}
}
return 0;
}