답안 #1004010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004010 2024-06-20T23:59:17 Z vjudge1 Pastiri (COI20_pastiri) C++14
0 / 100
470 ms 63824 KB
#include <bits/stdc++.h>
using namespace std;
#define bug(x) cout << #x << " " << x << endl
#define pii pair<int, int>

const int maxn = 5e5 + 10;
vector<int> adj[maxn], dist( maxn, maxn ), grau( maxn ), nivel( maxn );
vector<bool> sheep( maxn ), marc( maxn );
set<pair<int, int>> folhas;

queue<int> fila;
void add( int x, int d ){ fila.push(x); dist[x] = d; }

void bfs( vector<int>& sources ){
  for( int x : sources ) add( x, 0 );
  while( !fila.empty() ){
    int cur = fila.front(); fila.pop();
    for( int viz : adj[cur] ) if( dist[viz] > dist[cur] + 1 ) add( viz, dist[cur] + 1 );
  }
}

int get_centro( int n ){
  queue<int> fila;
  for( int i = 1; i <= n; i++ ) if( grau[i] == 1 ) fila.push(i);
  while( !fila.empty() ){
    int cur = fila.front(); fila.pop();
    if( grau[cur] == 0 ){
      for( int i = 1; i <= n; i++ ) grau[i] = adj[i].size();
      return cur;
    }
    for( int viz : adj[cur] ) if( --grau[viz] == 1 ) fila.push(viz);
  }
}

void dfsInit( int cur, int pai ){
  for( int viz : adj[cur] ) if( viz != pai ){ nivel[viz] = nivel[cur] + 1; dfsInit( viz, cur ); }
  if( !sheep[cur] ) nivel[cur] = maxn;
  if( grau[cur] == 1 ) folhas.insert({ nivel[cur], cur });
}

int get_next( int node ){ for( int viz : adj[node] ) if( !marc[viz] ) return viz; }

int get_pastor( int cur, int pai ){
  for( int viz : adj[cur] ) if( !marc[viz] && viz != pai && dist[viz] > dist[cur] ) return get_pastor( viz, cur );
  return cur;
}

void dfs( int cur ){
  marc[cur] = true;
  for( int viz : adj[cur] ) if( !marc[viz] && dist[viz] < dist[cur] ) dfs( viz );
}

int main(){
  int n, k; cin >> n >> k;
  if( n == 1 ){ cout << 1 << endl << 1; return 0; }
  for( int i = 1; i < n; i++ ){
    int a, b; cin >> a >> b;
    adj[a].push_back(b); grau[a]++;
    adj[b].push_back(a); grau[b]++;
  }
  vector<int> v(n), resp;
  for( int i = 0; i < n; i++ ){ cin >> v[i]; sheep[v[i]] = true; }
  bfs(v);

  int centro = get_centro(n);
  dfsInit( centro, centro );

  while( !folhas.empty() ){
    int cur = prev(folhas.end())->second; folhas.erase(prev(folhas.end()));
    if( !sheep[cur] ) marc[cur] = true;
    if( marc[cur] ) continue;

    int pastor = get_pastor(cur, cur), next = get_next(cur);
    resp.push_back(pastor);
    if( --grau[next] == 1 ) folhas.insert({ nivel[next], next });

    dfs(pastor);
  }

  sort( resp.begin(), resp.end() );
  cout << resp.size() << endl;
  for( int x : resp ) cout << x << " "; cout << endl;
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:82:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   82 |   for( int x : resp ) cout << x << " "; cout << endl;
      |   ^~~
pastiri.cpp:82:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   82 |   for( int x : resp ) cout << x << " "; cout << endl;
      |                                         ^~~~
pastiri.cpp: In function 'int get_centro(int)':
pastiri.cpp:23:14: warning: control reaches end of non-void function [-Wreturn-type]
   23 |   queue<int> fila;
      |              ^~~~
pastiri.cpp: In function 'int get_next(int)':
pastiri.cpp:41:83: warning: control reaches end of non-void function [-Wreturn-type]
   41 | int get_next( int node ){ for( int viz : adj[node] ) if( !marc[viz] ) return viz; }
      |                                                                                   ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 219 ms 63824 KB Integer 0 violates the range [1, 500000]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 18520 KB Sheep 857 not protected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 18268 KB Sheep 16 not protected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 470 ms 51636 KB Sheep 216 not protected
2 Halted 0 ms 0 KB -