답안 #877232

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877232 2023-11-23T04:10:31 Z CDuong Pastiri (COI20_pastiri) C++17
49 / 100
1000 ms 55484 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 = 5e5 + 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 Correct 114 ms 50560 KB Output is correct
2 Correct 158 ms 50304 KB Output is correct
3 Correct 137 ms 50308 KB Output is correct
4 Correct 242 ms 55484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 15192 KB Output is correct
2 Correct 5 ms 15256 KB Output is correct
3 Correct 291 ms 39892 KB Output is correct
4 Correct 329 ms 42512 KB Output is correct
5 Correct 384 ms 39700 KB Output is correct
6 Correct 759 ms 44212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 4 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 4 ms 14964 KB Output is correct
5 Correct 5 ms 14996 KB Output is correct
6 Correct 5 ms 14940 KB Output is correct
7 Correct 4 ms 14940 KB Output is correct
8 Correct 4 ms 15072 KB Output is correct
9 Correct 5 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 5 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 7 ms 14940 KB Output is correct
14 Correct 5 ms 14936 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 387 ms 35296 KB Output is correct
2 Correct 352 ms 39248 KB Output is correct
3 Correct 398 ms 42704 KB Output is correct
4 Correct 368 ms 38744 KB Output is correct
5 Correct 347 ms 38096 KB Output is correct
6 Correct 384 ms 44988 KB Output is correct
7 Correct 452 ms 44032 KB Output is correct
8 Correct 465 ms 43692 KB Output is correct
9 Execution timed out 1047 ms 42940 KB Time limit exceeded
10 Halted 0 ms 0 KB -