提交时间:2024-01-23 16:46:23

运行 ID: 125338

# include <bits/stdc++.h> using namespace std ; bool flag ; int l [1000005] , r [1000005] , v [1000005] ; int dfs ( int x , int y , int s ) { if ( x == -1 && y == -1 ) { return 0 ; } if ( x == -1 || ( y == -1 && x != y ) ) { flag = true ; return 0 ; } if ( v [x] != v [y] ) { flag = true; return 0 ; } return dfs ( l [x] , r [y] , 2) + dfs ( r [x] , l [y] , 2 ) + s ; } int main ( ) { int n , ans = 1 ; cin >> n ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> v [i] ; } for ( int i = 1 ; i <= n ; i ++ ) { cin >> l [i] >> r [i] ; } for ( int i = 1 ; i <= n ; i ++ ) { int s = dfs ( l [i] , r [i] , 3 ) ; if ( s > ans && ! flag ) { ans = s ; } flag = false ; } cout << ans << endl ; return 0 ; }