Sicily 1910. Box

1910. Box

Constraints

Time Limit: 5 secs, Memory Limit: 32 MB

Description

 There are N boxes on the ground, which are labeled by numbers from 1 to N . The boxes are magical, the size of each one can be enlarged or reduced arbitrarily.

Jack can perform the ``MOVE x y " operation to the boxes: take out box x ; if y = 0 , put it on the ground; Otherwise, put it inside box y . All the boxes inside box x remain the same. It is possible that an operation is illegal, that is, if box y is contained (directly or indirectly) by box x , or if y is equal to x .

Input

Input contains several test cases.

For each test case, the first line has an integer N (1≤N≤50000) , representing the number of boxes.

Next line has N integers: a1, a2, a3,..., aN (0≤ai≤N) , describing the initial state of the boxes. If ai is 0, box i is on the ground, it is not contained by any box. Otherwise, box i is directly inside box ai . It is guaranteed that the input state is always correct (No loop exists).

Next line has an integer M (1≤M≤100000) , representing the number of MOVE operations and queries.

On the next M lines, each line contains a `MOVE' operation or a query:

MOVE x y , 1≤x≤N , 0≤y≤N , which is described above. If an operation is illegal, just ignore it.
QUERY x , 1≤x≤N , output the root box of box x .

Output

For each query, output the result on a single line. Use a blank line to separate each test case. 

Sample Input

2 
0 1 
5 
QUERY 1 
QUERY 2 
MOVE 2 0 
MOVE 1 2 
QUERY 1
6 
0 6 4 6 1 0 
4 
MOVE 4 1 
QUERY 3 
MOVE 1 4 
QUERY 1

Sample Output

1 
1 
2 

1 

1

// Problem#: 1910
// Submission#: 3591062
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>

const int MAXN = 50001;

struct Node {
    int child[2];
    int parent;
};

int n;
Node node[MAXN];

void rotate(int x) {
    int y = node[x].parent;
    int d = x == node[y].child[0] ? 0 : 1;
    int t = node[x].child[1 - d];
    if (t > 0) node[t].parent = y;
    node[y].child[d] = t;
    int z = node[y].parent;
    node[x].child[1 - d] = y;
    node[y].parent = x;
    node[x].parent = z;
    if (z > 0) {
        node[z].child[y == node[z].child[0] ? 0 : 1] = x;
    }
}

void splay(int x) {
    while (1) {
        int y = node[x].parent;
        if (y <= 0) break;
        int d = x == node[y].child[0] ? 0 : 1;
        int z = node[y].parent;
        if (z <= 0 || node[z].child[d] != y) rotate(x);
        else {
            rotate(y);
            rotate(x);
        }
    }
}

void expose(int v) {
    splay(v);
    int x = v;
    while (1) {
        int px = -node[x].parent;
        if (px == 0) break;
        splay(px);
        int lpx = node[px].child[0];
        if (lpx > 0) node[lpx].parent = -px;
        node[px].child[0] = x;
        node[x].parent = px;
        x = px;
    }
    splay(v);
}

void link(int v, int w) {
    expose(v);
    expose(w);
    node[v].parent = -w;
}

int root(int v) {
    expose(v);
    int w = v;
    while (node[w].child[1] > 0) w = node[w].child[1];
    splay(w);
    return w;
}

void cut(int v) {
    expose(v);
    int rv = node[v].child[1];
    if (rv > 0) {
        node[v].child[1] = 0;
        node[rv].parent = 0;;
    }
}

int parent(int v) {
    expose(v);
    int p = node[v].child[1];
    if (p == 0) return 0;
    while (node[p].child[0] > 0) p = node[p].child[0];
    splay(p);
    return p;
}

void readTrees() {
    for (int i = 1; i <= n; i++) {
        node[i].child[0] = node[i].child[1] = node[i].parent = 0;
    }
    for (int i = 1; i <= n; i++) {
        int p;
        scanf("%d", &p);
        if (p > 0) link(i, p);
    }
}

void solve() {
    int m;
    scanf("%d", &m);
    while (m--) {
        char cmd[10];
        int x, y;
        scanf("%s", cmd);
        if (cmd[0] == 'Q') {
            scanf("%d", &x);
            printf("%d\n", root(x));
        } else if (cmd[0] == 'M') {
            scanf("%d%d", &x, &y);
            int px = parent(x);
            if (px > 0) cut(x);
            if (y > 0) {
                if (root(y) != x) {
                    link(x, y);
                } else {
                    if (px > 0) link(x, px);
                }
            }
        }
    }
}

int main() {
    bool firstCase = 1;
    while (scanf("%d", &n) == 1) {
        if (firstCase) firstCase = 0;
        else printf("\n");
        readTrees();
        solve();
    }
    return 0;
}                                 


深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值