【玲珑杯R7 B】Capture

Submissions:445Solved:150

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
T
T, indicating the number of test cases.
For each test case:

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.
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
1
5
+1
+2
+1
-2
+4
SAMPLE OUTPUT
2
3
3
4
5

【题目链接】:http://www.ifrog.cc/acm/problem/1072

【题解】

每次都加入一个点到一棵树上的某个节点下面;
容易想出来最后是个树结构;
且1号节点肯定一直都是根节点;
则维护每个节点的深度;
每次添加节点的时候直接更新这个节点的深度;
用set维护一个深度最大的节点(相同则编号小的优先);
删除的时候,用dfs把这个节点及以下的节点都删掉.

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

struct abc
{
    int num,dep;
    friend bool operator <(abc a,abc b)
    {
        if (a.dep!=b.dep)
            return a.dep > b.dep;
        else
            return a.num < b.num;
    }
};

int fi[MAXN],nex[MAXN*2],en[MAXN*2],n,totm = 0,num = 0,dep[MAXN];
bool bo[MAXN];
set <abc> myset;

void dfs(int x)
{
    if (!bo[x])
        return;
    myset.erase({x,dep[x]});
    bo[x] = false;
    for (int i = fi[x];i!=0;i = nex[i])
    {
        int y = en[i];
        dfs(y);
    }
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    int T;
    cin >> T;
    while (T--)
    {
        myset.clear();
        totm = 0;
        num = 1;
        rep1(i,1,100000)
            fi[i] = 0,dep[i] = 0;
        memset(bo,true,sizeof bo);
        cin >> n;
        myset.insert({1,0});
        dep[1] = 0;
        rep1(i,1,n)
        {
            char key;int x;
            cin >> key >> x;
            if (key=='+')
            {
                num++;
                totm++;
                nex[totm] = fi[x];
                fi[x] = totm;
                en[totm] = num;
                dep[num] = dep[x]+1;
                myset.insert({num,dep[num]});
            }
            else
                dfs(x);
            printf("%d
",myset.begin()->num);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626775.html