답안 #877229

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877229 2023-11-23T04:08:57 Z CDuong Pastiri (COI20_pastiri) C++17
23 / 100
77 ms 35704 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname "SHEEP"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

int n, k;
int dis[mxN], par[mxN], dep[mxN];
bool vis[mxN], visbfs[mxN];
vector<int> G[mxN], res, cand;

void dfs(int v, int p) {
    par[v] = p;
    dep[v] = dep[p] + 1;

    for (int u : G[v]) {
        if (u == p) continue;
        dfs(u, v);
    }
}

void bfs(int v, int len) {
    vector<int> reset;
    queue<pair<int, int>> q;
    q.emplace(v, 0); visbfs[v] = true;
    while (not q.empty()) {
        int v = q.front().ff, cdis = q.front().ss; q.pop();
        reset.emplace_back(v);
        if (cdis < len) {
            for (int u : G[v]) if (!visbfs[u]) {
                q.emplace(u, cdis + 1);
                visbfs[u] = true;
            }
        }
        else {
            vis[v] = false;
        }
    }
    for (int v : reset) visbfs[v] = false;
}

void solve() {
    cin >> n >> k;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    fill(dis + 1, dis + n + 1, -1);
    queue<int> q;
    for (int i = 1; i <= k; ++i) {
        int v; cin >> v;
        vis[v] = true;
        q.emplace(v); dis[v] = 0;
        cand.emplace_back(v);
    }
    while (not q.empty()) {
        int v = q.front(); q.pop();
        for (int u : G[v]) {
            if (dis[u] == -1) {
                dis[u] = dis[v] + 1;
                q.emplace(u);
            }
        }
    }

    dfs(1, 0);

    sort(all(cand), [&](int x, int y) {
        return dep[x] > dep[y];
    });
    for (int v : cand) if (vis[v]) {
        // cout << v << endl;
        int cur = 0;
        while (v != 1 && dis[par[v]] == cur + 1) {
            ++cur, v = par[v];
        }
        // cout << "chosen: " << v << " " << endl;
        res.emplace_back(v);
        bfs(v, dis[v]);
    }
    cout << isz(res) << "\n";
    for (int v : res) cout << v << " ";
    cout << "\n";
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 77 ms 35704 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6748 KB Output is correct
2 Correct 3 ms 6744 KB Output is correct
3 Runtime error 6 ms 13404 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 3 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 3 ms 6748 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 6 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 13404 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -