답안 #332574

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332574 2020-12-02T22:02:03 Z CaroLinda Mergers (JOI19_mergers) C++14
0 / 100
71 ms 34404 KB
#include <bits/stdc++.h>

#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )
#define ll long long

const int MAXN = 5e5+10 ;

using namespace std ;

int n , k ;
int state[MAXN] , deg[MAXN] ;
int dsu[MAXN] , qtdState[MAXN] ;
vector<int> adj[MAXN] ;
map<int,int> *ptr[MAXN] ;

int find(int x) { return (x==dsu[x]) ? x : (dsu[x] = find(dsu[x]) ) ; } 

void join(int x, int y )
{
	x = find(x) ;
	y = find(y) ;

	if(x  == y ) return ;

	if(rand() % 2 ) swap(x,y) ;

	deg[x] += deg[y] ;
	dsu[y] = x ;

}

int dfs(int x, int father)
{
	int bc = -1 ;
	int subBc = 0 ;
	int sub = 1 ;

	for(auto e : adj[x] )
	{

		if(e == father ) continue ;

		int subChild = dfs(e,x) ;
		sub += subChild ;

		if(subChild > subBc )
		{
			subBc = subChild ;
			bc = e ;			
		}

	}

	if(bc == -1 ) ptr[x] = new map<int,int> ;
	else ptr[x] = ptr[bc] ;
	
	map<int,int> &it = (*ptr[x] );

	if( sz(it) == 0 && bc != -1 )
	{
		deg[ find(bc) ]++ ;
		deg[ find(x) ]++ ;
	}
	else if(bc != -1 ) join(x, bc) ;

	for(auto e : adj[x] )
	{
		if(e == father || e == bc ) continue ;
		if( sz( (*ptr[e] ) ) == 0 ) 
		{
			deg[ find(x) ]++ ;
			deg[ find(e) ]++ ;
			continue ;
		}

		join(x, e) ;

		for(auto p : (*ptr[e] ) )
		{
			it[p.first] += p.second ;
			if( it[p.first]  == qtdState[p.first] )	
				it.erase( it.find(p.first) ) ;		
		}
	}

	it[ state[x] ]++ ;

	if( it[ state[x] ] == qtdState[ state[x] ] )
		it.erase( it.find( state[x] ) ) ;


}

int main()
{
	scanf("%d %d", &n, &k ) ;
	for(int i = 0 , u , v ; i < n-1 ; i++ )
	{
		scanf("%d %d", &u, &v ) ;

		adj[u].push_back(v) ;
		adj[v].push_back(u) ;

	}
	for(int i = 1 ; i <= n ; i++ ) 
	{
		scanf("%d", &state[i] ) ;

		dsu[i] = i ;
		qtdState[ state[i] ]++ ;
	}

	dfs(1,-1) ;

	int qtd = 0 ;

	for(int i = 1 ; i <= n ; i++ )
		if( find(i) == i && deg[i] == 1 )  qtd++ ;

	printf("%d\n", (qtd+1)/2 ) ;

}

Compilation message

mergers.cpp: In function 'int dfs(int, int)':
mergers.cpp:93:1: warning: no return statement in function returning non-void [-Wreturn-type]
   93 | }
      | ^
mergers.cpp: In function 'int main()':
mergers.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   97 |  scanf("%d %d", &n, &k ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  100 |   scanf("%d %d", &u, &v ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |   scanf("%d", &state[i] ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 24300 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 24300 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 24300 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 71 ms 34404 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 24300 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -