Submission #715793

#TimeUsernameProblemLanguageResultExecution timeMemory
715793Charizard2021Network (BOI15_net)C++17
Compilation error
0 ms0 KiB
#include<bits/stdc++.h>
using namespace std;
const int N = 500001;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
bool visited[N];
int LeavesInsubtee[N];
vector<int> adj[N];
vector<int> res[N];
int n;
int ans = 0;
int cur_ans = 0;
int subtreeValue = 0;
vector<int> arrVectors[N];
void dfs(int u){
    visited[u] = true;
    if((int)adj[u].size() == 1){
        LeavesInsubtee[u] = 1;
    }
    for(int v : adj[u]){
        if(!visited[v]){
            dfs(v);
            LeavesInsubtee[u] += LeavesInsubtee[v];
        }
    }
}
void dfs2(int u){
    visited[u] = true;
    if((int)adj[u].size() == 1){
        res[subtreeValue].push_back(u);
    }
    for(int v : adj[u]){
        if(!visited[v]){
            dfs2(v);
        }
    }
}
int cutlocation(int u){
    visited[u] = true;
    for(int v : adj[u]){
        if(!visited[v]){
            if(LeavesInsubtee[v] > floor((cur_ans)/2)){
                return cutlocation(v);
            }
        }
    }
    return u;
}
int cut(int u){
    dfs(1);
    for(int i = 1; i <= n; i++){
        visited[i] = false;
    }
    int x = cutlocation(1);
    for(int i = 1; i <= n; i++){
        visited[i] = false;
    }
    visited[x] = true;
    for(int v : adj[x]){
        subtreeValue++;
        dfs2(v);
    }
    return x;
}
struct cmp{
    bool operator()(const int &a, const int &b){
        if(arrVectors[a].size() == arrVectors[b].size()){
            return a < b;
        }
        return arrVectors[a].size() > arrVectors[b].size();
    }
};
int main(){
    IOS;
    cin >> n;
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i = 1; i <= n; i++){
        if((int)adj[i].size() == 1){
            ans++;
        }
    }
    cur_ans = ans;
    ans = (ans + 1)/2;
    cout << ans << "\n";
    int x12 = cut(n);
    int idx = 0;
    vector<int> finalAns(2 * ans);
    vector<int> v;
    set<int, cmp> s;
    for(int i = 1; i <= subtree; i++){
        s.insert(i);
    }
    while(!s.empty()){
        int a = *(s.begin());
        s.erase(a);
        v.push_back(res[a].back());
        res[a].pop_back();
        if(!s.empty()){
            int b = *(s.begin());
            s.erase(b);
            v.push_back(res[b].back());
            res[b].pop_back();
            if(!res[b].empty()){
                s.insert(b);
            }
        }
        if(!res[a].empty()){
            s.insert(a);
        }
    }
    if(v.size() % 2 == 0){
        v.push_back(res[1][0]);
    }
    for(int i = 0; i < (int)finalAns.size(); i += 2){
        cout << finalAns[i] << " " << finalAns[i + 1] << "\n";
    }
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:94:25: error: 'subtree' was not declared in this scope
   94 |     for(int i = 1; i <= subtree; i++){
      |                         ^~~~~~~
net.cpp:89:9: warning: unused variable 'x12' [-Wunused-variable]
   89 |     int x12 = cut(n);
      |         ^~~
net.cpp:90:9: warning: unused variable 'idx' [-Wunused-variable]
   90 |     int idx = 0;
      |         ^~~
In file included from /usr/include/c++/10/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:81,
                 from net.cpp:1:
/usr/include/c++/10/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type = const std::_Rb_tree_node<int>*]':
/usr/include/c++/10/bits/stl_tree.h:2101:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = int]'
/usr/include/c++/10/bits/stl_tree.h:2154:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>]'
/usr/include/c++/10/bits/stl_set.h:512:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = cmp; _Alloc = std::allocator<int>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, cmp, std::allocator<int> >::const_iterator; std::set<_Key, _Compare, _Alloc>::value_type = int]'
net.cpp:95:19:   required from here
/usr/include/c++/10/bits/stl_tree.h:780:8: error: static assertion failed: comparison object must be invocable as const
  780 |        is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~