Submission #107228

# Submission time Handle Problem Language Result Execution time Memory
107228 2019-04-22T18:02:29 Z eriksuenderhauf Mergers (JOI19_mergers) C++11
100 / 100
1522 ms 86620 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int deg[MAXN], col[MAXN], lo[MAXN], disc[MAXN];
int ind[MAXN];
pii edg[MAXN];
vii adj[MAXN];
int t = 0, vis[MAXN], cnt = 0, br[MAXN];

void dfs(int u, int p) {
	disc[u] = lo[u] = t++;
	vis[u] = 1;
	for (pii nx: adj[u]) {
		int v, id; tie(v, id) = nx;
		if (!vis[v]) {
			dfs(v, id);
			lo[u] = min(lo[u], lo[v]);
			if (disc[u] < lo[v])
				br[id] = 1;
		} else if (id != p)
			lo[u] = min(lo[u], disc[v]);
	}
}

int curC = 0;

void bfs(int cur) {
	deque<int> pq;
	pq.pb(cur);
	vis[cur] = 1;
	while (!pq.empty()) {
		int u = pq.front(); pq.pop_front();
		ind[u] = curC;
		for (pii nx: adj[u]) {
			int v, id; tie(v, id) = nx;
			if (!br[id] && !vis[v]) {
				pq.pb(v);
				vis[v] = 1;
			}
		}
	}
	curC++;
}

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		edg[i] = {--u, --v};
	}
	for (int i = 0; i < n; i++) {
		ni(col[i]); --col[i];
	}
	for (int i = 1; i < n; i++) {
		int u, v; tie(u, v) = edg[i];
		u = col[u], v = col[v];
		if (u == v) continue;
		adj[u].pb({v, cnt});
		adj[v].pb({u, cnt});
		cnt++;
	}
	dfs(0, -1);
	mem(vis,0);
	for (int i = 0; i < k; i++)
		if (!vis[i])
			bfs(i);
	for (int i = 0; i < k; i++) {
		for (pii nx: adj[i]) {
			int u, id; tie(u, id) = nx;
			if (!br[id]) continue;
			deg[ind[u]]++;
		}
	}
	int lvs = 0;
	for (int i = 0; i < curC; i++)
		if (deg[i] == 1)
			lvs++;
	pri((lvs+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:79: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:82: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:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
mergers.cpp:86:3: note: in expansion of macro 'ni'
   ni(col[i]); --col[i];
   ^~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 27776 KB Output is correct
2 Correct 24 ms 27768 KB Output is correct
3 Correct 27 ms 27776 KB Output is correct
4 Correct 26 ms 27844 KB Output is correct
5 Correct 26 ms 27776 KB Output is correct
6 Correct 26 ms 27776 KB Output is correct
7 Correct 29 ms 27904 KB Output is correct
8 Correct 26 ms 27776 KB Output is correct
9 Correct 25 ms 27776 KB Output is correct
10 Correct 25 ms 27904 KB Output is correct
11 Correct 26 ms 27896 KB Output is correct
12 Correct 25 ms 27768 KB Output is correct
13 Correct 25 ms 27768 KB Output is correct
14 Correct 25 ms 27776 KB Output is correct
15 Correct 25 ms 27776 KB Output is correct
16 Correct 29 ms 27776 KB Output is correct
17 Correct 25 ms 27776 KB Output is correct
18 Correct 25 ms 27776 KB Output is correct
19 Correct 25 ms 27768 KB Output is correct
20 Correct 27 ms 27768 KB Output is correct
21 Correct 24 ms 27776 KB Output is correct
22 Correct 27 ms 27776 KB Output is correct
23 Correct 30 ms 27768 KB Output is correct
24 Correct 26 ms 27768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 27776 KB Output is correct
2 Correct 24 ms 27768 KB Output is correct
3 Correct 27 ms 27776 KB Output is correct
4 Correct 26 ms 27844 KB Output is correct
5 Correct 26 ms 27776 KB Output is correct
6 Correct 26 ms 27776 KB Output is correct
7 Correct 29 ms 27904 KB Output is correct
8 Correct 26 ms 27776 KB Output is correct
9 Correct 25 ms 27776 KB Output is correct
10 Correct 25 ms 27904 KB Output is correct
11 Correct 26 ms 27896 KB Output is correct
12 Correct 25 ms 27768 KB Output is correct
13 Correct 25 ms 27768 KB Output is correct
14 Correct 25 ms 27776 KB Output is correct
15 Correct 25 ms 27776 KB Output is correct
16 Correct 29 ms 27776 KB Output is correct
17 Correct 25 ms 27776 KB Output is correct
18 Correct 25 ms 27776 KB Output is correct
19 Correct 25 ms 27768 KB Output is correct
20 Correct 27 ms 27768 KB Output is correct
21 Correct 24 ms 27776 KB Output is correct
22 Correct 27 ms 27776 KB Output is correct
23 Correct 30 ms 27768 KB Output is correct
24 Correct 26 ms 27768 KB Output is correct
25 Correct 32 ms 27748 KB Output is correct
26 Correct 32 ms 27896 KB Output is correct
27 Correct 30 ms 27908 KB Output is correct
28 Correct 32 ms 28024 KB Output is correct
29 Correct 31 ms 27904 KB Output is correct
30 Correct 26 ms 27904 KB Output is correct
31 Correct 27 ms 27768 KB Output is correct
32 Correct 28 ms 28160 KB Output is correct
33 Correct 25 ms 27776 KB Output is correct
34 Correct 28 ms 27904 KB Output is correct
35 Correct 29 ms 28024 KB Output is correct
36 Correct 27 ms 27776 KB Output is correct
37 Correct 31 ms 27904 KB Output is correct
38 Correct 29 ms 27748 KB Output is correct
39 Correct 28 ms 28024 KB Output is correct
40 Correct 28 ms 27776 KB Output is correct
41 Correct 32 ms 27804 KB Output is correct
42 Correct 28 ms 27920 KB Output is correct
43 Correct 30 ms 27776 KB Output is correct
44 Correct 26 ms 27776 KB Output is correct
45 Correct 28 ms 28032 KB Output is correct
46 Correct 28 ms 27896 KB Output is correct
47 Correct 26 ms 27768 KB Output is correct
48 Correct 29 ms 27896 KB Output is correct
49 Correct 35 ms 28028 KB Output is correct
50 Correct 29 ms 28024 KB Output is correct
51 Correct 28 ms 27904 KB Output is correct
52 Correct 26 ms 27904 KB Output is correct
53 Correct 27 ms 27896 KB Output is correct
54 Correct 25 ms 27904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 27776 KB Output is correct
2 Correct 24 ms 27768 KB Output is correct
3 Correct 27 ms 27776 KB Output is correct
4 Correct 26 ms 27844 KB Output is correct
5 Correct 26 ms 27776 KB Output is correct
6 Correct 26 ms 27776 KB Output is correct
7 Correct 29 ms 27904 KB Output is correct
8 Correct 26 ms 27776 KB Output is correct
9 Correct 25 ms 27776 KB Output is correct
10 Correct 25 ms 27904 KB Output is correct
11 Correct 26 ms 27896 KB Output is correct
12 Correct 25 ms 27768 KB Output is correct
13 Correct 25 ms 27768 KB Output is correct
14 Correct 25 ms 27776 KB Output is correct
15 Correct 25 ms 27776 KB Output is correct
16 Correct 29 ms 27776 KB Output is correct
17 Correct 25 ms 27776 KB Output is correct
18 Correct 25 ms 27776 KB Output is correct
19 Correct 25 ms 27768 KB Output is correct
20 Correct 27 ms 27768 KB Output is correct
21 Correct 24 ms 27776 KB Output is correct
22 Correct 27 ms 27776 KB Output is correct
23 Correct 30 ms 27768 KB Output is correct
24 Correct 26 ms 27768 KB Output is correct
25 Correct 25 ms 27776 KB Output is correct
26 Correct 71 ms 32164 KB Output is correct
27 Correct 64 ms 32376 KB Output is correct
28 Correct 26 ms 27904 KB Output is correct
29 Correct 26 ms 27768 KB Output is correct
30 Correct 26 ms 27768 KB Output is correct
31 Correct 64 ms 30388 KB Output is correct
32 Correct 28 ms 27904 KB Output is correct
33 Correct 80 ms 32124 KB Output is correct
34 Correct 63 ms 32096 KB Output is correct
35 Correct 30 ms 27768 KB Output is correct
36 Correct 60 ms 30328 KB Output is correct
37 Correct 31 ms 27768 KB Output is correct
38 Correct 28 ms 27776 KB Output is correct
39 Correct 68 ms 31960 KB Output is correct
40 Correct 28 ms 27768 KB Output is correct
41 Correct 64 ms 30200 KB Output is correct
42 Correct 61 ms 32196 KB Output is correct
43 Correct 25 ms 27776 KB Output is correct
44 Correct 55 ms 30208 KB Output is correct
45 Correct 61 ms 32120 KB Output is correct
46 Correct 26 ms 27904 KB Output is correct
47 Correct 29 ms 27904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 30760 KB Output is correct
2 Correct 118 ms 34796 KB Output is correct
3 Correct 27 ms 27896 KB Output is correct
4 Correct 28 ms 27832 KB Output is correct
5 Correct 29 ms 27768 KB Output is correct
6 Correct 26 ms 27776 KB Output is correct
7 Correct 26 ms 27904 KB Output is correct
8 Correct 109 ms 34808 KB Output is correct
9 Correct 27 ms 27776 KB Output is correct
10 Correct 59 ms 30508 KB Output is correct
11 Correct 27 ms 27816 KB Output is correct
12 Correct 62 ms 30584 KB Output is correct
13 Correct 103 ms 34424 KB Output is correct
14 Correct 131 ms 36388 KB Output is correct
15 Correct 71 ms 32024 KB Output is correct
16 Correct 26 ms 27896 KB Output is correct
17 Correct 26 ms 27768 KB Output is correct
18 Correct 121 ms 36072 KB Output is correct
19 Correct 170 ms 41708 KB Output is correct
20 Correct 27 ms 28024 KB Output is correct
21 Correct 29 ms 27768 KB Output is correct
22 Correct 89 ms 34440 KB Output is correct
23 Correct 30 ms 27904 KB Output is correct
24 Correct 63 ms 31224 KB Output is correct
25 Correct 173 ms 39256 KB Output is correct
26 Correct 30 ms 27996 KB Output is correct
27 Correct 33 ms 28028 KB Output is correct
28 Correct 31 ms 27996 KB Output is correct
29 Correct 33 ms 27832 KB Output is correct
30 Correct 30 ms 28032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 27776 KB Output is correct
2 Correct 24 ms 27768 KB Output is correct
3 Correct 27 ms 27776 KB Output is correct
4 Correct 26 ms 27844 KB Output is correct
5 Correct 26 ms 27776 KB Output is correct
6 Correct 26 ms 27776 KB Output is correct
7 Correct 29 ms 27904 KB Output is correct
8 Correct 26 ms 27776 KB Output is correct
9 Correct 25 ms 27776 KB Output is correct
10 Correct 25 ms 27904 KB Output is correct
11 Correct 26 ms 27896 KB Output is correct
12 Correct 25 ms 27768 KB Output is correct
13 Correct 25 ms 27768 KB Output is correct
14 Correct 25 ms 27776 KB Output is correct
15 Correct 25 ms 27776 KB Output is correct
16 Correct 29 ms 27776 KB Output is correct
17 Correct 25 ms 27776 KB Output is correct
18 Correct 25 ms 27776 KB Output is correct
19 Correct 25 ms 27768 KB Output is correct
20 Correct 27 ms 27768 KB Output is correct
21 Correct 24 ms 27776 KB Output is correct
22 Correct 27 ms 27776 KB Output is correct
23 Correct 30 ms 27768 KB Output is correct
24 Correct 26 ms 27768 KB Output is correct
25 Correct 32 ms 27748 KB Output is correct
26 Correct 32 ms 27896 KB Output is correct
27 Correct 30 ms 27908 KB Output is correct
28 Correct 32 ms 28024 KB Output is correct
29 Correct 31 ms 27904 KB Output is correct
30 Correct 26 ms 27904 KB Output is correct
31 Correct 27 ms 27768 KB Output is correct
32 Correct 28 ms 28160 KB Output is correct
33 Correct 25 ms 27776 KB Output is correct
34 Correct 28 ms 27904 KB Output is correct
35 Correct 29 ms 28024 KB Output is correct
36 Correct 27 ms 27776 KB Output is correct
37 Correct 31 ms 27904 KB Output is correct
38 Correct 29 ms 27748 KB Output is correct
39 Correct 28 ms 28024 KB Output is correct
40 Correct 28 ms 27776 KB Output is correct
41 Correct 32 ms 27804 KB Output is correct
42 Correct 28 ms 27920 KB Output is correct
43 Correct 30 ms 27776 KB Output is correct
44 Correct 26 ms 27776 KB Output is correct
45 Correct 28 ms 28032 KB Output is correct
46 Correct 28 ms 27896 KB Output is correct
47 Correct 26 ms 27768 KB Output is correct
48 Correct 29 ms 27896 KB Output is correct
49 Correct 35 ms 28028 KB Output is correct
50 Correct 29 ms 28024 KB Output is correct
51 Correct 28 ms 27904 KB Output is correct
52 Correct 26 ms 27904 KB Output is correct
53 Correct 27 ms 27896 KB Output is correct
54 Correct 25 ms 27904 KB Output is correct
55 Correct 25 ms 27776 KB Output is correct
56 Correct 71 ms 32164 KB Output is correct
57 Correct 64 ms 32376 KB Output is correct
58 Correct 26 ms 27904 KB Output is correct
59 Correct 26 ms 27768 KB Output is correct
60 Correct 26 ms 27768 KB Output is correct
61 Correct 64 ms 30388 KB Output is correct
62 Correct 28 ms 27904 KB Output is correct
63 Correct 80 ms 32124 KB Output is correct
64 Correct 63 ms 32096 KB Output is correct
65 Correct 30 ms 27768 KB Output is correct
66 Correct 60 ms 30328 KB Output is correct
67 Correct 31 ms 27768 KB Output is correct
68 Correct 28 ms 27776 KB Output is correct
69 Correct 68 ms 31960 KB Output is correct
70 Correct 28 ms 27768 KB Output is correct
71 Correct 64 ms 30200 KB Output is correct
72 Correct 61 ms 32196 KB Output is correct
73 Correct 25 ms 27776 KB Output is correct
74 Correct 55 ms 30208 KB Output is correct
75 Correct 61 ms 32120 KB Output is correct
76 Correct 26 ms 27904 KB Output is correct
77 Correct 29 ms 27904 KB Output is correct
78 Correct 61 ms 30760 KB Output is correct
79 Correct 118 ms 34796 KB Output is correct
80 Correct 27 ms 27896 KB Output is correct
81 Correct 28 ms 27832 KB Output is correct
82 Correct 29 ms 27768 KB Output is correct
83 Correct 26 ms 27776 KB Output is correct
84 Correct 26 ms 27904 KB Output is correct
85 Correct 109 ms 34808 KB Output is correct
86 Correct 27 ms 27776 KB Output is correct
87 Correct 59 ms 30508 KB Output is correct
88 Correct 27 ms 27816 KB Output is correct
89 Correct 62 ms 30584 KB Output is correct
90 Correct 103 ms 34424 KB Output is correct
91 Correct 131 ms 36388 KB Output is correct
92 Correct 71 ms 32024 KB Output is correct
93 Correct 26 ms 27896 KB Output is correct
94 Correct 26 ms 27768 KB Output is correct
95 Correct 121 ms 36072 KB Output is correct
96 Correct 170 ms 41708 KB Output is correct
97 Correct 27 ms 28024 KB Output is correct
98 Correct 29 ms 27768 KB Output is correct
99 Correct 89 ms 34440 KB Output is correct
100 Correct 30 ms 27904 KB Output is correct
101 Correct 63 ms 31224 KB Output is correct
102 Correct 173 ms 39256 KB Output is correct
103 Correct 30 ms 27996 KB Output is correct
104 Correct 33 ms 28028 KB Output is correct
105 Correct 31 ms 27996 KB Output is correct
106 Correct 33 ms 27832 KB Output is correct
107 Correct 30 ms 28032 KB Output is correct
108 Correct 514 ms 61284 KB Output is correct
109 Correct 209 ms 41208 KB Output is correct
110 Correct 221 ms 41148 KB Output is correct
111 Correct 1284 ms 86620 KB Output is correct
112 Correct 1114 ms 84176 KB Output is correct
113 Correct 800 ms 70716 KB Output is correct
114 Correct 197 ms 41336 KB Output is correct
115 Correct 203 ms 41112 KB Output is correct
116 Correct 533 ms 60080 KB Output is correct
117 Correct 1431 ms 72204 KB Output is correct
118 Correct 211 ms 41848 KB Output is correct
119 Correct 1445 ms 72116 KB Output is correct
120 Correct 1522 ms 83536 KB Output is correct
121 Correct 1416 ms 72184 KB Output is correct
122 Correct 865 ms 72700 KB Output is correct
123 Correct 708 ms 73064 KB Output is correct
124 Correct 1156 ms 67876 KB Output is correct