Submission #859884

# Submission time Handle Problem Language Result Execution time Memory
859884 2023-10-11T02:21:54 Z iskhakkutbilim Village (BOI20_village) C++17
0 / 100
2 ms 6744 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 1e9 + 7;
const int N = 2e5;

int dp[N];
int n;
vector<int> g[N];
int ans;

void dfs(int v, int par){
	for(int to : g[v]){
		if(to != par) dfs(to, v);
		
	}
	int to = par;
	if(par != v && dp[v] == -1 && dp[to] == -1){
		dp[v] = to, dp[to] = v;
		ans+= 2;
	}
}

main(){
   ios::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);
	for(int i = 0;i <= N; i++) dp[i] = -1;
	cin >> n;
	for(int i = 0;i < n-1; i++){
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	
	dfs(1, 1);
	for(int i = 1;i <= n; i++){
		if(dp[i] == -1 or dp[i] == i){
			int to = g[i][0];
			dp[i] = i;
			swap(dp[to], dp[i]);
			ans+= 2;
		}
	}
	cout << ans << '\n';
	for(int i = 1;i <= n; i++) cout << dp[i] << ' ';
	return 0;
}

Compilation message

Village.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   28 | main(){
      | ^~~~
Village.cpp: In function 'int main()':
Village.cpp:31:35: warning: iteration 200000 invokes undefined behavior [-Waggressive-loop-optimizations]
   31 |  for(int i = 0;i <= N; i++) dp[i] = -1;
      |                             ~~~~~~^~~~
Village.cpp:31:18: note: within this loop
   31 |  for(int i = 0;i <= N; i++) dp[i] = -1;
      |                ~~^~~~
Village.cpp:31:35: warning: 'void* __builtin_memset(void*, int, long unsigned int)' forming offset [1600000, 1600007] is out of the bounds [0, 1600000] of object 'dp' with type 'long long int [200000]' [-Warray-bounds]
   31 |  for(int i = 0;i <= N; i++) dp[i] = -1;
      |                             ~~~~~~^~~~
Village.cpp:11:5: note: 'dp' declared here
   11 | int dp[N];
      |     ^~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6492 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6744 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6492 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -