Submission #1102086

# Submission time Handle Problem Language Result Execution time Memory
1102086 2024-10-17T12:05:14 Z NDT134 Village (BOI20_village) C++14
25 / 100
561 ms 262144 KB
#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;

int main() {
	int n; cin >> n;
	vector<vector<int>> g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int x, y, z; cin >> x >> y; x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	vector<int> p(n), c(n);
	queue<int> q; q.push(0);
	while (!q.empty())
	{
		int x = q.front(); q.pop();
		for (int y : g[x])
		{
			if (p[x] != y)
			{
				p[y] = x;
				q.push(y);
				c[x]++;
			}
		}
	}

	for (int i = 0; i < n; i++)
	{
		if (!c[i])
		{
			q.push(i);
		}
	}
	vector<int> a(n), b(n), rm(n), cm(n);
	for (int i = 0; i < n; i++)
	{
		cm[i] = i;
	}
	vector<map<int,int>> m(n);
	while (!q.empty())
	{
		vector<int> v;
		int x = q.front(); q.pop();
		c[p[x]]--;
		if (!c[p[x]])
		{
			q.push(p[x]);
		}
		for (int y : g[x])
		{
			if (p[x] != y)
			{
				v.push_back(y);
			}
		}

		if (v.empty())
		{
			b[x] = 0;
			a[x] = -1;
			continue;
		}

		int k = 0, s = 0;
		vector<int> u;
		for (int y : v)
		{
			if (a[y] == -1)
			{
				k++;
				u.push_back(y);
			}
			else
			{
				s += a[y];
			}
		}

		int z = v[rand() % v.size()];
		//m[x] = m[z];
		cm[x] = cm[z];
		if (k > 0)
		{
			a[x] = s + 2 * k;
			b[x] = a[x];
			for (int y : v)
			{
				if (a[y] != -1 && y != z)
				{
					for (map<int, int>::const_iterator it = m[cm[y]].begin(); it != m[cm[y]].end(); ++it)
					{
						m[cm[x]][it->first] = it->second;
					}
				}
			}
			u.push_back(x);
			int l = u.size();
			for (int i = 0; i < l; i++)
			{
				m[cm[x]][u[i]] = u[(i + 1) % l];
			}
			rm[x] = u[u.size() - 2];
			continue;
		}
		
		b[x] = s;
		bool bb = 1;
		int t = -1;
		for (int y : v)
		{
			if (b[y] != a[y])
			{
				t = y;
			}
		}
		for (int y : v)
		{
			if (y != z)
			{
				for (map<int, int>::const_iterator it = m[cm[y]].begin(); it != m[cm[y]].end(); ++it)
				{
					m[cm[x]][it->first] = it->second;
				}
			}
		}
		a[x] = b[x] + 2 * (t == -1);
		if (t != -1)
		{
			m[cm[x]][rm[t]] = m[cm[t]][t];
			m[cm[x]][x] = t;
			m[cm[x]][t] = x;
			rm[x] = t;
			continue;
		}
		
		t = v[0];
		m[cm[x]][rm[t]] = x;
		m[cm[x]][x] = t;
		rm[x] = rm[t];
	}

	cout << a[0] << " " << 0 << endl;
	for (int i = 0; i < n; i++)
	{
		cout << m[cm[0]][i] + 1 << " ";
	}
	cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << 1 << " ";
	}
	cout << endl;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:12:13: warning: unused variable 'z' [-Wunused-variable]
   12 |   int x, y, z; cin >> x >> y; x--; y--;
      |             ^
