Submission #427264

# Submission time Handle Problem Language Result Execution time Memory
427264 2021-06-14T13:51:12 Z model_code Counterspells (CPSPC17_counterspells) C++17
100 / 100
824 ms 37780 KB
#include <bits/stdc++.h>
using namespace std;

#define inf 1023456789
#define linf 1023456789123456789ll
#define pii pair<int,int>
#define pipii pair<int, pii >
#define pll pair<long long,long long>
#define vint vector<int>
#define vvint vector<vint >
#define ll long long
#define pdd pair<double, double>

#define DEBUG
#ifdef DEBUG
#define db(x) cerr << #x << " = " << x << endl
#else
#define db(x)
#endif

struct dd_path;

struct node
{
	node *parent, *heavy;
	vector<node*> sons;
	bool heavy_alive;
	bool black; //only applies to blocking/head nodes
	int maxdepth, depth;
	int black_light;
	dd_path* path;
	
	node()
	{
		parent = NULL;
		heavy = NULL;
		black = true;
		depth = 0;
		maxdepth = depth;
		black_light = 0;
		heavy_alive = false;
		path = NULL;
	}
	
	node(node* par)
	{
		parent = par;
		heavy = NULL;
		black = true;
		depth = parent->depth+1;
		maxdepth = depth;
		black_light = 0;
		heavy_alive = false;
		path = NULL;
		parent->sons.push_back(this);
	}
};

struct dd_path
{
	node* head;
	map<int, node*> blocking;
	
	bool get_color(node* n)
	{
		int d = n->depth;
		node *block = head;
		map<int, node*>::iterator it = blocking.lower_bound(-d);
		if(it != blocking.end())block = it->second;
		return (block->black) ^  ((n->depth - block->depth)%2);
	}
	
	int counter(node* whom)
	{
		bool color = get_color(whom);
		if(color)
		{
			return chain(whom);
		}
		return 0;
	}
	
	int counter_light(node* whom)
	{
		whom->black_light++;
		if(whom->black_light == 1 && whom->heavy_alive)
		{
			whom->heavy->black = get_color(whom->heavy);
			blocking[-whom->heavy->depth] = whom->heavy;
		}
		return counter(whom);
	}
	
	int counter_heavy(node* whom)
	{
		whom->heavy_alive = true;
		if(whom->black_light > 0)
		{
			blocking[-whom->heavy->depth] = whom->heavy;
		}
		return counter(whom);
	}
	
	int uncounter(node* whom)
	{
		bool black_heavy = 0;
		if(whom->heavy_alive) black_heavy = get_color(whom->heavy);
		whom->black_light--;
		if(whom->black_light == 0 && whom->heavy_alive)
		{
			blocking.erase(-whom->heavy->depth);
		}
		if(!black_heavy && (whom->black_light == 0))
		{
			return chain(whom);
		}
		return 0;
	}
	
	int chain(node* whom)
	{
		map<int, node*>::iterator it = blocking.lower_bound(-whom->depth);
		if(it != blocking.end())
		{
			it->second->black = !it->second->black;
			return whom->depth - it->second->depth + 1;
		}
		head->black = !head->black;
		int res = whom->depth - head->depth + 1;
		if(head->parent != NULL)
		{
			if(head->black)
			{
				res += head->parent->path->counter_light(head->parent);
			}
			else
			{
				res += head->parent->path->uncounter(head->parent);
			}
		}
		return res;
	}
	
	dd_path(node* h)
	{
		head = h;
	}
};

void dfs(node* cur)
{
	cur->maxdepth = cur->depth;
	for(int i=0; i<cur->sons.size(); i++)
	{
		dfs(cur->sons[i]);
		cur->maxdepth = max(cur->maxdepth, cur->sons[i]->maxdepth);
	}
}

