Submission #783729

#TimeUsernameProblemLanguageResultExecution timeMemory
783729raysh07Network (BOI15_net)C++17
100 / 100
371 ms52068 KiB

#include <bits/stdc++.h>
#include <iomanip>
 
#define ll long long
#define pb push_back
#define F first
#define S second
#define nl '\n'
#define all(x) x.begin(), x.end()
 
const int N = 5e5 + 7;
const int modd = 1e9 + 7;
const int INF = 1e9 + 7;
const double pi = 3.141592653589793238462643383279502884197;
const double EPS = 0.0000000000001;
using namespace std;
  
int gcd(int a, int b){
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
ll binpow(ll a, ll x){
    if(x == 0) return 1;
    if(x % 2 == 0){
        ll y = binpow(a, x / 2);
        return (y * y);
    }
    else{
        return (binpow(a, x - 1) * a);
    }
}
 
ll n;
vector <ll> v[N];
vector <ll> s;
vector <ll> l;
vector <ll> r;
bool used[N];
 
void dfs(ll x){
    used[x] = 1;
    if(v[x].size() == 1){
        s.pb(x);
    }
    for(auto to : v[x]){
        if(used[to] == 0){
            dfs(to);
        }
    }
}
 
void Muqaltin(){  
    cin >> n;
    for(int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    dfs(1);
    for(int i = 0; i < s.size() / 2; i++){
        l.pb(s[i]);
    }
    for(int i = s.size() / 2; i < s.size(); i++){
        r.pb(s[i]);
    }
    cout << max(l.size(), r.size()) << nl;
    for(int i = 0; i < min(l.size(), r.size()); i++){
        cout << l[i] << ' ' << r[i] << nl;
    }
    if(l.size() > r.size()){
        cout << l[l.size() - 1] << " " << l[0] << nl;
    }   
    else if(l.size() < r.size()){
        cout << r[r.size() - 1] << " " << r[0] << nl;
    }
    return;
}
 
int main(){
    // freopen("pails.in", "r", stdin);
    //  freopen("pails.out", "w", stdout);
    ios_base :: sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
 
    int t = 1;
    // cin >> t;
    while(t--){
        Muqaltin();
    }
    return 0;
}

Compilation message (stderr)

net.cpp: In function 'void Muqaltin()':
net.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for(int i = 0; i < s.size() / 2; i++){
      |                    ~~^~~~~~~~~~~~~~
net.cpp:68:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for(int i = s.size() / 2; i < s.size(); i++){
      |                               ~~^~~~~~~~~~
net.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   72 |     for(int i = 0; i < min(l.size(), r.size()); i++){
      |                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...