Submission #367733

# Submission time Handle Problem Language Result Execution time Memory
367733 2021-02-18T07:18:32 Z arnold518 Pastiri (COI20_pastiri) C++14
0 / 100
1000 ms 111852 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 5e5;
const int MAXK = 15;
const int INF = 987654321;

int N, K;
vector<int> adj[MAXN+10];

int par[MAXN+10][30], dep[MAXN+10];
int P[MAXK+10];

void dfs(int now, int bef, int d)
{
	par[now][0]=bef;
	dep[now]=d;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
	}
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	return par[u][0];
}

int dist(int u, int v)
{
	int w=lca(u, v);
	return dep[u]+dep[v]-dep[w]-dep[w];
}

int half(int u, int v)
{
	int w=lca(u, v);
	int d=dep[u]+dep[v]-dep[w]-dep[w];	
	if(d%2) return -1;
	d/=2;
	if(dep[u]-dep[w]>=d)
	{
		for(int i=20; i>=0; i--) if(d&(1<<i)) u=par[u][i];
		return u;
	}
	else
	{
		for(int i=20; i>=0; i--) if(d&(1<<i)) v=par[v][i];	
		return v;
	}
}

int chk[(1<<MAXK)+10], dp[(1<<MAXK)+10], memo[(1<<MAXK)+10];

int main()
{
	scanf("%d%d", &N, &K);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=0; i<K; i++)
	{
		scanf("%d", &P[i]);
	}

	dfs(1, 1, 1);
	for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];

	for(int i=1; i<(1<<K); i++)
	{
		vector<int> V;
		for(int j=0; j<K; j++)
		{
			if(i&(1<<j)) V.push_back(P[j]);
		}
		if(V.size()==1)
		{
			chk[i]=1;
			continue;
		}

		int u=V[0], v=V[0], dd=0;
		bool flag=true;
		for(int p=0; p<V.size(); p++)
		{
			for(int q=0; q<V.size(); q++)
			{
				int d=dist(V[p], V[q]);
				if(d%2) flag=false;
				if(d>dd)
				{
					u=V[p]; v=V[q];
					dd=d;
				}
			}
		}
		if(!flag)
		{
			chk[i]=0;
			continue;
		}
		int t=half(u, v);
		int d=dist(t, V[0]);
		chk[i]=1;
		for(int j=0; j<K; j++)
		{	
			if(i&(1<<j))
			{
				if(d!=dist(P[j], t)) chk[i]=0;
			}
			else
			{
				if(d>dist(P[j], t)) chk[i]=0;
			}
		}
	}

	for(int i=0; i<(1<<K); i++)
	{
		if(i==0) continue;
		dp[i]=INF;
		for(int j=i; j; j=i&(j-1))
		{
			if(chk[j])
			{
				if(dp[i]>dp[i^j]+1)
				{
					dp[i]=dp[i^j]+1;
					memo[i]=i^j;
				}
			}
		}
	}
	int ans=dp[(1<<K)-1];
	printf("%d\n", ans);
	int t=(1<<K)-1;
	while(t)
	{
		int x=(t^memo[t]);
		//printf("!!%d\n", x);
		vector<int> V;
		for(int i=0; i<K; i++) if(x&(1<<i)) V.push_back(P[i]);
		if(V.size()==1) printf("%d ", V[0]);
		else printf("%d ", half(V[0], V[1]));
		t=memo[t];
	}
	printf("\n");
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:97:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for(int p=0; p<V.size(); p++)
      |                ~^~~~~~~~~
pastiri.cpp:99:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |    for(int q=0; q<V.size(); q++)
      |                 ~^~~~~~~~~
pastiri.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 350 ms 111852 KB Output is correct
2 Execution timed out 1099 ms 111852 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 565 ms 13164 KB Sheep 3030 not protected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 55 ms 12396 KB Integer 987654321 violates the range [1, 2000]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 89132 KB Time limit exceeded
2 Halted 0 ms 0 KB -