Submission #1004088

# Submission time Handle Problem Language Result Execution time Memory
1004088 2024-06-21T05:01:49 Z vjudge1 Pastiri (COI20_pastiri) C++17
0 / 100
1000 ms 182356 KB
#include<bits/stdc++.h>

using namespace std;

const int N = 5e5+5;
int n, k, dist[N], cnt[N];
vector<int> s, G[N], S[N], P[N], ans;
bool p[N], seen[N];
set<pair<int,pair<int,int> > > st;

void bfs()
{
  queue<int> Q;
  for(int i = 1; i <= n; i ++) dist[i] = N + 20;
  for(int i : s)
    dist[i] = 0, cnt[i] = 1, Q.push(i), p[i] = true;

  while(Q.size())
    {
      int u = Q.front();
      // cerr << u << endl;
      Q.pop();
      for(int  v: G[u])
	if(dist[u] + 1 <= dist[v])
	  {
	    S[u].push_back(v);
	    P[v].push_back(u);

	    cnt[v] += cnt[u];
	    
	    if(dist[v] > dist[u] + 1)
	      {
		dist[v] = dist[u] + 1;
		Q.push(v);
	      }
	  }
    }
}

void dfs(int v)
{
  if(seen[v]) return;
  seen[v] = true;

  for(int u : S[v])
    {
      st.erase({cnt[u], {dist[u], u}});
      cnt[v] -= cnt[u];
      if(cnt[u] > 0)
	st.insert({cnt[u],{dist[u], u}});
    }

  for(int u : P[v])
    dfs(u);
}

void solve()
{

  for(int i = 1; i <= n; i ++)
    st.insert({cnt[i], {dist[i], i}});
  
  while(st.size() && st.rbegin()->first > 0)
    {
      int u = st.rbegin()->second.second;
      if(!seen[u]) ans.push_back(u);
      st.erase(--st.end());
      dfs(u);
    }
  
}

int main()
{
  cin >> n >> k;

  for(int i = 1; i < n; i ++)
    {
      int u, v;
      cin >> u >> v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
  
  s.resize(k);
  for(int i = 0; i < k; i ++)
    cin >> s[i];

  bfs();
  solve();
  
  cout << ans.size() << endl;
  for(int v : ans) cout << v << ' ';
  cout << endl;
  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 447 ms 182356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 40028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 39516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1027 ms 121812 KB Time limit exceeded
2 Halted 0 ms 0 KB -