답안 #850693

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850693 2023-09-17T09:41:04 Z StefanSebez Cat Exercise (JOI23_ho_t4) C++14
0 / 100
4 ms 21100 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
const ll N=2*1e5+50,lg=20;
//map<pair<int,int>,int>mapa;
vector<ll>E[N];
set<ll>st[N];
ll dp[N];
ll idx[N],parent[N][lg+10],depth[N];
bool was[N];
void DFScalc(ll u,ll j,ll par,ll d)
{
	depth[u]=d;
	parent[u][0]=par;
	for(auto i:E[u])
	{
		if(i!=par)
		{
			DFScalc(i,j,u,d+1);
		}
	}
	parent[u][j]=parent[parent[u][j-1]][j-1];
}
ll dist(ll u,ll v)
{
	ll res=1;
	if(depth[u]<depth[v]) swap(u,v);
	ll e=1<<(lg+1);
	for(ll 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(ll 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+=2;
	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()
{
    ll n;scanf("%lld",&n);
    ll a[n+1];//L[n+1],R[n+1];
    //vector<int>mono;
    for(ll i=1;i<=n;i++)
	{
		//L[i]=0,R[i]=n+1;
		idx[i]=i;
		scanf("%lld",&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(ll i=1;i<n;i++)
	{
		ll u,v;
		scanf("%lld%lld",&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(ll 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("%lld ",parent[i][j]);
		}
		printf("\n");
	}*/
	//printf("%i\n",dist(3,5));
	//if(bul) {printf("%lld\n",dp[mapa[{0,n+1}]]);return 0;}
	for(ll i=1;i<=n;i++)
	{
		dp[i]=0;
		ll 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 %lld %i: %lld\n",u,maks,i,x);
				//pair<int,int> x=DFS(u,i,1);
				//dp[i]=max(dp[x.fi]+x.se,dp[i]);
			}
		}
		//printf("%lld: %lld %lld\n",i,ind,sz);
		for(auto u:E[i])
		{
			if(was[u] && u!=ind)
			{
				while(!st[idx[u]].empty())
				{
					ll 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]);
	/*for(int i=1;i<=n;i++)
	{
		printf("%lld ",dp[i]);
	}*/
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:132:25: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  132 |     if(st[idx[u]].size()>sz)
      |        ~~~~~~~~~~~~~~~~~^~~
Main.cpp:72:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     ll n;scanf("%lld",&n);
      |          ~~~~~^~~~~~~~~~~
Main.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   scanf("%lld",&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("%lld%lld",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Incorrect 4 ms 21100 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21088 KB Output is correct
6 Correct 4 ms 20824 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Incorrect 4 ms 21084 KB Output isn't correct
9 Halted 0 ms 0 KB -