Submission #1368065

#TimeUsernameProblemLanguageResultExecution timeMemory
1368065SmuggingSpunNetwork (BOI15_net)C++20
0 / 100
2 ms344 KiB
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
const int lim = 5e5 + 5;
int n, tdfs = 0, low[lim];
vector<int>g[lim];
void dfs(int s, int p = -1){
  low[s] = ++tdfs;
  for(int& d : g[s]){
    if(d != p){
      dfs(d, s);
    }
  }
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
  cin >> n;
  for(int i = 1; i < n; i++){
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  vector<int>leaf;
  for(int i = 1; i <= n; i++){
    if(g[i].size() == 1){
      leaf.push_back(i);
    }
  }
  sort(leaf.begin(), leaf.end(), [&] (int i, int j){
    return low[i] < low[j];
  });
  cout << (int(leaf.size() + 1) >> 1) << "\n";
  if(int(leaf.size()) & 1){
    cout << leaf[0] << " " << leaf[int(leaf.size()) >> 1] << "\n";
  }
  for(int i = 0; i < (int(leaf.size()) >> 1); i++){
    cout << leaf[i] << " " << leaf[int(leaf.size()) - i - 1] << "\n";
  }
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:18:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...