题目
题目有点多,待补全
1001 Static Query on Tree
Problem Description
In country X, there are n cities and n−1 one-way roads, and all city can reach city 1. One query will give 3 sets of cities A, B, C. Alice will choose a city x in set A, choose a city z in set C, and walk from x to z (if x can reach z). Bob will choose a city y in set B, and walk from y to z (if y can reach z). How many cities can possibly be the city where Alice and Bob meet each other?
In other words, how many cities can be reached from at least one city in set A, and can be reached from at least one city in set B, and can reach at least one city in set C?
There are T test cases, and each case has q queries.
Input
First line is one integer T, indicating T test cases. In each case:
First line is 2 integers n,q, indicating n cities and q queries.
Next line is n−1 integers r1,r2,…,rn−1, the i-th integer indicates the road from city i+1 to city ri.
Next is q queries, in each query:
First line is 3 integer |A|,|B|,|C|, indicating the size of set A, B, C.
Next line is |A| integers, indicating the set A.
Next line is |B| integers, indicating the set B.
Next line is |C| integers, indicating the set C.
1≤T≤20, 1≤n,q,|A|,|B|,|C|≤2×105, for all cases ∑n≤2×105, ∑q≤2×105, for all queries in all cases ∑|A|+∑|B|+∑|C|≤2×105.
Output
In each case, print q integers, one integer per line, i-th integer indicates the answer of i-th query.
Sample Input
1
7 3
1 1 2 2 3 3
2 1 1
1 2
4
1
4 4 3
4 5 6 7
4 5 6 7
2 4 6
2 1 1
4 5
6
1
Sample Output
2
4
1
Hint
For the first query, city 1, 2.
(1 can be reached from 1 (A), 4 (B), can reach 1©)
(2 can be reached from 2 (A), 4 (B), can reach 1©)
For the second query, city 2, 4, 5, 6.
For the third query, only city 1.
Source
2022“杭电杯”中国大学生算法设计超级联赛(2)
给你一颗n个结点的树和n-1条单向边,给你A B C 三个集合,问有多少个城市可以被A、B到达,并且能到达C集合
对于A和B能到达的城市,可以看作从该结点到根节点的路径,而能到达C集合的节点一定是在C的子树内,所以我们用树剖去标记这三种集合
那么满足的结点就是他们的并集
tags:树链剖分
AC代码
/****************
*@description:for the Escape Project
*@author: Nebula_xuan
* @Date: 2022-07-24 16:25:06
*************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
#define x first
#define y second
#define rep(i, h, t) for (int i = h; i <= t; i++)
#define dep(i, t, h) for (int i = t; i >= h; i--)
#define endl char(10)
#define int long long
//记得%d应该写成%lld
typedef pair<int, int> PII;
typedef long long ll;
const int N = 2e5 + 10, M = 2 * N;
int n, m, q;
int h[N], e[M], ne[M], idx;
int sz[N], dep[N], son[N], fa[N];
int id[N], top[N], cnt, nw[N];
vector<PII> a, b, c;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs1(int u, int father)
{
fa[u] = father;
dep[u] = dep[father] + 1;
sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == father)
continue;
dfs1(j, u);
sz[u] += sz[j];
if (sz[son[u]] < sz[j])
son[u] = j;
}
}
void dfs2(int u, int t)
{
id[u] = ++cnt, top[u] = t, nw[cnt] = u;
if (!son[u])
return;
dfs2(son[u], t);
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == fa[u] || j == son[u])
continue;
dfs2(j, j);
}
}
void path(int u, int v, vector<PII> &vec)
{
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]])
swap(u, v);
vec.push_back({
id[top[u]], id[u]});
u = fa[top[u]];
}
if (dep[u] < dep[v])
swap(u, v);
vec.push_back({
id[v], id[u]});
}
void merge(vector<PII> &v)
{
if (!v.size())
return;
vector<PII> res;
sort(v.begin(), v.end());
int st = v[0].x, ed = v[0].y;
for (auto item : v)
{
if (item.x > ed)
{
res.push_back({
st, ed});
st = item.x, ed = item.y;
}
else
ed = max(ed, item.y);
}
res.push_back({
st, ed});
v = res;
}
vector<PII> Union(vector<PII> a, vector<PII> b)
{
vector<PII> v;
int idx1 = 0, idx2 = 0;
while (idx1 < a.size() && idx2 < b.size())
{
int xx = max(a[idx1].x, b[idx2].x);
int yy = min(a[idx1].y, b[idx2].y);
if (xx <= yy)
v.push_back({
xx, yy});
if (a[idx1].y < b[idx2].y)
idx1++;
else
idx2++;
}
return v;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
idx = 0, cnt = 0;
cin >> n >> q;
rep(i, 0, n)
h[i] = -1,
son[i] = 0;
rep(i, 2, n)
{
int x;
cin >> x;
add(x, i);
add(i, x);
}
dfs1(1, 0);
dfs2(1, 1);
while (q--)
{
int x, y, z, u;
cin >> x >> y >> z;
while (x--)
{
cin >> u;
path(1, u, a);
}
while (y--)
{
cin >> u;
path(1, u, b);
}
while (z--)
{
cin >> u;
c.push_back({
id[u], id[u] + sz[u] - 1});
}
merge(a);
merge(b);
merge(c);
vector<PII> ans;
ans = Union(a, b);
ans = Union(ans, c);
int flag = 0;
for (auto item : ans)
{
flag += item.y - item.x + 1;
}
cout << flag << endl;
a.clear(), b.clear(), c.clear();
}
}
}
1002 C++ to Python
Problem Description
Kayzin’s repository has lots of code in C++, but Aba aba can only understand Python code. So Aba aba calls you for help.
The only things you should do is using (…) in Python to match std::make_tuple(…) in C++.
Input
First line has one integer TT, indicates TT test cases. In each case:
First line has string ss, indicates C++ code.
The C++ code only contains std::make_tuple, “(”, “)”, “,” and integers, without space.
1\le T \le 1001≤T≤100, the length of ss not exceeds 1000.
Output
Each test cases print one line of Python code, do not print any space.
Sample Input
2 std::make_tuple(-2,3,3,std::make_tuple(3,3)) std::make_tuple(std::make_tuple(1,1,4,5,1,4))
Sample Output
(-2,3,3,(3,3)) ((1,1,4,5,1,4))
签到题,稍微处理一下输出即可
AC代码
/****************
*@description:for the Escape Project
*@author: Nebula_xuan
* @Date: 2022-07-21 12:02:34
*************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
inline ll read()
{
ll x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c))
{
if (c == '-')
f = 1;