Submission #863496

#TimeUsernameProblemLanguageResultExecution timeMemory
863496RifalPastiri (COI20_pastiri)C++14
0 / 100
1041 ms162024 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...