Advertisement
chang2394

Untitled

Oct 25th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. // uzumaki naruto
  2. #include <bits/stdc++.h>
  3. #define each(v,c)  for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
  4. #define pb         push_back
  5. #define mp         make_pair
  6. #define sz(a)      ((int)(a.size()))
  7. #define all(a)     (a).begin(), (a).end()
  8. #define fi         first
  9. #define se         second
  10. #define TRACE
  11. using namespace std;
  12.  
  13. #ifdef TRACE
  14. #define debug(a,n)    cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
  15. #define dbg(args...)  {debug1,args; cerr<<endl;}
  16. #define pause()       cin.get();cin.get();
  17.  
  18. #else
  19.  
  20. #define debug(a,n)
  21. #define dbg(args...)
  22. #define pause()
  23.  
  24. #endif
  25.  
  26. struct debugger {
  27.     template<typename T> debugger& operator , (const T& v) {
  28.         cerr<<v<<" "; return *this;
  29.     }
  30. } debug1;
  31.  
  32. template <typename T1, typename T2>
  33. inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
  34.     return os << "(" << p.first << ", " << p.second << ")";
  35. }
  36.  
  37. template<typename T>
  38. inline ostream &operator << (ostream & os,const vector<T>& v) {
  39.     bool first = true; os << "[";
  40.     for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
  41.         if(!first) os << ", ";
  42.         os << *ii; first = false;
  43.     }
  44.     return os << "]";
  45. }
  46.  
  47. typedef long long LL;
  48. typedef pair<int,int> pii;
  49. typedef vector<int> vi;
  50. typedef pair<char,pii> pci;
  51.  
  52. const int NN = 2123;
  53.  
  54. vi ad[NN], con[NN];
  55. int p[NN], d[NN][NN];
  56.  
  57. int n;
  58. void init(){
  59.     for(int i = 1; i <= n; ++i){
  60.         ad[i].clear();
  61.         con[i].clear();
  62.         p[i] = -1;
  63.     }
  64. }
  65.  
  66. void dfs(int x,int f){
  67.     p[x] = f;
  68.  
  69.     for(int i = 0; i < sz(ad[x]); ++i){
  70.         int y = ad[x][i];
  71.         if (y == f) break;
  72.         if (p[y] != -1) continue;
  73.         dfs(y,x);
  74.     }
  75.  
  76.     for(int i = 1; i <= n; ++i){
  77.         if (p[i] != -1) continue;
  78.         if (d[i][x] < d[i][f])
  79.             dfs(i,x);
  80.     }
  81. }
  82.  
  83. void solve(){
  84.     int num;
  85.     cin >> n;
  86.  
  87.     init();
  88.     for(int i = 1; i <= n; ++i){
  89.         for(int k = 1; k <= n; ++k){
  90.             cin >> num;
  91.             d[i][num] = k;
  92.  
  93.             if (num == i) continue;
  94.             if (!sz(ad[i]))
  95.                 con[num].pb(i);
  96.             ad[i].pb(num);
  97.         }
  98.     }
  99.  
  100.     ad[1].pb(0);
  101.     dfs(1,0);
  102.  
  103.     for(int i = 2; i <= n; ++i)
  104.         cout << i << " " << p[i] << "\n";
  105.     cout << "\n";
  106. }
  107.  
  108. int main()
  109. {
  110.     ios_base::sync_with_stdio(0);
  111.     int t; cin >> t;
  112.     while(t--) solve();
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement