比赛 |
CSP2023-J模拟赛 |
评测结果 |
AAAAAAAAAA |
题目名称 |
新建题目 |
最终得分 |
100 |
用户昵称 |
usr10086 |
运行时间 |
0.293 s |
代码语言 |
C++ |
内存使用 |
3.59 MiB |
提交时间 |
2023-10-18 20:04:13 |
显示代码纯文本
- #include <bits/stdc++.h>
-
- using namespace std;
-
- const int N = 2e3 + 10;
-
- ifstream fin("touch.in");
- ofstream fout("touch.out");
- #define cin fin
- #define cout fout
-
- struct D
- {
- int i, down;
- };
-
- bool vis[N];
- int n, fa[N], w[N], dep[N], path[N];
- vector<int> son[N], g[N];
-
- void dfs(int x, int fa)
- {
- dep[x] = dep[fa]+1; ::fa[x] = fa;
- for (int y : g[x])
- {
- if (y == fa) continue;
- son[x].push_back(y);
- dfs(y, x);
- }
- }
-
- int main()
- {
- cin >> n;
- for (int i = 1; i <= n; i++) cin >> w[i];
- for (int i = 1, u, v; i <= n-1; i++)
- cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
- dfs(1, 0);
- long long ans = 0;
- for (int i = 1; i <= n; i++)
- {
- int x = i;
- while (x) path[dep[x]] = x, x = fa[x];
- x = i;
- memset(vis, 0, sizeof(bool)*(n+1));
- queue<D> q;
- q.push({i, false}); vis[i] = true;
- int cnt = 0;
- while (!q.empty())
- {
- D k = q.front(); q.pop();
- cnt++;
- int i = k.i, down = k.down;
- if (!down)
- {
- int f = fa[i];
- if (f && !vis[f] && w[f] < w[i]) vis[f] = true, q.push({f, false});
- }
- for (int y : son[i])
- if (!vis[y] && (w[y] > max(w[i], w[path[dep[i]]]) && (dep[y] >= dep[x] || w[y] < w[path[dep[y]+1]]))) vis[y] = true, q.push({y, true});
- }
- for (int d = 0; d <= dep[x]; d++) path[d] = 0;
- ans += (cnt-1); // i->i does not count
- }
- cout << ans / 2;
- }
-