void dd(node* n, dd_path* p, vector<dd_path*>& paths)
{
	if(p == NULL)
	{
		p = new dd_path(n);
		paths.push_back(p);
	}
	n->path = p;
	pair<int, node*> best(-1, NULL);
	for(int i=0; i<n->sons.size(); i++)
	{
		best = max(best, pair<int, node*>(n->sons[i]->maxdepth, n->sons[i]));
	}
	if(best.second != NULL) n->heavy = best.second;
	for(int i=0; i<n->sons.size(); i++)
	{
		if(n->sons[i] == best.second) dd(n->sons[i], p, paths);
		else dd(n->sons[i], NULL, paths);
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	vector<dd_path*> paths;
	vector<node*> nodes;
	nodes.push_back(new node);
	vint par(n+1, -1);
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &par[i]);
		nodes.push_back(new node(nodes[par[i]]));
	}
	dfs(nodes[0]);
	dd(nodes[0], NULL, paths);
	for(int i=1; i <= n; i++)
	{
		int res = 0;
		if(nodes[i]->path != nodes[i]->parent->path)
		{
			res = nodes[i]->parent->path->counter_light(nodes[i]->parent);
		}
		else
		{
			res = nodes[i]->path->counter_heavy(nodes[i]->parent);
		}
		printf("%d\n", res);
	}
	for(int i=0; i<=n; i++) delete nodes[i];
	for(int i=0; i<paths.size(); i++) delete paths[i];
	return 0;
}

Compilation message

Main.cpp: In function 'void dfs(node*)':
Main.cpp:153:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |  for(int i=0; i<cur->sons.size(); i++)
      |               ~^~~~~~~~~~~~~~~~~
Main.cpp: In function 'void dd(node*, dd_path*, std::vector<dd_path*>&)':
Main.cpp:169:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |  for(int i=0; i<n->sons.size(); i++)
      |               ~^~~~~~~~~~~~~~~
Main.cpp:174:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |  for(int i=0; i<n->sons.size(); i++)
      |               ~^~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:210:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<dd_path*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |  for(int i=0; i<paths.size(); i++) delete paths[i];
      |               ~^~~~~~~~~~~~~
Main.cpp:184:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
Main.cpp:191:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  191 |   scanf("%d", &par[i]);
      |   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 7 ms 1740 KB Output is correct
9 Correct 5 ms 2124 KB Output is correct
10 Correct 5 ms 1612 KB Output is correct
11 Correct 4 ms 1868 KB Output is correct
12 Correct 8 ms 1740 KB Output is correct
13 Correct 14 ms 1556 KB Output is correct
14 Correct 4 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 7 ms 1740 KB Output is correct
9 Correct 5 ms 2124 KB Output is correct
10 Correct 5 ms 1612 KB Output is correct
11 Correct 4 ms 1868 KB Output is correct
12 Correct 8 ms 1740 KB Output is correct
13 Correct 14 ms 1556 KB Output is correct
14 Correct 4 ms 1356 KB Output is correct
15 Correct 111 ms 15560 KB Output is correct
16 Correct 65 ms 18744 KB Output is correct
17 Correct 71 ms 14740 KB Output is correct
18 Correct 41 ms 17252 KB Output is correct
19 Correct 48 ms 14900 KB Output is correct
20 Correct 278 ms 12392 KB Output is correct
21 Correct 53 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 32140 KB Output is correct
2 Correct 145 ms 32308 KB Output is correct
3 Correct 71 ms 28312 KB Output is correct
4 Correct 119 ms 29352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 7 ms 1740 KB Output is correct
9 Correct 5 ms 2124 KB Output is correct
10 Correct 5 ms 1612 KB Output is correct
11 Correct 4 ms 1868 KB Output is correct
12 Correct 8 ms 1740 KB Output is correct
13 Correct 14 ms 1556 KB Output is correct
14 Correct 4 ms 1356 KB Output is correct
15 Correct 111 ms 15560 KB Output is correct
16 Correct 65 ms 18744 KB Output is correct
17 Correct 71 ms 14740 KB Output is correct
18 Correct 41 ms 17252 KB Output is correct
19 Correct 48 ms 14900 KB Output is correct
20 Correct 278 ms 12392 KB Output is correct
21 Correct 53 ms 14684 KB Output is correct
22 Correct 306 ms 32140 KB Output is correct
23 Correct 145 ms 32308 KB Output is correct
24 Correct 71 ms 28312 KB Output is correct
25 Correct 119 ms 29352 KB Output is correct
26 Correct 317 ms 32112 KB Output is correct
27 Correct 126 ms 37780 KB Output is correct
28 Correct 137 ms 33040 KB Output is correct
29 Correct 102 ms 34280 KB Output is correct
30 Correct 98 ms 29568 KB Output is correct
31 Correct 824 ms 25584 KB Output is correct
32 Correct 141 ms 32100 KB Output is correct