#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 10010
#define M ll(10007)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef short int si;
typedef array <int, 3> a3;
typedef array <int, 4> a4;
typedef pair <int, int> pt;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int n, ans, from[N], a[N], siz[N], h[N], l, r, kol;
bool mk[105], mkr[105][105], mkrr[N];
vector <int> val[105], gr;
set <int> st[N];
vector <int> g[N];
bool bad, found;
void dfs_rev(int v)
{
st[v].insert(a[v]);
int lst = -1;
for (auto it : st[v])
{
if (lst != -1 && it - lst != 1) {bad = 1; return;}
lst = it;
}
if (v != 1)
{
int nxt = from[v];
if (nxt == 0 || !mkrr[nxt]) {bad = 1; return;}
if (sz(st[nxt]) < sz(st[v])) swap(st[v], st[nxt]);
for (auto it : st[v]) st[nxt].insert(it);
}
else if (r - l + 1 != sz(st[v])) bad = 1;
}
bool cmp(int x, int y) {return h[x] > h[y];}
bool cmpr(int x, int y) {return h[x] < h[y];}
void dfs(int v)
{
siz[v] = 1;
for (auto it : g[v])
{
h[it] = h[v] + 1;
dfs(it);
siz[v] += siz[it];
}
}
void rec(int v, int r)
{
if (r + 1 == v)
{
if (!found)
{
kol++;
bad = 0;
vector <int> pr = gr;
sort(pr.begin(), pr.end(), cmp);
for (auto it : pr) {dfs_rev(it); if (bad) break;}
if (!bad) {found = 1; ans++;}
if (kol == 15) found = 1;
}
}
else
{
for (auto it : val[v])
{
if (found) break;
gr.pb(it);
st[it].clear();
mkrr[it] = 1;
rec(v + 1, r);
mkrr[it] = 0;
if (found) break;
}
}
if (sz(gr) != 0) gr.pop_back();
}
int main()
{
//freopen("mining.in", "r", stdin); freopen("mining.out", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {cin >> a[i]; val[a[i]].pb(i); mk[a[i]] = 1;}
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].pb(y);
from[y] = x;
}
dfs(1);
for (l = 1; l <= 100; l++)
if (mk[l])
for (r = l; r <= 100; r++)
{
if (!mk[r]) break;
if (!(l <= a[1] && a[1] <= r)) continue;
bad = 0;
memset(mkrr, 0, sizeof(mkrr));
vector <int> gr; gr.clear();
for (int i = 1; i <= n; i++) st[i].clear();
for (int i = l; i <= r; i++) {mkrr[val[i][0]] = 1; gr.pb(val[i][0]);}
sort(gr.begin(), gr.end(), cmp);
for (auto it : gr) {dfs_rev(it); if (bad) break;}
if (!bad) {mkr[l][r] = 1; ans++;}
}
for (int i = 1; i <= 100; i++) sort(val[i].begin(), val[i].end(), cmpr);
for (l = 1; l <= 100; l++)
if (mk[l])
for (r = l; r <= 100; r++)
{
if (!mk[r]) break;
if (mkr[l][r]) continue;
if (!(l <= a[1] && a[1] <= r)) continue;
bad = 0;
memset(mkrr, 0, sizeof(mkrr));
vector <int> gr; gr.clear();
for (int i = 1; i <= n; i++) st[i].clear();
for (int i = l; i <= r; i++) {mkrr[val[i][0]] = 1; gr.pb(val[i][0]);}
sort(gr.begin(), gr.end(), cmp);
for (auto it : gr) {dfs_rev(it); if (bad) break;}
if (!bad) {mkr[l][r] = 1; ans++;}
}
for (int i = 1; i <= n; i++) st[i].clear();
memset(mkrr, 0, sizeof(mkrr));
for (l = 1; l <= 100; l++)
if (mk[l])
for (r = l; r <= 100; r++)
{
if (!mk[r]) break;
if (mkr[l][r]) continue;
if (!(l <= a[1] && a[1] <= r)) continue;
gr.clear();
kol = 0;
found = 0;
rec(l, r);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1024 KB |
Output isn't correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
18 ms |
1024 KB |
Output is correct |
7 |
Correct |
22 ms |
1152 KB |
Output is correct |
8 |
Correct |
8 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Incorrect |
6 ms |
1408 KB |
Output isn't correct |
12 |
Incorrect |
6 ms |
1408 KB |
Output isn't correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
14 |
Correct |
188 ms |
1912 KB |
Output is correct |
15 |
Correct |
136 ms |
1792 KB |
Output is correct |
16 |
Correct |
168 ms |
1792 KB |
Output is correct |
17 |
Correct |
5 ms |
1408 KB |
Output is correct |
18 |
Correct |
5 ms |
1408 KB |
Output is correct |
19 |
Correct |
71 ms |
1280 KB |
Output is correct |
20 |
Correct |
73 ms |
1280 KB |
Output is correct |