Village.cpp:112:8: warning: unused variable 'bb' [-Wunused-variable]
  112 |   bool bb = 1;
      |        ^~
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 1 ms 336 KB Partially correct
3 Partially correct 1 ms 336 KB Partially correct
4 Partially correct 1 ms 336 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 336 KB Partially correct
7 Partially correct 1 ms 336 KB Partially correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 336 KB Partially correct
12 Partially correct 1 ms 336 KB Partially correct
13 Partially correct 1 ms 336 KB Partially correct
14 Partially correct 1 ms 336 KB Partially correct
15 Partially correct 1 ms 336 KB Partially correct
16 Partially correct 1 ms 336 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 1 ms 592 KB Partially correct
3 Partially correct 1 ms 760 KB Partially correct
4 Partially correct 3 ms 848 KB Partially correct
5 Partially correct 2 ms 848 KB Partially correct
6 Partially correct 2 ms 848 KB Partially correct
7 Partially correct 2 ms 592 KB Partially correct
8 Partially correct 3 ms 1104 KB Partially correct
9 Partially correct 3 ms 1104 KB Partially correct
10 Partially correct 7 ms 3664 KB Partially correct
11 Partially correct 7 ms 3152 KB Partially correct
12 Partially correct 1 ms 592 KB Partially correct
13 Partially correct 1 ms 592 KB Partially correct
14 Partially correct 2 ms 592 KB Partially correct
15 Partially correct 2 ms 592 KB Partially correct
16 Partially correct 2 ms 592 KB Partially correct
17 Partially correct 2 ms 592 KB Partially correct
18 Partially correct 1 ms 592 KB Partially correct
19 Partially correct 1 ms 592 KB Partially correct
20 Partially correct 2 ms 592 KB Partially correct
21 Partially correct 2 ms 592 KB Partially correct
22 Partially correct 1 ms 592 KB Partially correct
23 Partially correct 2 ms 592 KB Partially correct
24 Partially correct 2 ms 592 KB Partially correct
25 Partially correct 2 ms 592 KB Partially correct
26 Partially correct 2 ms 848 KB Partially correct
27 Partially correct 1 ms 592 KB Partially correct
28 Partially correct 2 ms 848 KB Partially correct
29 Partially correct 2 ms 592 KB Partially correct
30 Partially correct 1 ms 592 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 336 KB Partially correct
2 Partially correct 1 ms 336 KB Partially correct
3 Partially correct 1 ms 336 KB Partially correct
4 Partially correct 1 ms 336 KB Partially correct
5 Partially correct 1 ms 336 KB Partially correct
6 Partially correct 1 ms 336 KB Partially correct
7 Partially correct 1 ms 336 KB Partially correct
8 Partially correct 1 ms 336 KB Partially correct
9 Partially correct 1 ms 336 KB Partially correct
10 Partially correct 1 ms 336 KB Partially correct
11 Partially correct 1 ms 336 KB Partially correct
12 Partially correct 1 ms 336 KB Partially correct
13 Partially correct 1 ms 336 KB Partially correct
14 Partially correct 1 ms 336 KB Partially correct
15 Partially correct 1 ms 336 KB Partially correct
16 Partially correct 1 ms 336 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
18 Partially correct 1 ms 336 KB Partially correct
19 Partially correct 1 ms 592 KB Partially correct
20 Partially correct 1 ms 760 KB Partially correct
21 Partially correct 3 ms 848 KB Partially correct
22 Partially correct 2 ms 848 KB Partially correct
23 Partially correct 2 ms 848 KB Partially correct
24 Partially correct 2 ms 592 KB Partially correct
25 Partially correct 3 ms 1104 KB Partially correct
26 Partially correct 3 ms 1104 KB Partially correct
27 Partially correct 7 ms 3664 KB Partially correct
28 Partially correct 7 ms 3152 KB Partially correct
29 Partially correct 1 ms 592 KB Partially correct
30 Partially correct 1 ms 592 KB Partially correct
31 Partially correct 2 ms 592 KB Partially correct
32 Partially correct 2 ms 592 KB Partially correct
33 Partially correct 2 ms 592 KB Partially correct
34 Partially correct 2 ms 592 KB Partially correct
35 Partially correct 1 ms 592 KB Partially correct
36 Partially correct 1 ms 592 KB Partially correct
37 Partially correct 2 ms 592 KB Partially correct
38 Partially correct 2 ms 592 KB Partially correct
39 Partially correct 1 ms 592 KB Partially correct
40 Partially correct 2 ms 592 KB Partially correct
41 Partially correct 2 ms 592 KB Partially correct
42 Partially correct 2 ms 592 KB Partially correct
43 Partially correct 2 ms 848 KB Partially correct
44 Partially correct 1 ms 592 KB Partially correct
45 Partially correct 2 ms 848 KB Partially correct
46 Partially correct 2 ms 592 KB Partially correct
47 Partially correct 1 ms 592 KB Partially correct
48 Partially correct 231 ms 64108 KB Partially correct
49 Partially correct 336 ms 94408 KB Partially correct
50 Partially correct 366 ms 106824 KB Partially correct
51 Partially correct 220 ms 59464 KB Partially correct
52 Partially correct 299 ms 86864 KB Partially correct
53 Partially correct 246 ms 66260 KB Partially correct
54 Partially correct 56 ms 9544 KB Partially correct
55 Partially correct 151 ms 22856 KB Partially correct
56 Runtime error 561 ms 262144 KB Execution killed with signal 9
57 Halted 0 ms 0 KB -