Submission #524574

# Submission time Handle Problem Language Result Execution time Memory
524574 2022-02-09T14:47:33 Z Yazan_Alattar Cat in a tree (BOI17_catinatree) C++14
0 / 100
27 ms 47200 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 2000007;
const ll inf = 1e9;
const ll mod = 998244353;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vector < pair <int,int> > v;
vector <int> adj[M];
int n, d, p[M], mn[M], sz[M], add[M], dep[M], dist, ans;
bool vist[M];

void calc(int node, int par)
{
	sz[node] = 1;
	for(auto i : adj[node]) if(i != par && !vist[i]){
		calc(i, node);
		sz[node] += sz[i];
	}
	return;
}

int get(int node, int par, int half)
{
	++dist;
	for(auto i : adj[node]) if(!vist[i] && i != par && sz[i] > half) return get(i, node, half);
	return node;
}

void decompose(int node, int par)
{
	dist = 0;
	calc(node, 0);
	int cen = get(node, 0, sz[node] / 2);
	vist[cen] = 1;
	p[cen] = par;
	add[cen] = dist;
	for(auto i : adj[cen]) if(!vist[i]) decompose(i, cen);
	return;
}

void dfs(int node, int par)
{
	v.pb({dep[node], node});
	for(auto i : adj[node]) if(i != par){
		dep[i] = dep[node] + 1;
		dfs(i, node);
	}
	return;
}

int main()
{
	scanf("%d%d", &n, &d);
	for(int i = 2; i <= n; ++i){
		int a;
		scanf("%d", &a);
		adj[a + 1].pb(i);
		adj[i].pb(a + 1);
	}
	decompose(1, 0);
	dfs(1, 0);
	sort(all(v), greater < pair <int,int> > ());
	for(int i = 1; i <= n; ++i) mn[i] = inf;
	for(auto i : v){
		bool ok = 1;
		int cur = i.S;
		dist = 0;
		while(cur){
			if(mn[cur] + dist < d) ok = 0;
			dist += add[cur];
			cur = p[cur];
		}
		if(ok){
			++ans;
			cur = i.S; dist = 0;
			while(cur){
				mn[cur] = min(mn[cur], dist);
				dist += add[cur];
				cur = p[cur];
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}

Compilation message

catinatree.cpp: In function 'int main()':
catinatree.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  scanf("%d%d", &n, &d);
      |  ~~~~~^~~~~~~~~~~~~~~~
catinatree.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d", &a);
      |   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47200 KB Output is correct
2 Incorrect 27 ms 47176 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47200 KB Output is correct
2 Incorrect 27 ms 47176 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47200 KB Output is correct
2 Incorrect 27 ms 47176 KB Output isn't correct
3 Halted 0 ms 0 KB -