Submission #785458

# Submission time Handle Problem Language Result Execution time Memory
785458 2023-07-17T09:17:04 Z KLPP Cat Exercise (JOI23_ho_t4) C++14
100 / 100
369 ms 49116 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
const int Mx=1000000;
int arr[Mx];
class DSU{
	public:
	int sz[Mx];
	int par[Mx];
	int cmp[Mx];

	void init(int n){
		rep(i,0,n)sz[i]=1,par[i]=i,cmp[i]=i;
	}
	int root(int x){
		if(par[x]==x)return x;
		par[x]=root(par[x]);
		return par[x];
	}
	void merge(int a, int b){
		a=root(a);
		b=root(b);
		if(a==b)return;
		if(sz[a]<sz[b])swap(a,b);
		sz[a]+=sz[b];
		par[b]=a;
		if(arr[cmp[a]]<arr[cmp[b]]){
			cmp[a]=cmp[b];
		}
	}
};
lld ans[Mx];
DSU D;
pair<int,int> p[Mx];


struct LCA {
  const int N, LOG;
  vector<int> depth;
  vector< vector<int> > dp;
  LCA(vector< vector<int> > &g) : N( (int)g.size() ), LOG( (int) log2(N)+1) {
    dp = vector< vector<int> > (N, vector<int> (LOG, -1) );
    depth = vector<int> (N, 0);
    dfs (g, 0);
    for (int j = 1; j < LOG; j++) {
      for (int i = 0; i < N; i++)
        if (dp[i][j - 1] != -1) dp[i][j] = dp[ dp[i][j - 1] ][j - 1];
    }
  }
  void dfs (vector< vector<int> > &g, int node, int p = -1) {
    dp[node][0] = p;
    for (auto to : g[node])
      if (to != p) { depth[to] = depth[node] + 1; dfs (g, to, node); }
  }
  // Edge weight = 1, otherwise change depth by sum till root
  int dist (int u, int v) { return depth[u] + depth[v] - 2 * depth[lca (u, v)]; }
  int kth (int u, int diff) {
    for (int i = LOG - 1; i >= 0; i--) if ((diff >> i) & 1) u = dp[u][i];
    return u;
  }
  int lca (int u, int v) {
    if (depth[u] < depth[v]) swap (u, v);
    u = kth (u, depth[u] - depth[v]);
    if (u == v) return u;
    for (int i = LOG - 1; i >= 0; i--)
      if (dp[u][i] != dp[v][i]) u = dp[u][i], v = dp[v][i];
    return dp[u][0];
  }
};

vector<vector<int> > nei;
void solve(){
	int n;
	cin>>n;
	rep(i,0,n)cin>>arr[i];
	rep(i,0,n)ans[i]=-100000000;
	D.init(n);
	nei.resize(n);
	rep(i,0,n-1){
		int x,y;
		cin>>x>>y;
		x--;y--;
		nei[x].push_back(y);
		nei[y].push_back(x);
	}
	LCA L(nei);
	rep(i,0,n)p[i]={arr[i],i};
	sort(p,p+n);
	lld fin=0;
	rep(i,0,n){
		int idx=p[i].second;
		ans[idx]=0;
		trav(a,nei[idx]){
			if(arr[a]<arr[idx]){
				int rep=D.cmp[D.root(a)];
				//cout<<idx<<" "<<rep<<" "<<L.dist(idx,rep)<<endl;
				ans[idx]=max(ans[idx],ans[rep]+L.dist(idx,rep));
			}
		}
		trav(a,nei[idx]){
			if(arr[a]<arr[idx]){
				D.merge(a,idx);
			}
		}
		fin=ans[idx];
	}
	cout<<fin<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	//cin>>tt;
	while(tt--){
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1368 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 3 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 3 ms 1364 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1368 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 3 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 3 ms 1364 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 1364 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 5 ms 1272 KB Output is correct
29 Correct 3 ms 1364 KB Output is correct
30 Correct 4 ms 1236 KB Output is correct
31 Correct 3 ms 1240 KB Output is correct
32 Correct 4 ms 1320 KB Output is correct
33 Correct 3 ms 1240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1368 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 3 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 3 ms 1364 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 136 ms 49108 KB Output is correct
26 Correct 136 ms 49040 KB Output is correct
27 Correct 127 ms 49012 KB Output is correct
28 Correct 200 ms 49112 KB Output is correct
29 Correct 194 ms 49052 KB Output is correct
30 Correct 206 ms 49116 KB Output is correct
31 Correct 221 ms 49116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 207 ms 42396 KB Output is correct
4 Correct 236 ms 42312 KB Output is correct
5 Correct 227 ms 42388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1368 KB Output is correct
19 Correct 3 ms 1364 KB Output is correct
20 Correct 3 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 3 ms 1364 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 3 ms 1364 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 1364 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 5 ms 1272 KB Output is correct
29 Correct 3 ms 1364 KB Output is correct
30 Correct 4 ms 1236 KB Output is correct
31 Correct 3 ms 1240 KB Output is correct
32 Correct 4 ms 1320 KB Output is correct
33 Correct 3 ms 1240 KB Output is correct
34 Correct 136 ms 49108 KB Output is correct
35 Correct 136 ms 49040 KB Output is correct
36 Correct 127 ms 49012 KB Output is correct
37 Correct 200 ms 49112 KB Output is correct
38 Correct 194 ms 49052 KB Output is correct
39 Correct 206 ms 49116 KB Output is correct
40 Correct 221 ms 49116 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 207 ms 42396 KB Output is correct
44 Correct 236 ms 42312 KB Output is correct
45 Correct 227 ms 42388 KB Output is correct
46 Correct 369 ms 46156 KB Output is correct
47 Correct 349 ms 45548 KB Output is correct
48 Correct 324 ms 46416 KB Output is correct
49 Correct 345 ms 45524 KB Output is correct
50 Correct 295 ms 42608 KB Output is correct
51 Correct 308 ms 42600 KB Output is correct
52 Correct 285 ms 42708 KB Output is correct
53 Correct 281 ms 42608 KB Output is correct