Submission #1003960

# Submission time Handle Problem Language Result Execution time Memory
1003960 2024-06-20T20:54:28 Z vjudge1 Pastiri (COI20_pastiri) C++17
0 / 100
592 ms 119632 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], pai( maxn ), dist( maxn, maxn ), marc( maxn ), nivel( maxn ), is_sheep(maxn), adj2[maxn], used( maxn ), grau( maxn );
vector<pii> v[maxn];
queue<int> fila;
set<pii> folhas;

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

void bfs( vector<int>& sources ){
  for( int x : sources ) marc[x] = x, 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 );
      if( dist[viz] == dist[cur] + 1 ) adj2[viz].push_back(cur);
    }
  }
}

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

void dfs( int cur ){
  used[cur] = true;
  for( int viz : adj2[cur] ) if( !used[viz] ) dfs( viz );
}

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

int main(){
  int n, k; cin >> n >> k;
  for( int i = 1; i < n; i++ ){
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    grau[a]++; grau[b]++;
  }
  vector<int> sheep(k);
  for( int i = 0; i < k; i++ ) cin >> sheep[i], is_sheep[sheep[i]] = true;

  bfs(sheep);

  dfsInit( 1, 1 );

  vector<int> resp;

  while( !folhas.empty() ){
    int cur = prev(folhas.end())->second; folhas.erase(prev(folhas.end()));
    // bug(cur);
    if( !is_sheep[cur] ) used[cur] = true;
    if( used[cur] || !is_sheep[cur] ) continue;
    int aux = find(cur, cur);
    // bug(aux);
    resp.push_back(aux);
    int next;
    for( int viz : adj[cur] ) if( !used[viz] ) next = viz;
    // bug(next);
    grau[next]--;
    if( grau[next] == 1 ) folhas.insert({ nivel[next], next });

    dfs(aux);
  }

  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:75:14: warning: 'next' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |     grau[next]--;
      |              ^
# Verdict Execution time Memory Grader output
1 Incorrect 238 ms 119632 KB Integer 0 violates the range [1, 500000]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 49820 KB Sheep 857 not protected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 49312 KB Sheep 16 not protected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 592 ms 89940 KB Sheep 216 not protected
2 Halted 0 ms 0 KB -