Submission #850674

# Submission time Handle Problem Language Result Execution time Memory
850674 2023-09-17T09:06:18 Z StefanSebez Cat Exercise (JOI23_ho_t4) C++14
0 / 100
4 ms 19960 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
const int N=2*1e5+50,lg=20;
map<pair<int,int>,int>mapa;
vector<int>E[N];
set<int>st[N];
ll dp[N];
int idx[N],parent[N][lg+1],depth[N];
bool was[N];
void DFScalc(int u,int j,int par,int d)
{
	depth[u]=d;
	parent[u][0]=par;
	for(auto i:E[u])
	{
		if(i!=parent[u][0])
		{
			DFScalc(i,j,u,d+1);
		}
	}
	parent[u][j]=parent[parent[u][j-1]][j-1];
}
int dist(int u,int v)
{
	int res=1;
	if(depth[u]<depth[v]) swap(u,v);
	int e=1<<(lg+1);
	for(int i=lg;i>=0;i--)
	{
		e/=2;
		//if(parent[u][i]==0) continue;
		if(depth[parent[u][i]]>=depth[v])
		{
			u=parent[u][i];
			res+=e;
		}
	}
	e=1<<(lg+1);
	if(u==v) return res;
	for(int i=lg;i>=0;i--)
	{
		e/=2;
		if(parent[u][i]==0 || parent[v][i]==0) continue;
		if(parent[u][i]!=parent[v][i])
		{
			res+=e*2;
			u=parent[u][i];
			v=parent[v][i];
		}
	}
	res++;
	return res;
}
/*pair<int,int> DFS(int u,int par,int depth)
{
	pair<int,int> x=make_pair(u,depth);
	for(auto i:E[u])
	{
		if(was[i] && i!=par)
		{
			pair<int,int>y=DFS(i,u,depth+1);
			if(x.fi<y.fi) x=y;
		}
	}
	return x;
}*/
int main()
{
    int n;scanf("%i",&n);
    int a[n+1];//L[n+1],R[n+1];
    //vector<int>mono;
    for(int i=1;i<=n;i++)
	{
		//L[i]=0,R[i]=n+1;
		idx[i]=i;
		scanf("%i",&a[i]);
		/*while(!mono.empty() && a[mono.back()]<a[i])
		{
			R[mono.back()]=i;
			mono.pop_back();
		}
		if(!mono.empty()) L[i]=mono.back();
		mono.push_back(i);*/
	}
	/*pair<int,int>par[n+1];
	for(int i=1;i<=n;i++)
	{
		mapa[{L[i],R[i]}]=i;
		par[i]=make_pair(R[i]-L[i],i);
	}
	sort(par+1,par+n+1);
	for(int i=1;i<=n;i++)
	{
		int j=par[i].se,maks=0,l=mapa[{L[j],j}],r=mapa[{j,R[j]}];
		if(j-L[j]>1)dp[j]=max(dp[l]+j-l,dp[j]);
		if(R[j]-j>1)dp[j]=max(dp[r]+r-j,dp[j]);
	}*/
	//bool bul=true;
    for(int i=1;i<n;i++)
	{
		int u,v;
		scanf("%i%i",&u,&v);
		E[a[u]].push_back(a[v]);
		E[a[v]].push_back(a[u]);
		//if(!(u==i && v==i+1))bul=false;
	}
	for(int i=1;i<=lg;i++)
	{
		DFScalc(n,i,0,0);
	}
	/*for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=3;j++)
		{
			printf("%i ",parent[i][j]);
		}
		printf("\n");
	}*/
	//printf("%i\n",dist(3,5));
	//if(bul) {printf("%lld\n",dp[mapa[{0,n+1}]]);return 0;}
	for(int i=1;i<=n;i++)
	{
		dp[i]=0;
		int ind=i,sz=0;
		for(auto u:E[i])
		{
			if(was[u])
			{
				if(st[idx[u]].size()>sz)
				{
					ind=u;
					sz=st[idx[u]].size();
				}
				ll maks=-*st[idx[u]].begin();
				ll x=dist(u,maks);
				dp[i]=max(dp[i],dp[maks]+x);
				//printf("%i %i %i: %i\n",u,maks,i,x);
				//pair<int,int> x=DFS(u,i,1);
				//dp[i]=max(dp[x.fi]+x.se,dp[i]);
			}
		}
		for(auto u:E[i])
		{
			if(was[u] && u!=ind)
			{
				while(!st[idx[u]].empty())
				{
					int x=*st[idx[u]].begin();
					st[idx[ind]].insert(x);
					idx[-x]=idx[ind];
					st[idx[u]].erase(x);
				}
			}
		}
		was[i]=true;
		st[idx[ind]].insert(-i);
		idx[i]=idx[ind];
		//printf("%i: %lld\n",i,dp[i]);
	}
	printf("%lld\n",dp[n]);
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:132:25: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  132 |     if(st[idx[u]].size()>sz)
      |        ~~~~~~~~~~~~~~~~~^~~
Main.cpp:72:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     int n;scanf("%i",&n);
      |           ~~~~~^~~~~~~~~
Main.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   scanf("%i",&a[i]);
      |   ~~~~~^~~~~~~~~~~~
Main.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%i%i",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Incorrect 4 ms 19536 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19544 KB Output is correct
2 Correct 3 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19960 KB Output is correct
5 Correct 4 ms 19544 KB Output is correct
6 Correct 4 ms 19544 KB Output is correct
7 Correct 4 ms 19548 KB Output is correct
8 Incorrect 4 ms 19544 KB Output isn't correct
9 Halted 0 ms 0 KB -