Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3398 仓鼠找sugar

P3398 仓鼠找sugar

题意:判断两条树上路径是否相交。

对于每条路径,在路径上的点增加 $1$ 的值。如果路径上有值为 $2$ 的点,则路径相交。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
int n, q, d[N], p[N], sz[N];
int idx, hd[N], nxt[M], edg[M];
int stmp, dfn[N], top[N], son[N];
struct Node
{
int mx;
int l, r, tag;
} tr[N << 2];
void pushup(int x)
{
tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx);
}
void pushdown(int x)
{
tr[x << 1].tag += tr[x].tag;
tr[x << 1 | 1].tag += tr[x].tag;
tr[x << 1].mx += tr[x].tag;
tr[x << 1 | 1].mx += tr[x].tag;
tr[x].tag = 0;
}
void build(int x, int l, int r)
{
tr[x].l = l, tr[x].r = r;
if (l == r)
return;
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
}
void modify(int x, int l, int r, int k)
{
if (tr[x].l >= l && tr[x].r <= r)
{
tr[x].tag += k;
tr[x].mx += k;
return;
}
if (tr[x].tag)
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
modify(x << 1, l, r, k);
if (r > mid)
modify(x << 1 | 1, l, r, k);
pushup(x);
}
int query(int x, int l, int r)
{
if (tr[x].l >= l && tr[x].r <= r)
return tr[x].mx;
if (tr[x].tag)
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
int res = 0;
if (l <= mid)
res = max(res, query(x << 1, l, r));
if (r > mid)
res = max(res, query(x << 1 | 1, l, r));
return res;
}
void dfs1(int x)
{
sz[x] = 1;
son[x] = -1;
for (int i = hd[x]; ~i; i = nxt[i])
if (!d[edg[i]])
{
d[edg[i]] = d[x] + 1;
p[edg[i]] = x;
dfs1(edg[i]);
sz[x] += sz[edg[i]];
if (son[x] == -1 || sz[edg[i]] > sz[son[x]])
son[x] = edg[i];
}
}
void dfs2(int x, int t)
{
dfn[x] = ++stmp;
top[x] = t;
if (son[x] == -1)
return;
dfs2(son[x], t);
for (int i = hd[x]; ~i; i = nxt[i])
if (edg[i] != p[x] && edg[i] != son[x])
dfs2(edg[i], edg[i]);
}
void ModifyPath(int x, int y, int k)
{
while (top[x] != top[y])
{
if (d[top[x]] < d[top[y]])
swap(x, y);
modify(1, dfn[top[x]], dfn[x], k);
x = p[top[x]];
}
if (d[x] > d[y])
swap(x, y);
modify(1, dfn[x], dfn[y], k);
}
bool QueryPath(int x, int y)
{
while (top[x] != top[y])
{
if (d[top[x]] < d[top[y]])
swap(x, y);
if (query(1, dfn[top[x]], dfn[x]) == 2)
return true;
x = p[top[x]];
}
if (d[x] > d[y])
swap(x, y);
return query(1, dfn[x], dfn[y]) == 2;
}
void add(int a, int b)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
}
int main()
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
hd[i] = -1;
for (int i = 1, u, v; i < n; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
d[1] = 1;
dfs1(1);
dfs2(1, 1);
build(1, 1, n);
for (int a, b, c, d; q; q--)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
ModifyPath(a, b, 1);
ModifyPath(c, d, 1);
puts(QueryPath(a, b) ? "Y" : "N");
ModifyPath(a, b, -1);
ModifyPath(c, d, -1);
}
return 0;
}