Submission #472636

#TimeUsernameProblemLanguageResultExecution timeMemory
472636robellNetwork (BOI15_net)C++17
0 / 100
36 ms47992 KiB
#pragma GCC optimize("O2") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef long long ll; #define pb push_back #define eb emplace_back #define countbits __builtin_popcount #define beg0 __builtin_clz #define terminal0 __builtin_ctz #define mod 1e9+7 void setIO(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void setIO(string f){ freopen((f+".in").c_str(),"r",stdin); freopen((f+".out").c_str(),"w",stdout); setIO(); } int N; vector<int> l[(int)(5e5)]; vector<int> z[(int)(5e5)]; vector<pair<int,int>> pairs; int dfs(int i, int p){ if (i==0){ int sum = 0; for (int j:l[i]){ if (j!=p){ sum+=dfs(j,i); } } vector<vector<int>> col; if (l[0].size()==1) col.pb(vector<int>(1,0)); for (int j:l[i]){ if (j!=p){ col.pb(z[j]); } } for (int j=0;j<col.size()-1;j+=2){ pairs.pb({col[j][0],col[j+1][0]}); } if (col.size()%2==1){ bool w = false; for (int i=0;i<col.size()-1;i++){ if (col[i].size()>1){ pairs.pb({col[col.size()-1][0],col[i][1]}); w=true; for (int j=0;j<col.size();j++){ col[i].erase(col[i].begin()); } col[i].erase(col[i].begin()); break; } } if (!w){ pairs.pb({col[col.size()-1][0],0}); for (int i=0;i<col.size();i++){ col[i].erase(col[i].begin()); } } }else{ for (int i=0;i<col.size();i++){ col[i].erase(col[i].begin()); } } vector<int> make; for (int i=0;i<col.size();i++){ for (int j=0;j<col[i].size();j++){ make.pb(col[i][j]); } } for (int i=0;i<make.size()-1;i+=2){ pairs.pb({make[i],make[i+1]}); } if (make.size()%2==1){ pairs.pb({make[make.size()-1],0}); } return sum; }else{ int sum = 0; int max = 0; int pos = -1; for (int j:l[i]){ if (j!=p){ sum+=dfs(j,i); if (z[j].size()>max){pos=j;max=z[j].size();} } } if (pos!=-1){ swap(z[pos],z[i]); for (int j:l[i]){ if (j!=p){ for (int k:z[j]){ z[i].pb(k); } } } } if (l[i].size()==1){sum++;z[i].pb(i);} return sum; } } int main(){ setIO(); cin >> N; for (int i=0;i<N-1;i++){ int a, b; cin >> a >> b; l[--a].pb(--b); l[b].pb(a); } int res = dfs(0,0); cout << pairs.size() << "\n"; for (pair<int,int> a:pairs){ cout << a.first+1 << " " << a.second+1 << "\n"; } }

Compilation message (stderr)

net.cpp: In function 'int dfs(int, int)':
net.cpp:42:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for (int j=0;j<col.size()-1;j+=2){
      |                ~^~~~~~~~~~~~~
net.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |    for (int i=0;i<col.size()-1;i++){
      |                 ~^~~~~~~~~~~~~
net.cpp:51:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |      for (int j=0;j<col.size();j++){
      |                   ~^~~~~~~~~~~
net.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i=0;i<col.size();i++){
      |                  ~^~~~~~~~~~~
net.cpp:65:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |    for (int i=0;i<col.size();i++){
      |                 ~^~~~~~~~~~~
net.cpp:70:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   for (int i=0;i<col.size();i++){
      |                ~^~~~~~~~~~~
net.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |    for (int j=0;j<col[i].size();j++){
      |                 ~^~~~~~~~~~~~~~
net.cpp:75:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |   for (int i=0;i<make.size()-1;i+=2){
      |                ~^~~~~~~~~~~~~~
net.cpp:88:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   88 |     if (z[j].size()>max){pos=j;max=z[j].size();}
      |         ~~~~~~~~~~~^~~~
net.cpp: In function 'int main()':
net.cpp:113:6: warning: unused variable 'res' [-Wunused-variable]
  113 |  int res = dfs(0,0);
      |      ^~~
net.cpp: In function 'void setIO(std::string)':
net.cpp:19:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |  freopen((f+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:20:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |  freopen((f+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...