Submission #863496

# Submission time Handle Problem Language Result Execution time Memory
863496 2023-10-20T12:53:06 Z Rifal Pastiri (COI20_pastiri) C++14
0 / 100
1000 ms 162024 KB
#include <bits/stdc++.h>
#include <fstream>
#define endl '\n'
#define mod 1000000007
#define INF 900000000
//#define cin fin
//#define cout fout
//#define fi first
//#define se second
using namespace std;
//ofstream fout("intel.out");
//ifstream fin("intel.in");
const int Max = 5e5 + 5;
vector<int> v[Max];
set<int> st[Max];
int dis[Max];
bool ok[Max];
void dfs(int s, int p) {
    if(ok[s] == 1) {
        dis[s] = 1;
        st[s].insert(s);
    }
    for(auto i : v[s]) {
        if(i != p) {
            dfs(i,s);
            if(dis[i]+1 < dis[s]) {
                dis[s] = dis[i]+1;
                st[s] = st[i];
            }
            else if(dis[i]+1 == dis[s]) {
                for(auto j : st[i]) {
                    st[s].insert(j);
                }
            }

        }
    }
}
void dfs2(int s, int p) {
    for(auto i : v[s]) {
        if(i != p) {
            if(dis[s]+1 < dis[i]) {
                dis[i] = dis[s]+1;
                st[i] = st[s];
            }
            else if(dis[s]+1 == dis[i]) {
                for(auto j : st[s]) {
                    st[i].insert(j);
                }
            }
            dfs2(i,s);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    int n, k; cin >> n >> k;
    for(int i = 1; i <= n; i++) dis[i] = INF;
    for(int i = 0; i < n-1; i++) {
        int a, b; cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for(int i = 0; i < k; i++) {
        int x; cin >> x; ok[x] = 1;
    }
    dfs(1,0);
    dfs2(1,0);
    vector<int> ans;
   /* for(int i = 1; i <= n; i++) {
         cout << i << ' ' << st[i].size() << endl;
         for(auto j : st[i]) {
            cout << j << ' ';
         }
         cout << endl;
      }*/
    while(true) {
        pair<int,int> last = {0,0};
        for(int i = 1; i <= n; i++) {
            if(st[i].size() > last.first) {
                last.first = st[i].size();
                last.second = i;
            } 
        }
        if(last.first == 0) break;
        ans.push_back(last.second);
        for(int i = 1; i <= n; i++) {
            if(i == last.second) continue;
            for(auto j : st[last.second]) {
                st[i].erase(j);
            }
        }
        st[last.second].clear();
    }
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
 return 0;
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:81:29: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   81 |             if(st[i].size() > last.first) {
      |                ~~~~~~~~~~~~~^~~~~~~~~~~~
pastiri.cpp:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
      |                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 211 ms 161872 KB Output is correct
2 Execution timed out 1041 ms 162024 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 38360 KB Output is correct
2 Correct 11 ms 38344 KB Output is correct
3 Correct 584 ms 95300 KB Output is correct
4 Correct 648 ms 128072 KB Output is correct
5 Incorrect 641 ms 83832 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 38232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1041 ms 89792 KB Time limit exceeded
2 Halted 0 ms 0 KB -