답안 #639382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639382 2022-09-09T19:20:08 Z 3omar_ahmed Network (BOI15_net) C++17
0 / 100
7 ms 11988 KB
/**
 * author: 3omar_ahmed
 * date: 09-09-2022
 * Expert When :)
 */
#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()
int n ;
vector < vector < int >> adj(500001);
vector < int > a;
void dfs(int node , int par){
    if(adj[node].size() == 1){
        a.push_back(node);
    }
    for(auto i : adj[node]){
        if(i == par) continue ;
        dfs(i , node);
    }
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

    cin >> n;
    for(int i = 1 ; i < n ; i++){
        int u , v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , 0);
    // for(auto i : a) cout << i << ' ';
    // cout << endl;
    cout << ((int)(a.size()) + 1) / 2 << endl;
    for(int i = 1 ; i < a.size() ; i += 2){
        cout << a[i - 1] << ' ' << a[i] << endl; 
    }
    if((int)(a.size()) % 2 == 0) return 0; 
    int val = a.back();
    set < int > s ;
    for(int i = 1 ; i <= n ; i++){
        if(count(all(adj[val]),i))
            continue ;
        val = i ;
        break ;
    }
    cout << a.back() << ' ' << val << endl;
    return 0 ;
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:38:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 1 ; i < a.size() ; i += 2){
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Incorrect 7 ms 11988 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Incorrect 7 ms 11988 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Incorrect 7 ms 11988 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -