Submission #221990

#TimeUsernameProblemLanguageResultExecution timeMemory
221990mohamedsobhi777Network (BOI15_net)C++14
0 / 100
7 ms4992 KiB
#include<bits/stdc++.h> using namespace std ; const int N = 2e5 + 7 ; int n; map<pair<int , int> , bool > is ; vector<int> adj[N] ; int node , dist ; bool vis[N] ; int dfs(int x , int p , int h){ for(auto u : adj[x]){ if(u==p)continue ; dfs(u , x , h+1 ) ; } if(!vis[x] && adj[x].size() == 1){ if(h > dist){ dist = h ; node = x ; } } } int main(){ ios_base::sync_with_stdio(0) ; cin.tie(0) ; //freopen("in.in" , "r" , stdin) ; cin>>n ; for(int i = 0 ; i < n- 1 ; i++){ int a , b ; cin>>a>>b ; if(a > b)swap(a , b) ; adj[a].push_back(b) ; adj[b].push_back(a) ; is[{a , b}] = 1; } int k = 0 ; for(int i = 1 ;i<=n ;i++){ k+=(adj[i].size()==1) ; } cout<<(k+1)/2<<'\n' ; while(k > 1){ dist = node = 0; dfs(1 , 1 , 0) ; int L = node ; dist = node = 0; dfs(L , L , 0) ; cout<<node<<' ' << L<<'\n' ; vis[L] = vis[node] = 1 ; k-=2 ; } if(k){ for(int i = 1 ;i <=n;i++){ if(adj[i].size() > 1 || vis[i])continue ; for(int j = 1 ; j<=n;j++){ if(j==i)continue ; int a = min(i , j) , b = max(i , j) ; if(is[{a , b}])continue ; cout<<a<<' ' <<b; return 0; } } } return 0 ; }

Compilation message (stderr)

net.cpp: In function 'int dfs(int, int, int)':
net.cpp:25:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...