답안 #210176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210176 2020-03-16T18:41:38 Z arman_ferdous Mergers (JOI19_mergers) C++17
0 / 100
75 ms 30956 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 5e5+10;

int n, k, c[N];
vector<int> g[N], node[N];

int cur, vis[N], comp[N];
set<int> st;

int p[N];
void dfs(int u, int f) {
	p[u] = f;
	for(int v : g[u]) if(v != f)
		dfs(v, u);
}

void up(int u) {
	if(vis[u] != 0) return;
	vis[u] = cur;
	st.insert(c[u]);
	if(p[u] + 1) up(p[u]);
}

void process() {
	while(!st.empty()) {
		int x = *st.begin(); 
		st.erase(st.begin());
		for(int u : node[x])
			up(u);
		node[x].clear();
	}
}

int deg[N];

int main() {
	scanf("%d %d", &n, &k);
	for(int i = 1; i < n; i++) {
		int u, v; scanf("%d %d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1, -1);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		node[c[i]].push_back(i);
	}
	for(int i = 1; i <= k; i++) if(!node[i].empty()) {
		cur++;
		st.insert(i); 
		process();
	}
	for(int i = 2; i <= n; i++) if(vis[p[i]] != vis[i]) {
		deg[vis[p[i]]]++;
		deg[i]++;
	}
	int leaf = 0;
	for(int i = 1; i <= n; i++) if(deg[i] == 1) leaf++;
	if(leaf == 0) puts("0");
	else printf("%d\n", (leaf - 1) / 2 + 1);
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:40: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:42:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Incorrect 19 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Incorrect 19 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Incorrect 19 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 75 ms 30956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Incorrect 19 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -