Submission #986295

# Submission time Handle Problem Language Result Execution time Memory
986295 2024-05-20T09:17:08 Z vjudge1 Pastiri (COI20_pastiri) C++17
100 / 100
815 ms 95136 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 5e5 + 5;

vector<int> g[nmax];
vector<int> edge[nmax];
int dist[nmax];
int taken[nmax];

int dir[nmax];

vector<int> rez;

namespace Tree {
   int p[nmax], jump[nmax], d[nmax];
   
   namespace DS {
      void fill(int node, int f, int D) {
         if(taken[node]) return;
         taken[node] = 1;
         if(D == 0) return;
         //cerr << node << ' ' << f << ' ' << D << '\n';
         
         for(int i = 0; i < sz(g[node]); i++) {
            auto x = g[node][i];
            if(x != f && dir[edge[node][i]] == x) fill(x, node, D - 1);
         }
      }
      void toggle() {
         return;
      }
   }
   
   void init(int node, int f) {
      d[node] = d[f] + 1;
      p[node] = jump[node] = f;
      if(d[jump[jump[f]]] + d[f] == 2 * d[jump[f]]) jump[node] = jump[jump[f]];
      
      for(auto x : g[node])
         if(x != f)
            init(x, node);
      return;
   }
   
   void fill(int node) {
      int targetfather = node;
      auto isgood = [&](int x) {
         return dist[x] == d[node] - d[x];
      };
      
      while(isgood(p[targetfather]) && targetfather != 1) {
         if(isgood(jump[targetfather])) targetfather = jump[targetfather];
         if(isgood(p[targetfather])) targetfather = p[targetfather];
      }
      
      rez.emplace_back(targetfather);
      DS::fill(targetfather, targetfather, dist[targetfather]);
      
      return;
   }
   
}

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n, k;
   cin >> n >> k;
   for(int i = 1, x, y; i < n; i++) {
      cin >> x >> y;
      g[x].emplace_back(y);
      g[y].emplace_back(x);
      edge[x].emplace_back(i);
      edge[y].emplace_back(i);
   }
   Tree::init(1, 1);
   
   for(int i = 1; i <= n; i++) dist[i] = n + 5;
   
   vector<int> v(k);
   queue<int> que;
   for(auto &x : v) cin >> x, que.emplace(x), dist[x] = 0;
   
   while(!que.empty()) {
      int node = que.front();
      que.pop();
      
      for(int i = 0; i < sz(g[node]); i++) {
         auto x = g[node][i];
         if(dist[x] == n + 5) dist[x] = dist[node] + 1, que.emplace(x);
         if(dist[x] == dist[node] + 1) dir[edge[node][i]] = node;
         
      }
   }
   
   //cerr << "AA\n";
   
   sort(all(v), [&](int a, int b) { return Tree::d[a] > Tree::d[b]; });
   
   for(auto x : v) {
      if(taken[x]) continue;
      Tree::fill(x);
   }
   
   cout << sz(rez) << '\n';
   for(auto x : rez)
      cout << x << ' ';
   cout << '\n';
   
}

/**
      Istenem! Nu poate fi real.
-- Surse verificate
*/

# Verdict Execution time Memory Grader output
1 Correct 175 ms 90452 KB Output is correct
2 Correct 222 ms 90708 KB Output is correct
3 Correct 212 ms 90456 KB Output is correct
4 Correct 296 ms 95136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 33624 KB Output is correct
2 Correct 9 ms 33628 KB Output is correct
3 Correct 544 ms 67480 KB Output is correct
4 Correct 510 ms 72276 KB Output is correct
5 Correct 638 ms 66872 KB Output is correct
6 Correct 815 ms 69092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 33624 KB Output is correct
2 Correct 7 ms 33372 KB Output is correct
3 Correct 8 ms 33476 KB Output is correct
4 Correct 8 ms 33372 KB Output is correct
5 Correct 8 ms 33636 KB Output is correct
6 Correct 8 ms 33628 KB Output is correct
7 Correct 9 ms 33608 KB Output is correct
8 Correct 8 ms 33372 KB Output is correct
9 Correct 8 ms 33372 KB Output is correct
10 Correct 8 ms 33372 KB Output is correct
11 Correct 8 ms 33624 KB Output is correct
12 Correct 7 ms 33372 KB Output is correct
13 Correct 7 ms 33372 KB Output is correct
14 Correct 8 ms 33628 KB Output is correct
15 Correct 8 ms 33628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 658 ms 68136 KB Output is correct
2 Correct 648 ms 67948 KB Output is correct
3 Correct 636 ms 70212 KB Output is correct
4 Correct 630 ms 66968 KB Output is correct
5 Correct 474 ms 64064 KB Output is correct
6 Correct 663 ms 74324 KB Output is correct
7 Correct 715 ms 73124 KB Output is correct
8 Correct 666 ms 72532 KB Output is correct
9 Correct 811 ms 67128 KB Output is correct
10 Correct 680 ms 73648 KB Output is correct
11 Correct 483 ms 77008 KB Output is correct
12 Correct 413 ms 82004 KB Output is correct
13 Correct 534 ms 84788 KB Output is correct
14 Correct 368 ms 79908 KB Output is correct
15 Correct 49 ms 40276 KB Output is correct
16 Correct 724 ms 71124 KB Output is correct
17 Correct 448 ms 71608 KB Output is correct
18 Correct 640 ms 66900 KB Output is correct
19 Correct 682 ms 78720 KB Output is correct
20 Correct 659 ms 80832 KB Output is correct
21 Correct 587 ms 73880 KB Output is correct
22 Correct 617 ms 74580 KB Output is correct