#include <bits/stdc++.h> using namespace std; typedef long long int lld; #define rep(i,a,n) for(long long int i = (a); i <= (n); ++i) #define repI(i,a,n) for(int i = (a); i <= (n); ++i) #define repD(i,a,n) for(long long int i = (a); i >= (n); --i) #define repDI(i,a,n) for(int i = (a); i >= (n); --i) #define pb push_back #define mp make_pair #define ff first #define ss second typedef pair<lld,lld> PA; class InPrePost{ public: bool check(vector<int> A,vector<int> B){ sort(A.begin(),A.end()); sort(B.begin(),B.end()); int n = A.size(); if(n!=B.size()) return false; rep(i,0,n-1) if(A[i]!=B[i]) return false; return true; } bool tellMe(vector<string> S, vector<int> a1, vector<int> a2, vector<int> a3){ if(!check(a1,a2) || !check(a1,a3)) return false; int n = a1.size(); if(n<=1) return true; int root = a1[0]; vector<int> a1l,a1r,a2l,a2r,a3l,a3r; int leftn = -1; rep(i,0,n-1){ if(a2[i]==root){ leftn = i; } else if(leftn==-1){ a2l.pb(a2[i]); } else a2r.pb(a2[i]); } rep(i,1,n-1){ if(i<=leftn) a1l.pb(a1[i]); else a1r.pb(a1[i]); } rep(i,0,n-2){ if(i<leftn) a3l.pb(a3[i]); else a3r.pb(a3[i]); } map<string,vector<int> > M; M[S[0]] = a1l; M[S[2]] = a2l; M[S[4]] = a3l; if(!tellMe(S,M["pre"],M["in"],M["post"])) return false; M[S[1]] = a1r; M[S[3]] = a2r; M[S[5]] = a3r; if(!tellMe(S,M["pre"],M["in"],M["post"])) return false; return true; } string isPossible(vector<string> S, vector<int> a1, vector<int> a2, vector<int> a3){ if(tellMe(S,a1,a2,a3)) return "Possible"; else return "Impossible"; } };
|