Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <vector>
- #include <stdlib.h>
- #include <stdio.h>
- #include <fstream>
- #include <stack>
- #include <map>
- #include <cstring>
- #include <cmath>
- #include <set>
- #include <iterator>
- #include <cmath>
- #include <locale>
- #include <queue>
- #include<deque>
- #include <stack>
- using namespace std;
- const int MOD = 1e9+7;
- int main()
- {
- int N = 0, a = 0, m = 0,k = 0 ,t = 0, mi =-1;
- cin >> N;
- vector<stack<int> > s(N);
- for(auto i = 0; i < N; ++i)
- {
- while(!s[i].empty())
- {
- s[i].pop();
- }
- }
- if (N == 0)
- return 0;
- vector<pair<int, int> > answ;
- for(auto i = 0; i < N; ++i)
- {
- cin >> m;
- if (m != 0)
- {
- for(auto j = 0; j < m; ++j)
- {
- cin >> a;
- if(a == i + 1)
- k++;
- s[i].push(a);
- t++;
- }
- }
- }
- if(k == t || N == 1)
- return 0;
- t = 0;
- k = 0;
- if (N == 2)
- {
- while(!s[1].empty())
- {
- s[0].push(s[1].top());
- s[1].pop();
- answ.push_back(make_pair(2, 1));
- }
- while (!s[0].empty())
- {
- t++;
- a = s[0].top();
- s[0].pop();
- s[1].push(a);
- if (a != 1)
- answ.push_back(make_pair(1, 2));
- if (t == 1)
- {
- mi = a;
- }
- // cout << mi << " fgsasg " << a << endl;
- if (t > 1)
- {
- if (a > mi)
- {
- cout << 0;
- return 0;
- }
- if (mi > a)
- mi = a;
- }
- }
- }
- if (N > 2)
- {
- for (int i = 1; i < s.size() ; ++i)
- {
- while(!s[i].empty())
- {
- a = s[i].top();
- s[i].pop();
- s[0].push(a);
- answ.push_back(make_pair(i + 1, 1));
- //cout << s[0].top() << endl;
- }
- }
- while(!s[0].empty())
- {
- a = s[0].top();
- s[0].pop();
- if (a == 1)
- {
- s[N - 1].push(a);
- answ.push_back(make_pair(1, N));
- }
- else{
- s[a - 1].push(a);
- answ.push_back(make_pair(1, a));
- }
- }
- while(!s[N - 1].empty())
- {
- a = s[N - 1].top();
- s[N - 1].pop();
- if (a == 1){
- s[0].push(a);
- answ.push_back(make_pair(N, 1));
- }
- else{
- s[N - 2].push(a);
- // cout << a << " " << endl;
- answ.push_back(make_pair(N, N - 1));
- }
- if (s[N - 1].size() == 0)
- break;
- }
- if (s[N - 2].size() > 0)
- a = s[N - 2].top();
- while(a == N)
- {
- s[N - 2].pop();
- s[N - 1].push(a);
- //cout << a << " dasdoajod" << endl;
- answ.push_back(make_pair(N - 1, N));
- if (s[N - 2].size() == 0)
- break;
- a = s[N - 2].top();
- }
- }
- for(int i = 0; i < answ.size(); ++i)
- {
- cout << answ[i].first << " " << answ[i].second << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement