Submission #849142

#TimeUsernameProblemLanguageResultExecution timeMemory
849142sandry24Network (BOI15_net)C++17
0 / 100
5 ms12324 KiB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

vector<vi> adj(500005);
vector<bool> visited(500005);
int maxdist = 0, maxnode1 = -1, maxnode2 = -1;

void dfs(int x, int d, int &maxdist, int &maxnode){
    visited[x] = 1;
    if(maxdist < d){
        maxdist = d;
        maxnode = x;
    }
    for(auto i : adj[x])
        if(!visited[i])
            dfs(i, d+1, maxdist, maxnode);
}
 
void solve(){
    int n;
    cin >> n;
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, 0, maxdist, maxnode1);
    visited = vector<bool>(500005);
    maxdist = 0;
    dfs(maxnode1, 0, maxdist, maxnode2);
    vi leaves;
    for(int i = 1; i <= n; i++)
        if(adj[i].size() == 1 && i != maxnode1 && i != maxnode2)
            leaves.pb(i);
    cout << ceil((double)leaves.size()/2) + 1 << '\n';
    cout << maxnode1 << ' ' << maxnode2 << '\n';
    for(int i = 1; i < leaves.size(); i += 2)
        cout << leaves[i-1] << ' ' << leaves[i] << '\n';
    if(leaves.size() & 1)
        cout << leaves.back() << ' ' << maxnode1 << '\n';
}
 
int main(){
    //freopen("flota.in", "r", stdin);
    //freopen("flota.out", "w", stdout);
    //ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}

Compilation message (stderr)

net.cpp: In function 'void solve()':
net.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int i = 1; i < leaves.size(); i += 2)
      |                    ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...