답안 #367732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
367732 2021-02-18T07:08:47 Z arnold518 Pastiri (COI20_pastiri) C++14
0 / 100
1000 ms 118764 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 t=half(V[0], V[1]);
		if(t==-1)
		{
			chk[i]=0;
			continue;
		}
		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];
	if(ans==INF) ans=-1;
	printf("%d\n", ans);
	if(ans!=-1)
	{
		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: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]);
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 366 ms 118508 KB Output is correct
2 Execution timed out 1077 ms 118764 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 13264 KB Output is correct
2 Correct 77 ms 13036 KB Output is correct
3 Incorrect 745 ms 95380 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 12524 KB Integer -1 violates the range [1, 2000]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1081 ms 95980 KB Time limit exceeded
2 Halted 0 ms 0 KB -