답안 #1089650

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089650 2024-09-16T20:30:25 Z raczek Village (BOI20_village) C++17
56 / 100
93 ms 23828 KB
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG 
auto& operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";}
auto& operator <<(auto& o, auto x) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X) cerr<<"["#X"]: "<<X<<endl;
#else
#define debug(X) {}
#endif
vector<int> MinSolve(vector<vector<int> > graph)
{
	int n = graph.size();
	vector<int> result(n+1);
	for(int i=0;i<n+1;i++) result[i] = i;
	vector<int> par(n+1);
	vector<int> deg(n+1);
	queue<int> q;
	function<void(int, int)> dfs = [&](int a, int fat)
	{
		par[a] = fat;
		for(auto v : graph[a])
			if(fat != v)
				dfs(v, a);
		deg[a] = graph[a].size();
		if(a != 1 && graph[a].size() == 1) q.push(a);
	};
	dfs(1, -1);
	while(!q.empty())
	{
		int a = q.front();
		q.pop();
		if(a == 1) continue;
		if(result[a] == a)
			swap(result[a], result[par[a]]), result[0]+=2;
		deg[par[a]]--;
		if(deg[par[a]] == 1) q.push(par[a]);
	}
	if(result[1] == 1)
	{
		result[0] += 2;
		swap(result[graph[1][0]], result[1]);
	}
	return result;
}
vector<int> MaxSolve(vector<vector<int> > graph)
{
	int n = graph.size()-1;
	vector<int> Sz(n+1);
	vector<int> result(n+1);
	function<void(int, int)> getSz = [&](int a, int par)
	{
		Sz[a] = 1;
		for(auto v : graph[a])
			if(v != par)
			{
				getSz(v, a);
				Sz[a] += Sz[v];
				result[0] += Sz[v] * 2;
			}
	};
	getSz(1, -1);
	debug(Sz);
	debug(n/2);
	function<int(int, int)> getCtr = [&](int a, int par)
	{
		debug(a);
		for(auto v : graph[a])
			if(v != par)
				if(Sz[v] > n/2)
					return getCtr(v, a);
		return a;
	};
	int ctr = getCtr(1, -1);
	debug(ctr);
	result[0] = 0;
	getSz(ctr, -1);
	vector<vector<int> > grp;
	function<void(int, int)> dfs = [&](int a, int par)
	{
		grp.back().push_back(a);
		for(auto v : graph[a])
			if(v != par)
				dfs(v, a);
	};
	for(auto v : graph[ctr])
	{
		grp.push_back({});
		dfs(v, ctr);
	}
	debug(grp);
	sort(grp.begin(), grp.end(),[&](vector<int> v1,vector<int> v2){return v1.size()>v2.size();});
	grp.push_back({ctr});
	debug(grp);
	int pot = grp[0][0];
	for(int i=1;i<=n;i++) result[i] = i;
	for(int i=0;i<grp.size()-1;i++)
	{
		for(int j=i+1;j<grp.size() && grp[i].size();j++)
			while(grp[i].size() && grp[j].size())
			{
				result[grp[i].back()] = grp[j].back();
				result[grp[j].back()] = grp[i].back();
				grp[i].pop_back();
				grp[j].pop_back();
			}
	}
	int x = 0;
	for(int i=0;i<grp.size();i++) if(grp[i].size() > 0) x = i;
	if(grp[x].size() > 0) swap(result[pot], result[grp[x][0]]);
	return result;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	vector<vector<int> > graph(n+1);
	for(int i=0;i<n-1;i++)
	{
		int a, b;
		cin>>a>>b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	vector<int> minn = MinSolve(graph);
	vector<int> maxx = MaxSolve(graph);
	cout<<minn[0]<<" "<<maxx[0]<<"\n";
	for(int i=1;i<=n;i++) cout<<minn[i]<<" ";
	cout<<"\n";
	for(int i=1;i<=n;i++) cout<<maxx[i]<<" ";
}

Compilation message

Village.cpp: In function 'std::vector<int> MaxSolve(std::vector<std::vector<int> >)':
Village.cpp:96:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for(int i=0;i<grp.size()-1;i++)
      |              ~^~~~~~~~~~~~~
Village.cpp:98:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for(int j=i+1;j<grp.size() && grp[i].size();j++)
      |                 ~^~~~~~~~~~~
Village.cpp:108:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |  for(int i=0;i<grp.size();i++) if(grp[i].size() > 0) x = i;
      |              ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 0 ms 348 KB Partially correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Partially correct 1 ms 348 KB Partially correct
5 Partially correct 1 ms 604 KB Partially correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Partially correct 1 ms 348 KB Partially correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Partially correct 1 ms 604 KB Partially correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Partially correct 1 ms 348 KB Partially correct
27 Correct 0 ms 348 KB Output is correct
28 Partially correct 1 ms 348 KB Partially correct
29 Partially correct 1 ms 604 KB Partially correct
30 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Partially correct 0 ms 348 KB Partially correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Partially correct 1 ms 348 KB Partially correct
22 Partially correct 1 ms 604 KB Partially correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Partially correct 1 ms 348 KB Partially correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Partially correct 1 ms 604 KB Partially correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Partially correct 1 ms 348 KB Partially correct
44 Correct 0 ms 348 KB Output is correct
45 Partially correct 1 ms 348 KB Partially correct
46 Partially correct 1 ms 604 KB Partially correct
47 Correct 1 ms 604 KB Output is correct
48 Partially correct 60 ms 13556 KB Partially correct
49 Correct 61 ms 14792 KB Output is correct
50 Correct 59 ms 14720 KB Output is correct
51 Correct 45 ms 11476 KB Output is correct
52 Correct 58 ms 14396 KB Output is correct
53 Partially correct 52 ms 13124 KB Partially correct
54 Correct 36 ms 11572 KB Output is correct
55 Partially correct 73 ms 23828 KB Partially correct
56 Partially correct 69 ms 18940 KB Partially correct
57 Correct 71 ms 17160 KB Output is correct
58 Correct 65 ms 16156 KB Output is correct
59 Partially correct 62 ms 14872 KB Partially correct
60 Correct 93 ms 19208 KB Output is correct
61 Correct 56 ms 16132 KB Output is correct
62 Correct 50 ms 15384 KB Output is correct
63 Correct 48 ms 14704 KB Output is correct
64 Correct 60 ms 15060 KB Output is correct
65 Correct 50 ms 15496 KB Output is correct
66 Correct 49 ms 14552 KB Output is correct
67 Correct 46 ms 11936 KB Output is correct
68 Correct 42 ms 12896 KB Output is correct
69 Correct 49 ms 15384 KB Output is correct
70 Correct 48 ms 14356 KB Output is correct
71 Correct 34 ms 10868 KB Output is correct
72 Correct 43 ms 12152 KB Output is correct
73 Correct 55 ms 15384 KB Output is correct
74 Correct 44 ms 14112 KB Output is correct
75 Partially correct 56 ms 14616 KB Partially correct
76 Partially correct 57 ms 14380 KB Partially correct
77 Partially correct 58 ms 14356 KB Partially correct
78 Partially correct 34 ms 10024 KB Partially correct
79 Partially correct 41 ms 11732 KB Partially correct
80 Partially correct 65 ms 14616 KB Partially correct
81 Partially correct 50 ms 14296 KB Partially correct
82 Partially correct 49 ms 14900 KB Partially correct