Submission #1102058

# Submission time Handle Problem Language Result Execution time Memory
1102058 2024-10-17T11:21:22 Z NDT134 Village (BOI20_village) C++14
25 / 100
398 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);
	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];
		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[y].begin(); it != m[y].end(); ++it)
					{
						m[x][it->first] = it->second;
					}
				}
			}
			u.push_back(x);
			int l = u.size();
			for (int i = 0; i < l; i++)
			{
				m[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[y].begin(); it != m[y].end(); ++it)
				{
					m[x][it->first] = it->second;
				}
			}
		}
		a[x] = b[x] + 2 * (t == -1);
		if (t != -1)
		{
			m[x][rm[t]] = m[t][t];
			m[x][x] = t;
			m[x][t] = x;
			rm[x] = t;
			continue;
		}
		
		t = v[0];
		m[x][rm[t]] = x;
		m[x][x] = t;
		rm[x] = rm[t];
	}

	cout << a[0] << " " << 0 << endl;
	for (int i = 0; i < n; i++)
	{
		cout << m[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:107:8: warning: unused variable 'bb' [-Wunused-variable]
  107 |   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 508 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 508 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 2 ms 336 KB Partially correct
2 Partially correct 2 ms 592 KB Partially correct
3 Partially correct 2 ms 592 KB Partially correct
4 Partially correct 3 ms 1104 KB Partially correct
5 Partially correct 2 ms 852 KB Partially correct
6 Partially correct 3 ms 956 KB Partially correct
7 Partially correct 18 ms 13560 KB Partially correct
8 Partially correct 5 ms 2900 KB Partially correct
9 Partially correct 4 ms 2132 KB Partially correct
10 Partially correct 13 ms 6740 KB Partially correct
11 Partially correct 10 ms 5716 KB Partially correct
12 Partially correct 2 ms 596 KB Partially correct
13 Partially correct 2 ms 596 KB Partially correct
14 Partially correct 2 ms 700 KB Partially correct
15 Partially correct 2 ms 596 KB Partially correct
16 Partially correct 2 ms 644 KB Partially correct
17 Partially correct 2 ms 852 KB Partially correct
18 Partially correct 2 ms 596 KB Partially correct
19 Partially correct 1 ms 596 KB Partially correct
20 Partially correct 2 ms 596 KB Partially correct
21 Partially correct 2 ms 596 KB Partially correct
22 Partially correct 2 ms 596 KB Partially correct
23 Partially correct 2 ms 852 KB Partially correct
24 Partially correct 3 ms 864 KB Partially correct
25 Partially correct 2 ms 596 KB Partially correct
26 Partially correct 3 ms 1108 KB Partially correct
27 Partially correct 2 ms 596 KB Partially correct
28 Partially correct 2 ms 852 KB Partially correct
29 Partially correct 2 ms 852 KB Partially correct
30 Partially correct 3 ms 596 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 508 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 508 KB Partially correct
16 Partially correct 1 ms 336 KB Partially correct
17 Partially correct 1 ms 336 KB Partially correct
18 Partially correct 2 ms 336 KB Partially correct
19 Partially correct 2 ms 592 KB Partially correct
20 Partially correct 2 ms 592 KB Partially correct
21 Partially correct 3 ms 1104 KB Partially correct
22 Partially correct 2 ms 852 KB Partially correct
23 Partially correct 3 ms 956 KB Partially correct
24 Partially correct 18 ms 13560 KB Partially correct
25 Partially correct 5 ms 2900 KB Partially correct
26 Partially correct 4 ms 2132 KB Partially correct
27 Partially correct 13 ms 6740 KB Partially correct
28 Partially correct 10 ms 5716 KB Partially correct
29 Partially correct 2 ms 596 KB Partially correct
30 Partially correct 2 ms 596 KB Partially correct
31 Partially correct 2 ms 700 KB Partially correct
32 Partially correct 2 ms 596 KB Partially correct
33 Partially correct 2 ms 644 KB Partially correct
34 Partially correct 2 ms 852 KB Partially correct
35 Partially correct 2 ms 596 KB Partially correct
36 Partially correct 1 ms 596 KB Partially correct
37 Partially correct 2 ms 596 KB Partially correct
38 Partially correct 2 ms 596 KB Partially correct
39 Partially correct 2 ms 596 KB Partially correct
40 Partially correct 2 ms 852 KB Partially correct
41 Partially correct 3 ms 864 KB Partially correct
42 Partially correct 2 ms 596 KB Partially correct
43 Partially correct 3 ms 1108 KB Partially correct
44 Partially correct 2 ms 596 KB Partially correct
45 Partially correct 2 ms 852 KB Partially correct
46 Partially correct 2 ms 852 KB Partially correct
47 Partially correct 3 ms 596 KB Partially correct
48 Partially correct 263 ms 69332 KB Partially correct
49 Partially correct 391 ms 115796 KB Partially correct
50 Partially correct 398 ms 122700 KB Partially correct
51 Partially correct 271 ms 84556 KB Partially correct
52 Partially correct 332 ms 97100 KB Partially correct
53 Partially correct 283 ms 83020 KB Partially correct
54 Runtime error 290 ms 262144 KB Execution killed with signal 9
55 Halted 0 ms 0 KB -