1072 - Capture
Time Limit:15s Memory Limit:1024MByte
Submissions:396Solved:133
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) 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" 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" 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.
思路:+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;
}
}
}