#include <bits/stdc++.h>
#define f(i,j,n) for(int i = j; i < n; ++i)
#define fr(i,j,n) for(int i = j; i >= n; --i)
#define sz(v) (int) v.size()
#define ff first
#define ss second
#define endl "\n"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int N = 1e4 + 20;
const int M = 35;
const ll inf = 1e16;
int n,c[N];
vi adj[N],path[N];
void dfs(int v,int f){
for(auto u : adj[v]){
if(u == f) continue;
path[u] = path[v];
path[u].push_back(u);
dfs(u,v);
}
}
void solve(){
cin >> n;
f(i,1,n+1) cin >> c[i];
path[1] = {1};
dfs(1,0);
f(i,1,n){
int a,b; cin >> a >> b;
int ans = 0;
f(j,0,sz(path[a])){
f(k,j+1,sz(path[a])){
if(c[path[a][j]] > c[path[a][k]]) ans++;
}
}
for(auto u : path[a]) c[u] = c[b];
adj[a].push_back(b);
adj[b].push_back(a);
cout << ans << endl;
f(j,2,n+1) path[j].clear();
dfs(1,0);
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
f(i,1,tc+1){
// cout << "Case #" << i << ": ";
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
788 KB |
Output is correct |
2 |
Correct |
1 ms |
784 KB |
Output is correct |
3 |
Correct |
1 ms |
804 KB |
Output is correct |
4 |
Correct |
2 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
808 KB |
Output is correct |
6 |
Correct |
5 ms |
844 KB |
Output is correct |
7 |
Correct |
6 ms |
772 KB |
Output is correct |
8 |
Correct |
24 ms |
1856 KB |
Output is correct |
9 |
Correct |
27 ms |
2072 KB |
Output is correct |
10 |
Correct |
25 ms |
1952 KB |
Output is correct |
11 |
Correct |
11 ms |
1260 KB |
Output is correct |
12 |
Correct |
12 ms |
1276 KB |
Output is correct |
13 |
Correct |
15 ms |
1228 KB |
Output is correct |
14 |
Correct |
11 ms |
1336 KB |
Output is correct |
15 |
Correct |
4 ms |
796 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
4 ms |
844 KB |
Output is correct |
18 |
Correct |
5 ms |
792 KB |
Output is correct |
19 |
Correct |
12 ms |
1308 KB |
Output is correct |
20 |
Correct |
12 ms |
1300 KB |
Output is correct |
21 |
Correct |
11 ms |
1304 KB |
Output is correct |
22 |
Correct |
5 ms |
844 KB |
Output is correct |
23 |
Correct |
5 ms |
844 KB |
Output is correct |
24 |
Correct |
5 ms |
788 KB |
Output is correct |
25 |
Correct |
7 ms |
836 KB |
Output is correct |
26 |
Correct |
25 ms |
1872 KB |
Output is correct |
27 |
Correct |
14 ms |
1300 KB |
Output is correct |
28 |
Correct |
16 ms |
1268 KB |
Output is correct |
29 |
Correct |
12 ms |
1288 KB |
Output is correct |
30 |
Correct |
4 ms |
780 KB |
Output is correct |
31 |
Correct |
5 ms |
844 KB |
Output is correct |
32 |
Correct |
12 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
1048 KB |
Output is correct |
34 |
Correct |
6 ms |
972 KB |
Output is correct |
35 |
Correct |
6 ms |
908 KB |
Output is correct |
36 |
Correct |
7 ms |
1048 KB |
Output is correct |
37 |
Correct |
6 ms |
944 KB |
Output is correct |
38 |
Correct |
5 ms |
844 KB |
Output is correct |
39 |
Correct |
8 ms |
1044 KB |
Output is correct |
40 |
Correct |
6 ms |
992 KB |
Output is correct |
41 |
Correct |
6 ms |
844 KB |
Output is correct |
42 |
Correct |
7 ms |
1048 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
788 KB |
Output is correct |
2 |
Correct |
1 ms |
784 KB |
Output is correct |
3 |
Correct |
1 ms |
804 KB |
Output is correct |
4 |
Correct |
2 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
808 KB |
Output is correct |
6 |
Correct |
5 ms |
844 KB |
Output is correct |
7 |
Correct |
6 ms |
772 KB |
Output is correct |
8 |
Correct |
24 ms |
1856 KB |
Output is correct |
9 |
Correct |
27 ms |
2072 KB |
Output is correct |
10 |
Correct |
25 ms |
1952 KB |
Output is correct |
11 |
Correct |
11 ms |
1260 KB |
Output is correct |
12 |
Correct |
12 ms |
1276 KB |
Output is correct |
13 |
Correct |
15 ms |
1228 KB |
Output is correct |
14 |
Correct |
11 ms |
1336 KB |
Output is correct |
15 |
Correct |
4 ms |
796 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
4 ms |
844 KB |
Output is correct |
18 |
Correct |
5 ms |
792 KB |
Output is correct |
19 |
Correct |
12 ms |
1308 KB |
Output is correct |
20 |
Correct |
12 ms |
1300 KB |
Output is correct |
21 |
Correct |
11 ms |
1304 KB |
Output is correct |
22 |
Correct |
5 ms |
844 KB |
Output is correct |
23 |
Correct |
5 ms |
844 KB |
Output is correct |
24 |
Correct |
5 ms |
788 KB |
Output is correct |
25 |
Correct |
7 ms |
836 KB |
Output is correct |
26 |
Correct |
25 ms |
1872 KB |
Output is correct |
27 |
Correct |
14 ms |
1300 KB |
Output is correct |
28 |
Correct |
16 ms |
1268 KB |
Output is correct |
29 |
Correct |
12 ms |
1288 KB |
Output is correct |
30 |
Correct |
4 ms |
780 KB |
Output is correct |
31 |
Correct |
5 ms |
844 KB |
Output is correct |
32 |
Correct |
12 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
1048 KB |
Output is correct |
34 |
Correct |
6 ms |
972 KB |
Output is correct |
35 |
Correct |
6 ms |
908 KB |
Output is correct |
36 |
Correct |
7 ms |
1048 KB |
Output is correct |
37 |
Correct |
6 ms |
944 KB |
Output is correct |
38 |
Correct |
5 ms |
844 KB |
Output is correct |
39 |
Correct |
8 ms |
1044 KB |
Output is correct |
40 |
Correct |
6 ms |
992 KB |
Output is correct |
41 |
Correct |
6 ms |
844 KB |
Output is correct |
42 |
Correct |
7 ms |
1048 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
920 KB |
Output is correct |
45 |
Correct |
20 ms |
868 KB |
Output is correct |
46 |
Correct |
327 ms |
1284 KB |
Output is correct |
47 |
Correct |
349 ms |
1336 KB |
Output is correct |
48 |
Correct |
329 ms |
1288 KB |
Output is correct |
49 |
Execution timed out |
2077 ms |
19324 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
788 KB |
Output is correct |
2 |
Correct |
1 ms |
784 KB |
Output is correct |
3 |
Correct |
1 ms |
804 KB |
Output is correct |
4 |
Correct |
2 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
808 KB |
Output is correct |
6 |
Correct |
5 ms |
844 KB |
Output is correct |
7 |
Correct |
6 ms |
772 KB |
Output is correct |
8 |
Correct |
24 ms |
1856 KB |
Output is correct |
9 |
Correct |
27 ms |
2072 KB |
Output is correct |
10 |
Correct |
25 ms |
1952 KB |
Output is correct |
11 |
Correct |
11 ms |
1260 KB |
Output is correct |
12 |
Correct |
12 ms |
1276 KB |
Output is correct |
13 |
Correct |
15 ms |
1228 KB |
Output is correct |
14 |
Correct |
11 ms |
1336 KB |
Output is correct |
15 |
Correct |
4 ms |
796 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
4 ms |
844 KB |
Output is correct |
18 |
Correct |
5 ms |
792 KB |
Output is correct |
19 |
Correct |
12 ms |
1308 KB |
Output is correct |
20 |
Correct |
12 ms |
1300 KB |
Output is correct |
21 |
Correct |
11 ms |
1304 KB |
Output is correct |
22 |
Correct |
5 ms |
844 KB |
Output is correct |
23 |
Correct |
5 ms |
844 KB |
Output is correct |
24 |
Correct |
5 ms |
788 KB |
Output is correct |
25 |
Correct |
7 ms |
836 KB |
Output is correct |
26 |
Correct |
25 ms |
1872 KB |
Output is correct |
27 |
Correct |
14 ms |
1300 KB |
Output is correct |
28 |
Correct |
16 ms |
1268 KB |
Output is correct |
29 |
Correct |
12 ms |
1288 KB |
Output is correct |
30 |
Correct |
4 ms |
780 KB |
Output is correct |
31 |
Correct |
5 ms |
844 KB |
Output is correct |
32 |
Correct |
12 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
1048 KB |
Output is correct |
34 |
Correct |
6 ms |
972 KB |
Output is correct |
35 |
Correct |
6 ms |
908 KB |
Output is correct |
36 |
Correct |
7 ms |
1048 KB |
Output is correct |
37 |
Correct |
6 ms |
944 KB |
Output is correct |
38 |
Correct |
5 ms |
844 KB |
Output is correct |
39 |
Correct |
8 ms |
1044 KB |
Output is correct |
40 |
Correct |
6 ms |
992 KB |
Output is correct |
41 |
Correct |
6 ms |
844 KB |
Output is correct |
42 |
Correct |
7 ms |
1048 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
920 KB |
Output is correct |
45 |
Correct |
20 ms |
868 KB |
Output is correct |
46 |
Correct |
327 ms |
1284 KB |
Output is correct |
47 |
Correct |
349 ms |
1336 KB |
Output is correct |
48 |
Correct |
329 ms |
1288 KB |
Output is correct |
49 |
Execution timed out |
2077 ms |
19324 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |