답안 #133254

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
133254 2019-07-20T10:01:10 Z ekrem Mergers (JOI19_mergers) C++
0 / 100
202 ms 113516 KB
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (k+k)
#define sag (k+k+1)
#define orta ((bas+son)/2)
#define coc g[node][i]
#define LOGN 20
#define mod 1000000007
#define inf 1000000009
#define N 1000005
using namespace std;

typedef long long ll;
typedef pair < int , int > ii;

int n, k, root, ans, deg[N], l[N], a[N], der[N], par[LOGN + 5][N];
vector < int > g[N], cik[N];
set < int > s[N];
set < int > :: iterator it;

void hazirla(int node, int pr, int dr){
	der[node] = dr;
	par[0][node] = pr;
	for(int i = 0; i < g[node].size(); i++)
		if(coc != pr)
			hazirla(coc, node, dr + 1);
}

int lca(int u, int v){
	if(!u or !v)
		return u + v;
	if(der[u] < der[v])
		swap(u, v);
	for(int i = LOGN; i >= 0; i--)
		if(der[par[i][u]] >= der[v])
			u = par[i][u];
	if(u == v)
		return u;
	for(int i = LOGN; i >= 0; i--)
		if(par[i][u] != par[i][v]){
			u = par[i][u];
			v = par[i][v];
		}
	return par[0][u];
}

int dfs(int node, int par){
	int don = 0;
	for(int i = 0; i < g[node].size(); i++)
		if(coc != par){
			don += dfs(coc, node);
			if(s[coc].size() > s[node].size())
				swap(s[node], s[coc]);
			for(it = s[coc].begin(); it != s[coc].end(); it++)
				s[node].insert(*it);
		}
	// cout << node << " " << don << endl;
	s[node].insert(a[node]);
	for(int i = 0; i < cik[node].size(); i++)
		s[node].erase(cik[node][i]);
	// cout << node << " " << don << endl;
	if(don > 1 or !par){
		ans += don/2 + don%2;
		don = 0;
	}
	if(s[node].empty() and par){
		// cout << node << " " << ans << endl;
		don = 1;
	}
	// cout << node << " " << don << endl;
	return don;
}

int main() {
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	scanf("%d %d",&n ,&k);
	for(int i = 1; i < n; i++){
		int u, v;
		scanf("%d %d",&u ,&v);
		g[v].pb(u);
		g[u].pb(v);
		if(!deg[u]++)root = u;
		if(!deg[v]++)root = v;
	}
	root = 1;
	hazirla(root, 0, 1);
	for(int i = 1; i <= LOGN; i++)
		for(int j = 1; j <= n; j++)
			par[i][j] = par[i - 1][par[i - 1][j]];
	
	for(int i = 1; i <= n; i++){
		scanf("%d",a + i);
		l[a[i]] = lca(l[a[i]], i);
	}
	for(int i = 1; i <= k; i++)
		cik[l[i]].pb(i);
	dfs(root, 0);
	printf("%d",ans);
	return 0;
}

Compilation message

mergers.cpp: In function 'void hazirla(int, int, int)':
mergers.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < g[node].size(); i++)
                 ~~^~~~~~~~~~~~~~~~
mergers.cpp: In function 'int dfs(int, int)':
mergers.cpp:52:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < g[node].size(); i++)
                 ~~^~~~~~~~~~~~~~~~
mergers.cpp:62:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < cik[node].size(); i++)
                 ~~^~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n ,&k);
  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u ,&v);
   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",a + i);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 94428 KB Output is correct
2 Correct 86 ms 94456 KB Output is correct
3 Correct 87 ms 94496 KB Output is correct
4 Correct 86 ms 94456 KB Output is correct
5 Correct 87 ms 94484 KB Output is correct
6 Correct 86 ms 94456 KB Output is correct
7 Correct 86 ms 94456 KB Output is correct
8 Correct 87 ms 94392 KB Output is correct
9 Correct 86 ms 94460 KB Output is correct
10 Correct 94 ms 94456 KB Output is correct
11 Correct 87 ms 94556 KB Output is correct
12 Incorrect 86 ms 94456 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 94428 KB Output is correct
2 Correct 86 ms 94456 KB Output is correct
3 Correct 87 ms 94496 KB Output is correct
4 Correct 86 ms 94456 KB Output is correct
5 Correct 87 ms 94484 KB Output is correct
6 Correct 86 ms 94456 KB Output is correct
7 Correct 86 ms 94456 KB Output is correct
8 Correct 87 ms 94392 KB Output is correct
9 Correct 86 ms 94460 KB Output is correct
10 Correct 94 ms 94456 KB Output is correct
11 Correct 87 ms 94556 KB Output is correct
12 Incorrect 86 ms 94456 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 94428 KB Output is correct
2 Correct 86 ms 94456 KB Output is correct
3 Correct 87 ms 94496 KB Output is correct
4 Correct 86 ms 94456 KB Output is correct
5 Correct 87 ms 94484 KB Output is correct
6 Correct 86 ms 94456 KB Output is correct
7 Correct 86 ms 94456 KB Output is correct
8 Correct 87 ms 94392 KB Output is correct
9 Correct 86 ms 94460 KB Output is correct
10 Correct 94 ms 94456 KB Output is correct
11 Correct 87 ms 94556 KB Output is correct
12 Incorrect 86 ms 94456 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 113516 KB Output is correct
2 Correct 200 ms 112624 KB Output is correct
3 Incorrect 90 ms 94968 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 94428 KB Output is correct
2 Correct 86 ms 94456 KB Output is correct
3 Correct 87 ms 94496 KB Output is correct
4 Correct 86 ms 94456 KB Output is correct
5 Correct 87 ms 94484 KB Output is correct
6 Correct 86 ms 94456 KB Output is correct
7 Correct 86 ms 94456 KB Output is correct
8 Correct 87 ms 94392 KB Output is correct
9 Correct 86 ms 94460 KB Output is correct
10 Correct 94 ms 94456 KB Output is correct
11 Correct 87 ms 94556 KB Output is correct
12 Incorrect 86 ms 94456 KB Output isn't correct
13 Halted 0 ms 0 KB -