답안 #759055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759055 2023-06-15T17:38:02 Z Ronin13 Pastiri (COI20_pastiri) C++14
49 / 100
1000 ms 77164 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 500001;

int x[nmax], d[nmax];
bool active[nmax];
int dp[nmax];
vector <vector <int> > g(nmax);

void dfs(int v, int e = -1){
    for(int to : g[v]){
        if(to == e) continue;
        dfs(to, v);
        d[v] = min(d[v], d[to] + 1);
    }
}

vector <int> ans;

void DFS(int v, int e = -1){
    for(int to : g[v]){
        if(to == e) continue;
        DFS(to, v);
        active[v] |= active[to];
        dp[v] = max(dp[v], dp[to] - 1);
    }
    if(v == 1 && active[v]){
        if(dp[v] >= d[v]) return;
        ans.pb(v);
        dp[v] = -1;
        return;
    }
    if(!active[v])
        return;
    if(x[e] == d[v] + 1){
        if(dp[v] >= d[v]) active[v] = false;
        return;
    }
    active[v] = false;
    if(dp[v] >= d[v])
        return;
    dp[v] = d[v];
    ans.pb(v);
}

int main(){
    //ios_base::sync_with_stdio(false); cin.tie(0);
    int n, k;
    scanf("%d%d",&n, &k);
    for(int i = 1; i < n; i++){
        int u, v; scanf("%d%d", &u, &v);
        g[u].pb(v);
        g[v].pb(u);
    }
    //int k; cin >> k;
    for(int i = 1; i <= n; i++){
        x[i] = d[i] = 1e9;
        dp[i] = -1e9;
    }
    queue <int> q;
    for(int i = 1; i <= k; ++i){
        int y; scanf("%d", &y);
        q.push(y);
        active[y] = true;
        x[y] = d[y] = 0;
    }
    while(!q.empty()){
        int v = q.front();
        q.pop();
        for(int to : g[v]){
            if(x[to] <= x[v]) continue;
            x[to] = x[v] + 1;
            q.push(to);
        }
    }
    dfs(1);
    DFS(1);
    printf("%d\n", ans.size()); //ans.size() << "\n";
    sort(ans.begin(), ans.end());
    for(int to : ans)
        printf("%d ", to);
    return 0;
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:86:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   86 |     printf("%d\n", ans.size()); //ans.size() << "\n";
      |             ~^     ~~~~~~~~~~
      |              |             |
      |              int           std::vector<int>::size_type {aka long unsigned int}
      |             %ld
pastiri.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d%d",&n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~
pastiri.cpp:59:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:70:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         int y; scanf("%d", &y);
      |                ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 73548 KB Output is correct
2 Correct 184 ms 73448 KB Output is correct
3 Correct 180 ms 73420 KB Output is correct
4 Correct 307 ms 77164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12244 KB Output is correct
2 Correct 8 ms 12324 KB Output is correct
3 Correct 442 ms 35104 KB Output is correct
4 Correct 499 ms 38412 KB Output is correct
5 Correct 653 ms 34420 KB Output is correct
6 Correct 676 ms 37860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12080 KB Output is correct
2 Correct 7 ms 12128 KB Output is correct
3 Correct 8 ms 12188 KB Output is correct
4 Correct 10 ms 12132 KB Output is correct
5 Correct 7 ms 12116 KB Output is correct
6 Correct 7 ms 12136 KB Output is correct
7 Correct 7 ms 12116 KB Output is correct
8 Correct 7 ms 12116 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 7 ms 12204 KB Output is correct
11 Correct 7 ms 12244 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 13 ms 12936 KB Output is correct
14 Correct 7 ms 12244 KB Output is correct
15 Correct 8 ms 12128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 566 ms 35420 KB Output is correct
2 Correct 544 ms 35328 KB Output is correct
3 Correct 602 ms 36884 KB Output is correct
4 Correct 607 ms 34316 KB Output is correct
5 Execution timed out 1076 ms 31460 KB Time limit exceeded
6 Halted 0 ms 0 KB -