Submission #210363

# Submission time Handle Problem Language Result Execution time Memory
210363 2020-03-17T07:35:55 Z arman_ferdous Mergers (JOI19_mergers) C++14
0 / 100
90 ms 33248 KB
#include <bits/stdc++.h>
using namespace std;

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

int n, k, c[N], dsu[N], deg[N];
vector<int> g[N], node[N];
vector<pair<int,int>> E;

int lev[N], par[N];
int dfs(int u, int f, int d) {
	par[u] = f;
	lev[u] = d;
	for(int v : g[u]) if(v != f)
		dfs(v, u, d + 1);
}

int find(int u) {
	if(dsu[u] == u) return u;
	return dsu[u] = find(dsu[u]);
}

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);
		E.push_back({u, v});
	}
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		node[c[i]].push_back(i);
	}
	iota(dsu + 1, dsu + 1 + n, 1);
	dfs(1, 0, 0);
	for(int i = 1; i <= k; i++) if((int)node[i].size() > 1) {
		for(int j = 1; j < (int)node[i].size(); j++) {
			int u = node[i][j - 1], v = node[i][j];
			while(u != v) {
				if(lev[u] > lev[v]) swap(u, v);
				dsu[v] = find(par[v]);
				v = dsu[v];
			}
		}
	}
	for(auto e : E) {
		if(find(e.first) != find(e.second)) 
			deg[e.first]++, deg[e.second]++;
	} int cnt = 0;
	for(int i = 1; i <= n; ++i) if(deg[i] == 1) cnt++;
	printf("%d\n", (cnt + 1) >> 1);
	return 0;
}

Compilation message

mergers.cpp: In function 'int dfs(int, int, int)':
mergers.cpp:17:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
mergers.cpp: In function 'int main()':
mergers.cpp:25: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:27: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:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Incorrect 20 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Incorrect 20 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Incorrect 20 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 30308 KB Output is correct
2 Correct 90 ms 33248 KB Output is correct
3 Incorrect 24 ms 24056 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Incorrect 20 ms 23928 KB Output isn't correct
4 Halted 0 ms 0 KB -