Submission #946118

# Submission time Handle Problem Language Result Execution time Memory
946118 2024-03-14T10:35:04 Z pan Village (BOI20_village) C++17
0 / 100
2 ms 7260 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
ll n, x, y, minval, maxval = 0, label = -1;
vector<ll> adj[100005];
ll minc[100005], maxc[100005], size1[100005], depth[100005], in[100005], conv[100005];
void dfs1(ll p, ll from)
{
	size1[from] = 1;
	for (ll u: adj[from]) if(p!=u) {dfs1(from, u); size1[from]+=size1[u];}
	if (p!=-1 && minc[from]==from)
	{
		minc[p] = from;
		minc[from] = p;
	}
}

ll find_centroid(ll p, ll from)
{
	for (ll u: adj[from])
	{
		if (u==p) continue;
		if (size1[u]>n/2) return find_centroid(from, u);
	}
	return from;
}

void dfs2(ll p, ll from)
{
	in[from]= ++label;
	conv[label] = from;
	maxval += depth[from]*2;
	for (ll u: adj[from])
	{
		if (u==p) continue;
		depth[u] = depth[from]+1;
		dfs2(from, u);
	}
}
int main()
{
	input(n);
	for (ll i=1; i<=n; ++i) minc[i] = maxc[i] = i;
	for (ll i=0; i<n-1; ++i)
	{
		input2(x, y);
		adj[x].pb(y);
		adj[y].pb(x);
	}
	// Minimum:
	// ans = ((n+1)/2)*2
	minval = ((n+1)/2)*2;
	dfs1(-1, 1); 
	if (minc[1]==1) swap(minc[adj[1][0]], minc[1]); // odd n lead to 1 without paur
	
	
	// Maximum:
	// Find centroid, ensure that all ppl change to house that is out of its centroid subtree
	// i to be paired with (i+ (n)/2)%n
	// As all |subtree| < n/2, his ensure that both are from different subtree
	// This is optimal.
	ll centroid = find_centroid(-1, 1);
	depth[centroid] = 0;
	dfs2(-1, centroid);
	for (ll i=1; i<=n; ++i)
	{
		ll in1 = in[i];
		ll in2 = (in1 + n/2)%n;
		maxc[conv[in2]] = i;
	}
	// output
	cout << minval << ' ' << maxval << '\n';
	for (ll i=1; i<=n; ++i) print(minc[i], ' ');
	cout << '\n';
	//for (ll i=1; i<=n; ++i) print(maxc[i], ' ');
	
	return 0;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
Village.cpp:68:2: note: in expansion of macro 'input'
   68 |  input(n);
      |  ^~~~~
Village.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:72:3: note: in expansion of macro 'input2'
   72 |   input2(x, y);
      |   ^~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 7256 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 7260 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 7256 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -