HDU 5029 Relief grain
44609 단어 HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Problem Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
1 2 2 3 3 0 2
Hint
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
", ans[i]);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
hdu4671(다교리그 7--수 시뮬레이션)클릭하여 링크 열기 제목: n과 서버, m개의 데이터베이스가 있고 모든 데이터베이스는 서버를 연결해야 하지만 모든 데이터베이스는 서버를 연결하는 우선순위가 있습니다.모든 데이터베이스의 서버 우선순위를 구하다.또한 한...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.