제출 #575310

#제출 시각아이디문제언어결과실행 시간메모리
575310ktkeremBall Machine (BOI13_ballmachine)C++17
컴파일 에러
0 ms0 KiB
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <utility>
using namespace std;

// full solution
// (author: simon buerger)

void readTree(int n, vector<int>& nodes)
{
	nodes.resize(n);
	for(int i = 0; i < n; ++i)
	{
		scanf("%d", &nodes[i]);
		nodes[i]--;
		if(nodes[i] == -1)
			nodes[i] = i;
	}
}

#define LOGN 17	// will work for n <= 100.000
int n, q;
vector<int> par[LOGN];
vector<vector<int> > g;
vector<int> low;
vector<int> post;
vector<bool> ball;
vector<int> layer;	// root is at 0
int root;
bool cmpLow(int a, int b) { return low[a] < low[b]; }
bool cmpPostInv(int a, int b) { return post[a] > post[b]; }
priority_queue<int, vector<int>, bool(*)(int,int)> empty(cmpPostInv);
vector<int> full;	// not ordered in any sense

void dfsLow(int v)
{
	if(v == root)
		layer[v] = 0;
	else
		layer[v] = 1 + layer[par[0][v]];

	low[v] = v;
	for(int i = 0; i < (int)g[v].size(); ++i)
	{
		int w = g[v][i];
		dfsLow(w);
		low[v] = min(low[v], low[w]);
	}
}

void dfsPost(int v)
{
	static int time = 0;
	for(int i = 0; i < (int)g[v].size(); ++i)
		dfsPost(g[v][i]);
	post[v] = time++;
}

int add()	// return pos
{
	int k = empty.top();
	empty.pop();
	ball[k] = true;
	return k;
}

int remove(int pos)	// return position of actual remove
{
	if(!ball[pos])
		cerr << "ERROR: remove from empty node" << endl;
	//cerr << "remove " << pos;
	for(int level = LOGN-1; level >= 0; --level)
		if(ball[par[level][pos]])
			pos = par[level][pos];

	ball[pos] = false;
	empty.push(pos);
	//cerr << " actual = " << pos << endl;
	return pos;
}

int main(int argc, char** argv)
{
	scanf("%d %d", &n, &q);

	g.resize(n);
	for(int i = 0; i < LOGN; ++i)
		par[i].resize(n);
	ball.resize(n, false);
	low.resize(n);
	post.resize(n);
	layer.resize(n);

	readTree(n, par[0]);

	for(int i = 0; i < n; ++i)
		if(par[0][i] == i)
			root = i;
		else
			g[par[0][i]].push_back(i);

	par[0][root] = root;
	for(int level = 1; level < LOGN; ++level)
		for(int i = 0; i < n; ++i)
			par[level][i] = par[level-1][par[level-1][i]];

	dfsLow(root);
	for(int i = 0; i < n; ++i)
		sort(g[i].begin(), g[i].end(), cmpLow);

	dfsPost(root);
	for(int i = 0; i < n; ++i)
		empty.push(i);

	for(int i = 0; i < q; ++i)
	{
		int t, k;
		scanf("%d %d", &t, &k);

		if(t == 1)	// add k balls
		{
			for(int j = 0; j < k; ++j)
			{
				int pos = add();
				if(j+1 == k)
					printf("%d\n", pos+1);
			}
		}
		else if(t == 2)	// remove one ball from position k
		{
			--k;
			int actual = remove(k);
			printf("%d\n", layer[k]-layer[actual]);
		}
		else cerr << "ERROR: invalid t" << endl;
	}
}

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int add()':
ballmachine.cpp:65:10: error: reference to 'empty' is ambiguous
   65 |  int k = empty.top();
      |          ^~~~~
In file included from /usr/include/c++/10/vector:69,
                 from ballmachine.cpp:2:
/usr/include/c++/10/bits/range_access.h:281:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(std::initializer_list<_Tp>)'
  281 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:272:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  272 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:263:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  263 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
ballmachine.cpp:36:52: note:                 'std::priority_queue<int, std::vector<int>, bool (*)(int, int)> empty'
   36 | priority_queue<int, vector<int>, bool(*)(int,int)> empty(cmpPostInv);
      |                                                    ^~~~~
ballmachine.cpp:66:2: error: reference to 'empty' is ambiguous
   66 |  empty.pop();
      |  ^~~~~
In file included from /usr/include/c++/10/vector:69,
                 from ballmachine.cpp:2:
/usr/include/c++/10/bits/range_access.h:281:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(std::initializer_list<_Tp>)'
  281 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:272:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  272 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:263:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  263 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
ballmachine.cpp:36:52: note:                 'std::priority_queue<int, std::vector<int>, bool (*)(int, int)> empty'
   36 | priority_queue<int, vector<int>, bool(*)(int,int)> empty(cmpPostInv);
      |                                                    ^~~~~
ballmachine.cpp: In function 'int remove(int)':
ballmachine.cpp:81:2: error: reference to 'empty' is ambiguous
   81 |  empty.push(pos);
      |  ^~~~~
In file included from /usr/include/c++/10/vector:69,
                 from ballmachine.cpp:2:
/usr/include/c++/10/bits/range_access.h:281:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(std::initializer_list<_Tp>)'
  281 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:272:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  272 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:263:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  263 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
ballmachine.cpp:36:52: note:                 'std::priority_queue<int, std::vector<int>, bool (*)(int, int)> empty'
   36 | priority_queue<int, vector<int>, bool(*)(int,int)> empty(cmpPostInv);
      |                                                    ^~~~~
ballmachine.cpp: In function 'int main(int, char**)':
ballmachine.cpp:117:3: error: reference to 'empty' is ambiguous
  117 |   empty.push(i);
      |   ^~~~~
In file included from /usr/include/c++/10/vector:69,
                 from ballmachine.cpp:2:
/usr/include/c++/10/bits/range_access.h:281:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(std::initializer_list<_Tp>)'
  281 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:272:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  272 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/10/bits/range_access.h:263:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  263 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
ballmachine.cpp:36:52: note:                 'std::priority_queue<int, std::vector<int>, bool (*)(int, int)> empty'
   36 | priority_queue<int, vector<int>, bool(*)(int,int)> empty(cmpPostInv);
      |                                                    ^~~~~
ballmachine.cpp: In function 'void readTree(int, std::vector<int>&)':
ballmachine.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf("%d", &nodes[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main(int, char**)':
ballmachine.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:122:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |   scanf("%d %d", &t, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~