Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uzumaki naruto
- #include <bits/stdc++.h>
- #define each(v,c) for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
- #define pb push_back
- #define mp make_pair
- #define sz(a) ((int)(a.size()))
- #define all(a) (a).begin(), (a).end()
- #define fi first
- #define se second
- #define TRACE
- using namespace std;
- #ifdef TRACE
- #define debug(a,n) cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
- #define dbg(args...) {debug1,args; cerr<<endl;}
- #define pause() cin.get();cin.get();
- #else
- #define debug(a,n)
- #define dbg(args...)
- #define pause()
- #endif
- struct debugger {
- template<typename T> debugger& operator , (const T& v) {
- cerr<<v<<" "; return *this;
- }
- } debug1;
- template <typename T1, typename T2>
- inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
- return os << "(" << p.first << ", " << p.second << ")";
- }
- template<typename T>
- inline ostream &operator << (ostream & os,const vector<T>& v) {
- bool first = true; os << "[";
- for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
- if(!first) os << ", ";
- os << *ii; first = false;
- }
- return os << "]";
- }
- typedef long long LL;
- typedef pair<int,int> pii;
- typedef vector<int> vi;
- typedef pair<char,pii> pci;
- const int NN = 2123;
- vi ad[NN], con[NN];
- int p[NN], d[NN][NN];
- int n;
- void init(){
- for(int i = 1; i <= n; ++i){
- ad[i].clear();
- con[i].clear();
- p[i] = -1;
- }
- }
- void dfs(int x,int f){
- p[x] = f;
- for(int i = 0; i < sz(ad[x]); ++i){
- int y = ad[x][i];
- if (y == f) break;
- if (p[y] != -1) continue;
- dfs(y,x);
- }
- for(int i = 1; i <= n; ++i){
- if (p[i] != -1) continue;
- if (d[i][x] < d[i][f])
- dfs(i,x);
- }
- }
- void solve(){
- int num;
- cin >> n;
- init();
- for(int i = 1; i <= n; ++i){
- for(int k = 1; k <= n; ++k){
- cin >> num;
- d[i][num] = k;
- if (num == i) continue;
- if (!sz(ad[i]))
- con[num].pb(i);
- ad[i].pb(num);
- }
- }
- ad[1].pb(0);
- dfs(1,0);
- for(int i = 2; i <= n; ++i)
- cout << i << " " << p[i] << "\n";
- cout << "\n";
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- int t; cin >> t;
- while(t--) solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement