답안 #478383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
478383 2021-10-07T06:50:20 Z pluto_ish Village (BOI20_village) C++14
0 / 100
1 ms 336 KB
#include <bits/stdc++.h>
 
#define pb push_back
#define ff first
#define ss second
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
const ll INF = (ll)1<<62;
const ll MOD = 1e9+7;
const int iINF = 1<<30;
const double PI = 3.14159265359;

int ans;
vector<int> to, revto;
vector<vector<int> > adj;

int dfs(int u, int p){
  vector<int> tmp;
  for(int v : adj[u]){
    if(v == p) continue;

    int x = dfs(v, u);
    if(x != -1) tmp.pb(x);
  }

  tmp.pb(u);
  if(tmp.size() > 1){
    for(int i=0;i+1<(int)tmp.size();i+=2){
      to[tmp[i]] = revto[tmp[i]] = tmp[i+1];
      to[tmp[i+1]] = revto[tmp[i+1]] = tmp[i];
    }
    if(tmp.size()%2){
      to[tmp.back()] = tmp[1];
      to[tmp[0]] = tmp.back();

      revto[tmp[1]] = tmp.back();
      revto[tmp.back()] = tmp[0];
    }
    if(u != 0) ans -= 2;
    return -1;
  }
  else return u;
}  

void solve(){
  int n;
  cin >> n;
  assert(n != 1);
  adj.assign(n, vector<int>());
  for(int i=0;i<n-1;i++){
    int u, v;
    cin >> u >> v;
    u--; v--;
    adj[u].pb(v); adj[v].pb(u);
  }  
  adj[0].pb(0);
  
  to.assign(n, -1); revto.assign(n, -1);
  ans = 2*(n-1);
  int x = dfs(0, 0);
  if(x != -1){
    ans += 2;
    //cout << x+1 << " " << adj[x][0]+1 << " " << to[adj[x][0]]+1 << "\n";
    to[x] = adj[x][0];
    to[revto[adj[x][0]]] = x;
  }
  
  cout << ans << "\n";
  for(int v : to) cout << v+1 << " ";
  cout << "\n";
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }  
  
  return 0;
}  
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 208 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 336 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 208 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -