提交时间:2023-10-28 10:31:00

运行 ID: 107810

#include<iostream> #include<vector> using namespace std; int a[30], n, t; //int b[30][30]; vector<int> b[30], in; bool s = false; void f(int k){ if(s) return; if(k == n){ for(int i = 0; i < n; i++){ for(int j = 0; j < b[i+1].size(); j++){ if(a[i] == a[b[i+1][j]-1]){ return; } } } for(int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; s = true; return; } for(int i = 1; i <= 4; i++){ a[k] = i; f(k + 1); } } int main(){ cin >> n; while(cin >> t){in.push_back(t);} int j = n; for(int i = in.size() - 1; i >= 0; i--){ if(in[i] != j)b[j].push_back(in[i]); if(in[i] == j) j--; } // for(int i = 1; i <= n; i++){ // for(int j = 0; j < 2; j++){ // cout << b[i][j] << ' '; // } // cout << endl; // } f(0); return 0; }