Submission #223314

# Submission time Handle Problem Language Result Execution time Memory
223314 2020-04-15T07:10:40 Z _7_7_ Mergers (JOI19_mergers) C++14
100 / 100
1118 ms 75384 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], m; 
int tin[N], tout[N], timer, sum, cnt1[N], cnt2[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; 
		++m;
	}
	
	if (!keep) 
		for (int i = tin[v]; i <= tout[v]; ++i)
			del(c[a[i]]);
}
 
void dfs2 (int v, int p = 0) {
    cnt2[v] = bad[v];
	int cnt = 0;

	for (auto to : g[v])
		if (to != p) { 
		    dfs2(to, v);
		    if (cnt2[to])
				++cnt; 
			cnt2[v] += cnt2[to];
		}
		
 
	if (bad[v] && (!cnt || (cnt == 1 && cnt2[v] == m))) 
		++ans; 
}
 
 
main () {
	scanf("%d%d", &n, &k);
	if (n == 2) {
		cout << k - 1 << endl;
		return 0;
	}
 
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &u, &v);
		g[v].pb(u);
		g[u].pb(v);
	}
 
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &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:120:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
mergers.cpp: In function 'int main()':
mergers.cpp:121: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:128: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:134: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 13 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 11 ms 12160 KB Output is correct
6 Correct 11 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 12160 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 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 12 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12160 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 13 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 11 ms 12160 KB Output is correct
6 Correct 11 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 12160 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 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 12 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12160 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 11 ms 12160 KB Output is correct
26 Correct 13 ms 12288 KB Output is correct
27 Correct 13 ms 12288 KB Output is correct
28 Correct 14 ms 12416 KB Output is correct
29 Correct 14 ms 12288 KB Output is correct
30 Correct 14 ms 12288 KB Output is correct
31 Correct 12 ms 12160 KB Output is correct
32 Correct 14 ms 12544 KB Output is correct
33 Correct 12 ms 12160 KB Output is correct
34 Correct 14 ms 12288 KB Output is correct
35 Correct 14 ms 12288 KB Output is correct
36 Correct 14 ms 12288 KB Output is correct
37 Correct 14 ms 12288 KB Output is correct
38 Correct 13 ms 12160 KB Output is correct
39 Correct 14 ms 12288 KB Output is correct
40 Correct 14 ms 12288 KB Output is correct
41 Correct 14 ms 12288 KB Output is correct
42 Correct 14 ms 12288 KB Output is correct
43 Correct 14 ms 12544 KB Output is correct
44 Correct 12 ms 12160 KB Output is correct
45 Correct 14 ms 12416 KB Output is correct
46 Correct 14 ms 12288 KB Output is correct
47 Correct 12 ms 12160 KB Output is correct
48 Correct 14 ms 12288 KB Output is correct
49 Correct 14 ms 12288 KB Output is correct
50 Correct 14 ms 12416 KB Output is correct
51 Correct 15 ms 12288 KB Output is correct
52 Correct 14 ms 12288 KB Output is correct
53 Correct 13 ms 12288 KB Output is correct
54 Correct 14 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 11 ms 12160 KB Output is correct
6 Correct 11 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 12160 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 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 12 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12160 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 13 ms 12160 KB Output is correct
26 Correct 75 ms 18032 KB Output is correct
27 Correct 117 ms 17656 KB Output is correct
28 Correct 14 ms 12288 KB Output is correct
29 Correct 12 ms 12160 KB Output is correct
30 Correct 12 ms 12160 KB Output is correct
31 Correct 135 ms 17852 KB Output is correct
32 Correct 13 ms 12292 KB Output is correct
33 Correct 141 ms 26336 KB Output is correct
34 Correct 119 ms 17656 KB Output is correct
35 Correct 14 ms 12288 KB Output is correct
36 Correct 119 ms 18680 KB Output is correct
37 Correct 16 ms 12288 KB Output is correct
38 Correct 14 ms 12288 KB Output is correct
39 Correct 77 ms 18032 KB Output is correct
40 Correct 14 ms 12544 KB Output is correct
41 Correct 116 ms 17656 KB Output is correct
42 Correct 131 ms 20472 KB Output is correct
43 Correct 12 ms 12160 KB Output is correct
44 Correct 152 ms 26744 KB Output is correct
45 Correct 121 ms 22264 KB Output is correct
46 Correct 13 ms 12288 KB Output is correct
47 Correct 14 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 18032 KB Output is correct
2 Correct 80 ms 18924 KB Output is correct
3 Correct 14 ms 12288 KB Output is correct
4 Correct 14 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 13 ms 12288 KB Output is correct
8 Correct 125 ms 18144 KB Output is correct
9 Correct 14 ms 12288 KB Output is correct
10 Correct 123 ms 17784 KB Output is correct
11 Correct 12 ms 12160 KB Output is correct
12 Correct 120 ms 17760 KB Output is correct
13 Correct 129 ms 18168 KB Output is correct
14 Correct 129 ms 18552 KB Output is correct
15 Correct 89 ms 18072 KB Output is correct
16 Correct 13 ms 12288 KB Output is correct
17 Correct 13 ms 12160 KB Output is correct
18 Correct 85 ms 18796 KB Output is correct
19 Correct 142 ms 23288 KB Output is correct
20 Correct 14 ms 12288 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 85 ms 18416 KB Output is correct
23 Correct 15 ms 12288 KB Output is correct
24 Correct 135 ms 17784 KB Output is correct
25 Correct 131 ms 21752 KB Output is correct
26 Correct 14 ms 12288 KB Output is correct
27 Correct 16 ms 12416 KB Output is correct
28 Correct 15 ms 12288 KB Output is correct
29 Correct 13 ms 12288 KB Output is correct
30 Correct 14 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 11 ms 12160 KB Output is correct
6 Correct 11 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 12160 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 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 12 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 12 ms 12160 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 11 ms 12160 KB Output is correct
26 Correct 13 ms 12288 KB Output is correct
27 Correct 13 ms 12288 KB Output is correct
28 Correct 14 ms 12416 KB Output is correct
29 Correct 14 ms 12288 KB Output is correct
30 Correct 14 ms 12288 KB Output is correct
31 Correct 12 ms 12160 KB Output is correct
32 Correct 14 ms 12544 KB Output is correct
33 Correct 12 ms 12160 KB Output is correct
34 Correct 14 ms 12288 KB Output is correct
35 Correct 14 ms 12288 KB Output is correct
36 Correct 14 ms 12288 KB Output is correct
37 Correct 14 ms 12288 KB Output is correct
38 Correct 13 ms 12160 KB Output is correct
39 Correct 14 ms 12288 KB Output is correct
40 Correct 14 ms 12288 KB Output is correct
41 Correct 14 ms 12288 KB Output is correct
42 Correct 14 ms 12288 KB Output is correct
43 Correct 14 ms 12544 KB Output is correct
44 Correct 12 ms 12160 KB Output is correct
45 Correct 14 ms 12416 KB Output is correct
46 Correct 14 ms 12288 KB Output is correct
47 Correct 12 ms 12160 KB Output is correct
48 Correct 14 ms 12288 KB Output is correct
49 Correct 14 ms 12288 KB Output is correct
50 Correct 14 ms 12416 KB Output is correct
51 Correct 15 ms 12288 KB Output is correct
52 Correct 14 ms 12288 KB Output is correct
53 Correct 13 ms 12288 KB Output is correct
54 Correct 14 ms 12288 KB Output is correct
55 Correct 13 ms 12160 KB Output is correct
56 Correct 75 ms 18032 KB Output is correct
57 Correct 117 ms 17656 KB Output is correct
58 Correct 14 ms 12288 KB Output is correct
59 Correct 12 ms 12160 KB Output is correct
60 Correct 12 ms 12160 KB Output is correct
61 Correct 135 ms 17852 KB Output is correct
62 Correct 13 ms 12292 KB Output is correct
63 Correct 141 ms 26336 KB Output is correct
64 Correct 119 ms 17656 KB Output is correct
65 Correct 14 ms 12288 KB Output is correct
66 Correct 119 ms 18680 KB Output is correct
67 Correct 16 ms 12288 KB Output is correct
68 Correct 14 ms 12288 KB Output is correct
69 Correct 77 ms 18032 KB Output is correct
70 Correct 14 ms 12544 KB Output is correct
71 Correct 116 ms 17656 KB Output is correct
72 Correct 131 ms 20472 KB Output is correct
73 Correct 12 ms 12160 KB Output is correct
74 Correct 152 ms 26744 KB Output is correct
75 Correct 121 ms 22264 KB Output is correct
76 Correct 13 ms 12288 KB Output is correct
77 Correct 14 ms 12288 KB Output is correct
78 Correct 78 ms 18032 KB Output is correct
79 Correct 80 ms 18924 KB Output is correct
80 Correct 14 ms 12288 KB Output is correct
81 Correct 14 ms 12288 KB Output is correct
82 Correct 12 ms 12160 KB Output is correct
83 Correct 12 ms 12160 KB Output is correct
84 Correct 13 ms 12288 KB Output is correct
85 Correct 125 ms 18144 KB Output is correct
86 Correct 14 ms 12288 KB Output is correct
87 Correct 123 ms 17784 KB Output is correct
88 Correct 12 ms 12160 KB Output is correct
89 Correct 120 ms 17760 KB Output is correct
90 Correct 129 ms 18168 KB Output is correct
91 Correct 129 ms 18552 KB Output is correct
92 Correct 89 ms 18072 KB Output is correct
93 Correct 13 ms 12288 KB Output is correct
94 Correct 13 ms 12160 KB Output is correct
95 Correct 85 ms 18796 KB Output is correct
96 Correct 142 ms 23288 KB Output is correct
97 Correct 14 ms 12288 KB Output is correct
98 Correct 12 ms 12160 KB Output is correct
99 Correct 85 ms 18416 KB Output is correct
100 Correct 15 ms 12288 KB Output is correct
101 Correct 135 ms 17784 KB Output is correct
102 Correct 131 ms 21752 KB Output is correct
103 Correct 14 ms 12288 KB Output is correct
104 Correct 16 ms 12416 KB Output is correct
105 Correct 15 ms 12288 KB Output is correct
106 Correct 13 ms 12288 KB Output is correct
107 Correct 14 ms 12288 KB Output is correct
108 Correct 582 ms 43620 KB Output is correct
109 Correct 1024 ms 61816 KB Output is correct
110 Correct 1004 ms 75384 KB Output is correct
111 Correct 1053 ms 72216 KB Output is correct
112 Correct 1038 ms 60500 KB Output is correct
113 Correct 617 ms 45404 KB Output is correct
114 Correct 875 ms 40184 KB Output is correct
115 Correct 868 ms 40056 KB Output is correct
116 Correct 925 ms 41464 KB Output is correct
117 Correct 986 ms 44536 KB Output is correct
118 Correct 874 ms 40000 KB Output is correct
119 Correct 980 ms 44456 KB Output is correct
120 Correct 1033 ms 64764 KB Output is correct
121 Correct 964 ms 44464 KB Output is correct
122 Correct 940 ms 42872 KB Output is correct
123 Correct 600 ms 45796 KB Output is correct
124 Correct 1118 ms 43756 KB Output is correct