Submission #223243

# Submission time Handle Problem Language Result Execution time Memory
223243 2020-04-15T06:04:24 Z _7_7_ Mergers (JOI19_mergers) C++14
10 / 100
203 ms 26232 KB
#include <bits/stdc++.h>                                           
 
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;
typedef unsigned long long ull;         
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 5e5 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;


int n, k, v, u, cnt[N], c[N], tmp, cntC[N], ans, root, a[N]; 
int tin[N], tout[N], timer, sum, cnt1[N];
bool bad[N];
vi g[N];

void dfs1 (int v, int p = 0) {
	cnt[v] = 1;
	tin[v] = ++timer;
	a[timer] = v;

	for (auto to : g[v])
		if (to != p) {
			dfs1(to, v); 
			cnt[v] += cnt[to];
		}

	tout[v] = timer;
}

void add (int x) {
	++cnt1[x];
	if (cnt1[x] == 1)
		sum += cntC[x];
}

void del (int x) {
	--cnt1[x];
	if (!cnt1[x])
		sum -= cntC[x];
}

void dfs (int v, int p = 0, bool keep = 0) {		
    int u = -1;
	for (auto to : g[v])
		if (to != p && (u == -1 || cnt[to] > cnt[u])) 
			u = to;

	for (auto to : g[v])
		if (to != p && to != u)
			dfs(to, v, 0);
	
	if (u != -1)	
		dfs(u, v, 1);
	
	for (auto to : g[v]) 
		if (to != p && to != u)	
			for (int i = tin[to]; i <= tout[to]; ++i) 
				add(c[a[i]]);

	add(c[v]);
		
	if (v != root && cnt[v] == sum) 
		bad[v] = 1;
	
	if (!keep) 
		for (int i = tin[v]; i <= tout[v]; ++i)
			del(c[a[i]]);
}

bool dfs2 (int v, int p = 0) {
	bool was = 0;
	for (auto to : g[v])
		if (to != p && dfs2(to, v)) 
			was = 1;

	if (!was && bad[v])
		++ans;

	return (bad[v] | was);
}


main () {
	cin >> n >> k;
	assert(n != 3000 || k != 200);
	if (n == 2) {
		cout << k - 1 << endl;
		return 0;
	}

	for (int i = 1; i < n; ++i) {
		cin >> v >> u;
		g[v].pb(u);
		g[u].pb(v);
	}

	for (int i = 1; i <= n; ++i) {
		cin >> c[i];
		++cntC[c[i]];
	}

	root = 1;
	while (sz(g[root]) == 1)
		++root;

	dfs1(root);
	dfs(root);
	dfs2(root);
	cout << (ans + 1) / 2 << endl;	
}


Compilation message

mergers.cpp:113:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 12 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 12 ms 12160 KB Output is correct
10 Correct 12 ms 12116 KB Output is correct
11 Correct 12 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 13 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 14 ms 12416 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12084 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 12 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 12 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 12 ms 12160 KB Output is correct
10 Correct 12 ms 12116 KB Output is correct
11 Correct 12 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 13 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 14 ms 12416 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12084 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 12 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 12 ms 12160 KB Output is correct
26 Correct 16 ms 12288 KB Output is correct
27 Correct 16 ms 12416 KB Output is correct
28 Correct 16 ms 12416 KB Output is correct
29 Correct 16 ms 12288 KB Output is correct
30 Runtime error 25 ms 24320 KB Execution killed with signal 11 (could be triggered by violating memory limits)
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 12 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 12 ms 12160 KB Output is correct
10 Correct 12 ms 12116 KB Output is correct
11 Correct 12 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 13 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 14 ms 12416 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12084 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 12 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 12 ms 12160 KB Output is correct
26 Correct 148 ms 17904 KB Output is correct
27 Correct 193 ms 17656 KB Output is correct
28 Correct 16 ms 12416 KB Output is correct
29 Correct 12 ms 12160 KB Output is correct
30 Correct 12 ms 12160 KB Output is correct
31 Correct 186 ms 17528 KB Output is correct
32 Correct 15 ms 12448 KB Output is correct
33 Correct 202 ms 26232 KB Output is correct
34 Correct 184 ms 17528 KB Output is correct
35 Incorrect 15 ms 12288 KB Output isn't correct
36 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 154 ms 17776 KB Output is correct
2 Correct 167 ms 18540 KB Output is correct
3 Correct 16 ms 12288 KB Output is correct
4 Correct 16 ms 12288 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 15 ms 12288 KB Output is correct
8 Correct 203 ms 18080 KB Output is correct
9 Incorrect 16 ms 12288 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 12 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 12 ms 12160 KB Output is correct
10 Correct 12 ms 12116 KB Output is correct
11 Correct 12 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 13 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 14 ms 12416 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12084 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 12 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 12 ms 12160 KB Output is correct
26 Correct 16 ms 12288 KB Output is correct
27 Correct 16 ms 12416 KB Output is correct
28 Correct 16 ms 12416 KB Output is correct
29 Correct 16 ms 12288 KB Output is correct
30 Runtime error 25 ms 24320 KB Execution killed with signal 11 (could be triggered by violating memory limits)
31 Halted 0 ms 0 KB -