CSES - NOI 2019 - Results
Submission details
Task:Distance Code
Sender:Tuomas Katajisto
Submission time:2019-03-06 15:52:42 +0200
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::vector<int> dc1(int, std::set<int>*)':
input/code.cpp:24:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(rm.size() != n) {
      ~~~~~~~~~~^~~~
input/code.cpp: In function 'int dd(std::set<int>*, int, int)':
input/code.cpp:60:23: error: could not convert 'bt' from 'std::unordered_set<int>' to 'std::set<int>'
   return dist(t,s,bt,q)-1;
                       ^
input/code.cpp: In function 'std::vector<int> dc2(int, std::set<int>*)':
input/code.cpp:66:42: error: no match for 'operator=' (operand types are 'std::unordered_set<int>' and 'std::set<int>')
   for(int i = 0; i < 11; i++) tc[i] = t[i];
                                          ^
In file included from /usr/include/c++/7/unordered_set:48:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:118,
                 from input/code.cpp:1:
/usr/include/c++/7/bits/unordered_set.h:264:7: note: candidate: std::unordered_set<_Value, _Hash

Code

#include<bits/stdc++.h>
using namespace std;
 
vector<int> dc1(int n, set<int> t[11]) {
  vector<int> rm;
  bool dq = false;
  while(!dq) {
    dq = true;
    vector<pair<int,int>> tr;
    for(int i = 1; i <= n; i++) {
      if(t[i].size() != 0) dq = false;
      if(t[i].size() == 1) {
	rm.push_back(i);
	for(auto u : t[i]) {
	  tr.push_back({u,i});
	}
      }
    }
    for(auto u : tr) {
      t[u.first].erase(u.second);
      t[u.second].erase(u.first);
    }
  }
  if(rm.size() != n) {
    set<int> jj;
    for(int i = 1; i <= n; i++) {
      jj.insert(i);
    }
    for(auto u : rm) jj.erase(u);
    if(jj.size() != 0) {
      for(auto u : jj) {
	rm.push_back(u);
      }
    }
  }
  return rm;
}
 
 
int dist(set<int> t[11], int s, set<int> bt, int q) {
  //cout << "DIST FROM " << s << " TO " << q << "\n";
  if(bt.count(s)) { /*cout << "RTAV\n"*/; return 0; }
  bt.insert(s);
  if(s == q) {
    //cout << "AT Q" << "\n";
    return 1;
  }
  int ret = 0;
  //cout << "SETSIZE " << t[s].size() << endl;
  for(auto u : t[s]) {
    //cout << "NEXT" << "\n";
    ret += dist(t,u,bt,q);
  }
  if(ret > 0) ret++;
  return ret;
}
 
int dd(set<int> t[11], int s, int q) {
  unordered_set<int> bt;
  return dist(t,s,bt,q)-1;
}
 
vector<int> dc2(int n, set<int> t[11]) {
  vector<int> rm;
  unordered_set<int> tc[11];
  for(int i = 0; i < 11; i++) tc[i] = t[i];
  bool dq = false;
  while(!dq) {
    dq = true;
    vector<pair<int,int>> tr;
    for(int i = 1; i <= n; i++) {
      if(t[i].size() != 0) dq = false;
      if(t[i].size() == 1) {
	rm.push_back(i);
	for(auto u : t[i]) {
	  tr.push_back({u,i});
	}
      }
    }
    for(auto u : tr) {
      t[u.first].erase(u.second);
      t[u.second].erase(u.first);
    }
  }
  if(rm.size() != n) {
    set<int> jj;
    for(int i = 1; i <= n; i++) {
      jj.insert(i);
    }
    for(auto u : rm) jj.erase(u);
    if(jj.size() != 0) {
      for(auto u : jj) {
	rm.push_back(u);
      }
    }
  }
  //cout << "ERORDER" << "\n";
  //for(auto u : rm) {
  //  cout << u << " ";
  //}
  //cout << endl;
  vector<int> rv;
  for(int i = 1; i < rm.size(); i++) {
    //cout << "DIST BETWEEN " << rm[i-1] << " " << rm[i] << ": " << dd(tc,rm[i-1],rm[i]) << "\n";
    rv.push_back(dd(tc,rm[i-1],rm[i]));
  }
  return rv;
}
int n;
 
vector<int> ok;
 
int tc = 1;
 
vector<pair<int,int>> vp;
 
void puugen(int j) {
  if(j == n+1) {
    unordered_set<int> t[11];
    //cout << " ----- TREE " << tc << " -----\n";
    /*for(auto u : vp) {
      cout << u.first << " " << u.second << "\n";
      }*/
    //tc++;
    //cout << " ------ END ---- \n";
    for(auto u : vp) {
      t[u.first].insert(u.second);
      t[u.second].insert(u.first);
    }
    if(dc2(n,t) == ok) {
      for(auto u : vp) {
	cout << u.first << " " << u.second << "\n";
      }
      exit(0);
    }
    return;
  }
  if(1){
    if(j == 1) {
      1+1;
      puugen(j+1);
    }
    else {
      for(int i = 1; i < j; i++) {
	vp.push_back({i,j});
	puugen(j+1);
	vp.pop_back();
      }
    }
  }
}
 
int main() {
  int t; cin>>t;
  if(t == 1) {
    cin >> n;
    set<int> t[11];
    for(int i = 0; i < n-1; i++) {
      int a,b; cin >> a >> b;
      t[a].insert(b);
      t[b].insert(a);
    }
    vector<int> vs = dc1(n,t);
    for(auto u : vs) {
      cout << u << " ";
    }
  }
  else {
    cin >> n;
    for(int i = 0; i < n-1; i++) {
      int je; cin >> je;
      ok.push_back(je);
    }
    puugen(1);
  }
}