Submission #31424

#TimeUsernameProblemLanguageResultExecution timeMemory
31424imaxblueNetwork (BOI15_net)C++14
100 / 100
853 ms66264 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() (rand() >> 3)*rand()

int n, a, b, c, r[500005];
vector<int> v[500005], com[500005];
vector<pii> ans;
int mrg(int A, int B){
    if (com[A].size()<com[B].size()) return mrg(B, A);
    fox(l, com[B].size()){
        //cout << "*" << A << ' ' << B << ' ' << com[A].size() << ' ' << com[B].size() << ' ' << n << endl;
        if (com[A].size()+com[B].size()-l>(c+1)/2){
            c-=2;
            ans.pb(mp(com[A].back(), com[B][l]));
            com[A].pop_back();
        }
        else com[A].pb(com[B][l]);
    }
    return A;
}
void dfs(int N, int P){
    if (v[N].size()==1){
        r[N]=N;
        com[N]=vector<int>(1, N);
        return;
    }
    int best=-1;
    fox(l, v[N].size()){
        if (v[N][l]==P) continue;
        dfs(v[N][l], N);
        if (best==-1 || com[r[v[N][l]]].size()>com[best].size())
            best=r[v[N][l]];
    }
    fox(l, v[N].size()){
        if (v[N][l]==P || r[v[N][l]]==best) continue;
        mrg(best, r[v[N][l]]);
    }
    r[N]=best;
    //cout << N << ' ' << r[N] << endl;
}
int main(){
    scanf("%i", &n);
    fox(l, n-1){
        scanf("%i%i", &a, &b);
        v[a].pb(b);
        v[b].pb(a);
    }
    fox1(l, n){
        if (v[l].size()==1) c++;
    }
    fox1(l, n){
        if (v[l].size()>1){
            dfs(l, -1);
            //cout << com[r[l]].size() << endl;
            if (com[r[l]].size()){
                ans.pb(mp(com[r[l]][0], ans.back().y));
            }
            printf("%i\n", ans.size());
            fox(l2, ans.size()){
                printf("%i %i\n", ans[l2].x, ans[l2].y);
            }
            return 0;
        }
    }
    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int mrg(int, int)':
net.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
                                  ^
net.cpp:32:5: note: in expansion of macro 'fox'
     fox(l, com[B].size()){
     ^
net.cpp:34:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (com[A].size()+com[B].size()-l>(c+1)/2){
                                          ^
net.cpp: In function 'void dfs(int, int)':
net.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
                                  ^
net.cpp:50:5: note: in expansion of macro 'fox'
     fox(l, v[N].size()){
     ^
net.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
                                  ^
net.cpp:56:5: note: in expansion of macro 'fox'
     fox(l, v[N].size()){
     ^
net.cpp: In function 'int main()':
net.cpp:80:38: warning: format '%i' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' [-Wformat=]
             printf("%i\n", ans.size());
                                      ^
net.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
                                  ^
net.cpp:81:13: note: in expansion of macro 'fox'
             fox(l2, ans.size()){
             ^
net.cpp:64:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i", &n);
                    ^
net.cpp:66:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i", &a, &b);
                              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...