LA3486 Cells

弱弱的链接

给定一棵树,询问两个节点x和y,判断x是否是y的祖先

好吧,我承认对于带时间戳dfs我还不是那么了解,看了题解之后才有恍然大悟的感觉,这种写法我记下了

如果一个节点是另一个节点的祖先,那么我们遍历这棵树的时候,到达这个节点的时间肯定比另一个节点早,并且第二次到达这个节点的时候肯定比另一个节点晚,这就是时间戳的应用,那么我们只需要遍历一遍这棵树,O(n)的复杂度,预处理出来所有的节点两次访问的时间,然后询问的时候直接按照条件询问即可。

需掌握且必须掌握的就是时间戳dfs

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 300010;
const int maxtot = 20000010;
int cs, n, c[maxn], start[maxn], pre[maxtot], post[maxtot], dfs_clock;
void dfs(int root){
	stack<int> st;
	st.push(root);
	pre[root] = 0;
	dfs_clock = 1;
	while(!st.empty()){
		int u = st.top();
		if (pre[u]){
			post[u] = dfs_clock++;
			st.pop();
			continue;
		}
		pre[u] = dfs_clock++;
		for (int i = start[u]; i < start[u] + c[u]; i++){
			if (i < n){
				pre[i] = 0; st.push(i);
			}
			else{
				pre[i] = dfs_clock++; post[i] = dfs_clock++;
			}
		}
	}
}
int main(){
	scanf("%d", &cs);
	for (int k = 1; k <= cs; k++){
		printf("Case %d:\n", k);
		scanf("%d", &n);
		int s = 1;
		for (int i = 0; i < n; i++){
			scanf("%d", &c[i]);
			start[i] = s; s += c[i];
		}
		dfs(0);
		int m, x, y;
		scanf("%d", &m);
		while(m--){
			scanf("%d%d", &x, &y);
			if (pre[x] < pre[y] && post[x] > post[y]) puts("Yes");
			else puts("No");
		}
		if (k < cs) puts("");
	}
	return 0;
}


### 向集合添加元素的方法 在数据结构中,向集合添加元素的方式取决于具体的集合实现方式。以下是几种常见集合类型的添加方法及其示例代码。 #### 使用链表(LinkedList) 对于 `LinkedList` 类型的集合,在任意位置插入新元素的时间复杂度为 \( O(1) \)[^2]。可以通过头插法、尾插法或其他指定位置插入来完成操作。 ```c typedef struct Node { int data; struct Node* next; } Node; void addElementToLinkedList(Node** head, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; if (*head == NULL) { // 如果链表为空,则设置新节点为首节点 *head = newNode; } else { // 尾部插入 Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } ``` --- #### 使用动态数组(Vector 或 ArrayList) 如果使用的是基于动态数组的集合类型(如 C++ 的 `std::vector`),则可以利用索引快速访问修改元素。通过调用 `.push_back()` 方法可以在数组末尾追加新元素。 ```cpp #include <vector> void addElementToVector(std::vector<int>& vec, int value) { vec.push_back(value); // 在末尾添加元素 } // 示例 int main() { std::vector<int> myVec; addElementToVector(myVec, 5); addElementToVector(myVec, 10); for (const auto& elem : myVec) { printf("%d ", elem); } return 0; } ``` --- #### 基于哈希表的集合 当使用哈希表作为底层存储时,可以直接将元素插入到对应的桶中。例如,C 中定义了一个散列表单元 `Cell` 来表示单个键值对[^1]。 ```c #define MAX_CELLS 100 typedef struct HashEntry { int key; int value; } Cell; Cell H[MAX_CELLS]; void insertIntoHashTable(Cell hashTable[], int size, int key, int value) { int index = key % size; // 计算哈希值 while (hashTable[index].key != 0 && hashTable[index].key != -1) { index = (index + 1) % size; // 开放定址法解决冲突 } hashTable[index].key = key; hashTable[index].value = value; } // 初始化哈希表 for (int i = 0; i < MAX_CELLS; ++i) { H[i].key = -1; // 表示该槽位未被占用 } insertIntoHashTable(H, MAX_CELLS, 7, 42); ``` --- #### PTA 平台上的顺序表合并 针对 PTA 题目中的顺序表合并需求[^4],通常会涉及两个已排序的线性表 LA LB,目标是将其合并成一个新的非递增顺序表 LC。以下是一个可能的实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct SeqList { int* data; int length; int capacity; } SeqList; SeqList* createSeqList(int cap) { SeqList* list = (SeqList*)malloc(sizeof(SeqList)); list->data = (int*)malloc(cap * sizeof(int)); list->length = 0; list->capacity = cap; return list; } void mergeLists(SeqList* la, SeqList* lb, SeqList* lc) { int i = 0, j = 0, k = 0; while (i < la->length && j < lb->length) { if (la->data[i] >= lb->data[j]) { lc->data[k++] = la->data[i++]; } else { lc->data[k++] = lb->data[j++]; } } while (i < la->length) { lc->data[k++] = la->data[i++]; } while (j < lb->length) { lc->data[k++] = lb->data[j++]; } lc->length = k; } // 测试代码 int main() { SeqList* A = createSeqList(10); SeqList* B = createSeqList(10); SeqList* C = createSeqList(A->capacity + B->capacity); A->data[0] = 9; A->data[1] = 7; A->data[2] = 5; A->length = 3; B->data[0] = 8; B->data[1] = 6; B->data[2] = 4; B->length = 3; mergeLists(A, B, C); for (int i = 0; i < C->length; ++i) { printf("%d ", C->data[i]); } free(A->data); free(B->data); free(C->data); free(A); free(B); free(C); return 0; } ``` 上述代码展示了如何将两个有序表按降序合并至第三个表中。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值