Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
The input consists of at most 25 test cases.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
2 4
1 2
1 1 1
1 2 2
2 2 2
2 2 1
5 3
1 2
3 1
3 4
5 3
2 3 3
1 5 2
3 3 3
0 0
1
2
2
3
3
0
2
For the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}.
有n个村庄,村庄之间有n-1条边,然后给出两个村庄之间有w这个种类的作物,问每个村庄最多的作物种类是什么,假如两个数量相同的话,输出最小的。
树链剖分的一道题目,但是这次求得是种类得多少,所以得话就要去存这些作物种类得数量,首先就是打出模板,在线段树维护得是种类得数量
在找到两个村庄之间的树链之后,可以用一个结构体去存这个链的头和尾以及权值,这个权值就是种类了,然后在用线段树不断维护这个权值就行了
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 100010;
struct Edge {
int next;
int to;
};
Edge edge[maxn << 1];
struct ED {
int l;
int r;
int val;
ED(int l, int r, int val) : l(l), r(r), val(val) {};
};
vector<ED> e;
vector<int> a[maxn];
int num[maxn];
int deep[maxn];
int fa[maxn];
int son[maxn];
int p[maxn];
int fp[maxn];
int top[maxn];
int head[maxn];
int tot, pos;
void init() {
tot = 0;
memset(head, -1, sizeof(head));
pos = 0;
memset(son, -1, sizeof(son));
e.clear();
for (int i = 0; i < maxn; i++) a[i].clear();
}
void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int pre, int d) {
deep[u] = d;
fa[u] = pre;
num[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (v != pre) {
dfs(v, u, d + 1);
num[u] += num[v];
if (son[u] == -1 || num[son[u]] < num[v]) {
son[u] = v;
}
}
}
}
void getpos(int u, int sp) {
top[u] = sp;
p[u] = ++pos;
fp[p[u]] = u;
if (son[u] == -1) return ;
getpos(son[u], sp);
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (v != fa[u] && v != son[u]) {
getpos(v, v);
}
}
}
struct NODE {
int l;
int r;
int Max;
int id;
int mid () {
return (l + r) >> 1;
}
};
NODE node[maxn << 2];
void pushup(int rt) {
if (node[rt << 1].Max < node[rt << 1 | 1].Max) {
node[rt].id = node[rt << 1 | 1].id;
} else node[rt].id = node[rt << 1].id;
node[rt].Max = max(node[rt << 1].Max, node[rt << 1 | 1].Max);
}
void build(int l, int r, int rt) {
node[rt].l = l;
node[rt].r = r;
if (l == r) {
node[rt].Max = 0;
node[rt].id = l;
return ;
}
int mid = node[rt].mid();
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
pushup(rt);
}
void updata(int k, int val, int rt) {
if (node[rt].l == k && node[rt].r == k) {
node[rt].Max += val;
return ;
}
int mid = node[rt].mid();
if (k <= mid) updata(k, val, rt << 1);
else updata(k, val, rt << 1 | 1);
pushup(rt);
}
void findn(int u, int v, int k) {
int f1 = top[u], f2 = top[v];
while (f1 != f2) {
if (deep[f1] < deep[f2]) {
swap(f1, f2);
swap(u, v);
}
e.push_back(ED(p[f1], p[u], k));
u = fa[f1];
f1 = top[u];
}
if (deep[u] > deep[v]) swap(u, v);
e.push_back(ED(p[u], p[v], k));
}
int main() {
int n, m, u, v, val;
while (scanf("%d %d", &n, &m) != EOF && n + m) {
init();
int ans[maxn] = {0};
for (int i = 0; i < n - 1; i++) {
scanf("%d %d", &u, &v);
addedge(u, v);
addedge(v, u);
}
dfs(1, 0, 0);
getpos(1, 1);
while (m--) {
scanf("%d %d %d", &u, &v, &val);
findn(u, v, val);
}
for (int i = 0; i < e.size(); i++) {
int u = e[i].l, v = e[i].r, val = e[i].val;
a[u].push_back(val);
a[v + 1].push_back(-val);
}
for (int i = 1; i <= n; i++) sort(a[i].begin(), a[i].end());
build(1, maxn, 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < a[i].size(); j++) {
int tmp = a[i][j];
if (tmp < 0) updata(-tmp, -1, 1);
else updata(tmp, 1, 1);
}
if (node[1].Max <= 0) node[1].id = 0;
ans[fp[i]] = node[1].id;
}
for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
return 0;
}