Submission #835072

# Submission time Handle Problem Language Result Execution time Memory
835072 2023-08-23T07:36:06 Z josanneo22 Cat Exercise (JOI23_ho_t4) C++17
0 / 100
4 ms 5204 KB
#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array

/* cat exercises:
*/
const int N=2e5+5000,K=22;
int n,h[N],f[N][K],dep[N],dp[N];
vector<int> g[N];
void setf(int u,int fa){
	f[u][0]=fa;
	for(int i=1;i<K;i++)
		f[u][i]=f[f[u][i-1]][i-1];
	return;
}
void dfs(int u,int fa){
	setf(u,fa); dep[u]=dep[fa]+1;
	for(auto&v:g[u]) if(v!=fa) dfs(v,u);
}
int lca(int x,int y){
	if (dep[x] < dep[y]) swap(x, y);
	for(int i=20;i>=0;i--) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
	if (x == y) return x;
	for(int i=20;i>=0;i--) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
	return f[x][0];
}

vector<int> par;
void init(int n){
	par.resize(n);
	for(int i=0;i<n;i++) par[i]=i;
}
int find(int x){
	if(par[x]==x) return x;
	else return par[x]=find(par[x]);
}

int dis(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)];}
void solve(){
	cin>>n;
	init(n+500);
	for(int i=1;i<=n;i++) cin>>h[i];
	for(int i=0;i<n-1;i++){
		int u,v; cin>>u>>v;
		u=h[u]; v=h[v];
		g[u].pb(v); g[v].pb(u);
	}
	dfs(1,0);
	for(int u=1;u<=n;u++){
		for(auto&v:g[u]){
			v=find(v);
			if(v<u){
				par[v]=u;
				dp[u]=max(dp[u],dp[v]+dis(u,v));
			}
		}
	}
	cout<<dp[n]<<'\n';
}
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	clock_t tStart = clock();
	int local=1,multi=0,debug=1,tt=1;
	if(local){
	    freopen("input.txt","r",stdin);
	    freopen("output.txt","w",stdout);
	}	
	if(multi) cin>>tt;
	for(int i=1;i<=tt;i++){
		if(debug && multi && local) cout<<"样例 "<<i<<'\n';
		solve();
 	}
	fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:73:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |      freopen("input.txt","r",stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:74:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |      freopen("output.txt","w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -