//TLE #include #include #include #include using namespace std; template void resize(vector& vec, const H head){vec.resize(head);} template void resize(vector& vec, const H& head, const T ... tail){ vec.resize(head); for(auto& v: vec) resize(v, tail...); } //fill(vector, value); //v[x][y]...[z] = value; template void fill(V& vec_, const T& val){vec_ = val;}; template void fill(vector& vec, const T& val){ for(auto& v: vec) fill(v, val); } class UnionFindTree{ struct base_node{ int parent; int rank; int size; }; vector node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i node[y].rank){ node[y].parent = x; node[x].size += node[y].size; }else{ node[x].rank++; unite(x,y); } } }; bool is_kadomatsu_sequence(vector x){ if(x.size() != 3) return false; if(x[0] < x[1] && x[1] > x[2] && x[2] != x[0]) return true; if(x[0] > x[1] && x[1] < x[2] && x[2] != x[0]) return true; return false; } int main(){ int n; cin >> n; vector a(n); for(int i=0; i> a[i]; } vector> G(n); vector> e(n); vector u(n-1),v(n-1); for(int i=0; i> u[i] >> v[i]; u[i]--; v[i]--; G[u[i]].push_back(v[i]); G[v[i]].push_back(u[i]); e[u[i]].push_back(i); e[v[i]].push_back(i); } int ans = 0; for(int edge=0; edge<(1<<(n-1)); edge++){ UnionFindTree uft(n); for(int j=0; j>j)&1) uft.unite(u[j], v[j]); } function dfs = [&](int pos, int last, int first, int second){ if(first != -1 && second != -1){ if(is_kadomatsu_sequence({a[first], a[second], a[pos]})) return 1; else return -10000; } int ret = 0; if(first != -1 && second == -1){ for(int j=0; j cnt(n, 0); for(int i=0; i 0){ //ans = max(ans, uft.size(i)); ans = max(ans, cnt[uft.find(i)]/2); } } } //if(ans <= 2) ans = 0; cout << ans << endl; return 0; }