贪心求调
查看原帖
贪心求调
571841
ZVitality楼主2023/5/20 20:28

rt,30 pts,对了最后三个点。

#include <bits/stdc++.h>
using namespace std;

const int _ = 2005;

struct Edge { int v , w , nxt ; } e[_*2];
int head[_] , ecnt;
void Add( int u , int v , int w = 1 ) { e[ ++ecnt ] = Edge{ v , w , head[ u ] } ; head[ u ] = ecnt ; }

int n , cnt , ans;
int dfn[_] , fa[_];
int cover[_] , point[_];

void DFS( int x , int father ) {
  dfn[ ++cnt ] = x;
  for( int i = head[ x ] ; i ; i = e[ i ].nxt ) {
  	if( e[ i ].v != father ) {
  	  fa[ e[ i ].v ] = x;
  	  DFS( e[ i ].v , x );
  	}
  }
}

int main() {
  cin >> n;
  for( int i = 2 , x ; i <= n ; i++ ) {
  	cin >> x;
  	Add( x , i );
  	Add( i , x );
  }
  DFS( 1 , 0 );
  for( int i = cnt ; i >= 1 ; i-- ) {
  	int x = dfn[ i ];
  	if( cover[ x ] == 0 ) {
  	  if( point[ fa[ x ] ] == 0 && point[ fa[ fa[ x ] ] ] == 0 ) {
  	  	point[ fa[ fa[ x ] ] ] = 1;
  	  	ans++;
  	  }
  	  cover[ x ] = cover[ fa[ x ] ] = cover[ fa[ fa[ x ] ] ] =
  	  cover[ fa[ fa[ fa[ x ] ] ] ] = cover[ fa[ fa[ fa[ fa[ x ] ] ] ] ] = 1;
  	}
  }
  cout << ans << '\n';
}
2023/5/20 20:28
加载中...