CDOJ 32 树上战争(Battle on the tree) 解题报告

啊啊,最后一篇了,已经零点多了,我还打算写一段第一次打工赚钱做家教的感想呢……

接下来有时间做决赛题,感觉也不是很难吼?

题目链接:http://acm.uestc.edu.cn/#/problem/show/32

很简单的题目,比较一棵有根树两个节点哪个高度。

同样,用了一行广搜。不要问我为什么叫一行广搜,不要让我压代码压得不成样子,编程可是一种艺术……

也许我应该规范一下比如const变量的命名?

#include <cstdio>
#include <cstring>
using namespace std;

const int maxN = 100005;

int N, M;
int g[maxN], to[maxN], next[maxN], e;
int root, father[maxN];
int queue[maxN], head, tail, depth[maxN];

int nextInt() {
	char c; while ((c = getchar()) < ‘0‘ || c > ‘9‘); int r = c - ‘0‘;
	while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) (r *= 10) += c - ‘0‘;
	return r;
}

void addEdge(int u, int v) {
	next[e] = g[u]; to[e] = v; g[u] = e++;
}

void bfs(int root) {
	memset(depth, 0, sizeof depth);
	for (queue[head = tail = 0] = root; head <= tail; ++head)
		for (int e = g[queue[head]], child = to[e]; ~e; child = to[e = next[e]])
			if (child != father[queue[head]]) depth[child] = depth[queue[head]] + 1, queue[++tail] = child;
}

int main() {
	while ((N = nextInt()) && (M = nextInt())) {
		memset(g, -1, sizeof g); e = 0;
		memset(father, 0, sizeof father);
		for (int i = 1; i < N; ++i) {
			int A = nextInt(), B = nextInt();
			addEdge(A, B); father[B] = A;
		}
		for (int i = 1; i <= N; ++i)
			if (!father[i]) {
				root = i; break;
			}
		bfs(root);
		while (M--) {
			int X = nextInt(), Y = nextInt();
			if (depth[X] <= depth[Y]) puts("lxh"); else puts("pfz");
		}
	}
	return 0;
}

OK,我写一遍真正的一行广搜。其实上面那个bfs只是把变量名变长了以及加了空格和换行而已,一些变量名稍微有变化,可以对照着上面的bfs函数看。


for(q[h=t=0]=rt;h<=t;++h)for(int e=g[q[h]];~e;e=nxt[e])if(to[e]!=p[q[h]])d[to[e]]=d[q[h]]+1,q[++t]=to[e];

杀了我吧,这段代码太丑了。

时间: 2024-10-19 05:07:01

CDOJ 32 树上战争(Battle on the tree) 解题报告的相关文章

UESTC 树上战争(Battle on the tree) Label:并查集?

给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. Input 输入包含多组数据 每组第一行包含两个数NN,MM(NN,M≤100000M≤100000),NN表示树的节点数,MM表示询问数,N=M=0N=M=0表示输入结束.节点的编号为11到NN. 接下来N−1N−1行,每行22个整数AA,BB(1≤A1≤A,B≤NB≤N),表示编号为AA的节点是编号为BB的节点的父亲. 接下来MM

cdoj32-树上战争(Battle on the tree) 【记忆化搜索】

http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. In

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

LeetCode: Balanced Binary Tree 解题报告

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Show Tags SOLU

LeetCode: Symmetric Tree 解题报告

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1   / \  2   2 / \ / \3  4 4  3But the following is not:    1   / \  2   2   \   \   3    3Note:

LeetCode: Validate Binary Search Tree 解题报告

Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node co

[POJ3237]Tree解题报告|树链剖分|边剖

关于边剖 之前做的大多是点剖,其实转换到边剖非常简单. 我的做法是每个点的点权记录其到父亲节点的边的边权. 只要solve的时候不要把最上面的点记录在内就可以了. Tree Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a

655. Print Binary Tree 解题报告(树)

第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / 2 3 4 Output: [["", "", "", "1"

LeetCode: Recover Binary Search Tree 解题报告

Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? c