BZOJ2610 [Poi2003]Monkeys

首先我们离线,把操作倒过来,就变成每次把一棵树连到1所在的树上

我们可以通过并查集来完成,然后就是每次并到1所在的树上时dfs一下这棵子树就好了

 1 /**************************************************************
 2     Problem: 2610
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:760 ms
 7     Memory:12528 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <algorithm>
12
13 using namespace std;
14 const int N = 2e5 + 5;
15
16 struct edge {
17     int next, to;
18
19     edge() {}
20     edge(int _n, int _t) : next(_n), to(_t) {}
21 } e[N << 1];
22
23 int n, m;
24 int son[N][2], del[N][2];
25 int q[N << 1][2], fa[N], ans[N];
26 int first[N], tot;
27
28 inline int read() {
29     int x = 0, sgn = 1;
30     char ch = getchar();
31     while (ch < ‘0‘ || ‘9‘ < ch) {
32         if (ch == ‘-‘) sgn = -1;
33         ch = getchar();
34     }
35     while (‘0‘ <= ch && ch <= ‘9‘) {
36         x = x * 10 + ch - ‘0‘;
37         ch = getchar();
38     }
39     return sgn * x;
40 }
41
42 inline void Add_Edges(int x, int y) {
43     e[++tot] = edge(first[x], y), first[x] = tot;
44     e[++tot] = edge(first[y], x), first[y] = tot;
45 }
46
47 #define y e[x].to
48 void dfs(int p, int fa, int t) {
49     ans[p] = t;
50     int x;
51     for (x = first[p]; x; x = e[x].next)
52         if (y != fa) dfs(y, p, t);
53 }
54 #undef y
55
56 int find_fa(int x) {
57     return x == fa[x] ? x : fa[x] = find_fa(fa[x]);
58 }
59
60 inline void set_union(int x, int y, int t) {
61     if (find_fa(x) == find_fa(y)) return;
62     if (fa[x] == find_fa(1)) dfs(y, 0, t);
63     else if (fa[y] == find_fa(1)) dfs(x, 0, t);
64     else Add_Edges(x, y);
65     fa[fa[x]] = fa[y];
66 }
67
68 int main() {
69     int i, j, x, y;
70     n = read(), m = read() - 1;
71     for (i = 1; i <= n; ++i)
72         son[i][0] = read(), son[i][1] = read(), fa[i] = i;
73     for (i = 0; i <= m; ++i) {
74         x = read(), y = read() - 1;
75         q[i][0] = x, q[i][1] = y, del[x][y] = 1;
76     }
77     for (i = 1; i <= n; ++i)
78         for (j = 0; j < 2; ++j)
79             if (!del[i][j] && son[i][j] > 0) set_union(i, son[i][j], -1);
80     for (i = m; ~i; --i)
81         set_union(q[i][0], son[q[i][0]][q[i][1]], i);
82     puts("-1");
83     for (i = 2; i <= n; ++i) printf("%d\n", ans[i]);
84     return 0;
85 }

时间: 2024-10-12 16:25:34

BZOJ2610 [Poi2003]Monkeys的相关文章

zoj 3888 Twelves Monkeys 二分+线段树维护次小值

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3888 Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surf

ZOJ 3888 Twelves Monkeys (预处理+优先队列)

题目链接:ZOJ 3888 Twelves Monkeys 题意:题目描写叙述起来比較绕,直接讲案例 9 3 3 9 1 6 1 4 1 6 7 2 输入n,m,q.n限制了你询问的年份,m台时光机,q个询问. 接下来m行,描写叙述从第9年回到第1年. 接下里就是三个询问. 第一个询问6的答案是5. 1.从第6年回到第1年 (6-1)直接做时光机2,(6-7-8-9-1)过3年之后能够做时光机3. 有两个路径能够走(题目中要求至少两条路径).所以是有效的方案. 2.从第6年回到第2年 .....

uva Monkeys in the Emei Mountain

                                                   Monkeys in the Emei  Mountain 题目:   给出N之猴子,每只猴子要喝v升的水,每个小时只能喝一升的水,并且只能在[a,b]时间段进行喝水,时间可以不连续,但是每次喝水必须是一个正的单位且不小于1小时.而喝水的地方就一个,一次只能容纳m只猴子.现在要你判断是否能在满足全部的猴子喝水. 算法:   网络流的区间模型.常规做回超时,这题跟hdu kebab的模型一样,那题

【POI2003/2004 stage I】

[原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares numbered successively from 1 to m. There are n pawns on the board, each on a distinct square. None of them occupies the square with number m. Each single mo

Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)

Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus so deadly that it forced the sur

POJ 2897 Monkeys&#39; Pride 解题报告

Description Background There are a lot of monkeys in a mountain. Every one wants to be the monkey king. They keep arguing with each other about that for many years. It is your task to help them solve this problem. Problem Monkeys live in different pl

zoj 3888 Twelves Monkeys(zoj 2015年7月月赛)

Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus so deadly that it forced the sur

2015 USP-ICMC gym 100733 I. The Cool Monkeys

I. The Cool Monkeys time limit per test 4.0 s memory limit per test 256 MB input standard input output standard output The first mafia on planet Earth was actually composed of a bunch of cool jumping monkeys. The name of that group was pretty simple

HDU - 6178:Monkeys (贪心&amp;树上最大匹配输&amp;输入优化)

There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occupied by at most one monkey. They want to remove some edges and leave minimum edges, but each monkey must be connected to at least one other monkey throug