# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
667474 |
2022-12-01T13:43:15 Z |
Soul234 |
Mergers (JOI19_mergers) |
C++14 |
|
562 ms |
95588 KB |
#include<bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const ll INF = 1e18;
struct DSU {
vi e;
void init(int N) {
e.assign(N, -1);
}
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool join(int a, int b) {
a = find(a), b = find(b);
if(a == b) return false;
e[b] = a;
return true;
}
} dsu;
const int MX = 5e5+5;
int N, K;
vi adj[MX];
int par[MX], dep[MX];
void dfs(int u, int p) {
each(v, adj[u]) if(v != p) {
dep[v] = dep[u] + 1;
par[v] = u;
dfs(v, u);
}
}
void mergecc(int a, int b) {
a = dsu.find(a), b = dsu.find(b);
while(a != b) {
if(dep[a] < dep[b]) swap(a,b);
dsu.join(par[a], a);
a = dsu.find(par[a]);
}
}
vi lst[MX];
pi eds[MX];
int deg[MX];
void solve() {
cin >> N >> K;
dsu.init(N+5);
F0R(i, N-1) {
int u, v;
cin >> u >> v;
eds[i] = {u, v};
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, -1);
FOR(i,1,N+1) {
int col;
cin >> col;
lst[col].pb(i);
}
FOR(i,1,K+1) {
FOR(j,1,sz(lst[i])) {
mergecc(lst[i][0], lst[i][j]);
}
}
F0R(i, N-1) {
int u = dsu.find(eds[i].fi), v = dsu.find(eds[i].se);
if(u != v) {
deg[u]++;
deg[v]++;
}
}
int ans = 0;
FOR(i,1,N+1) if(dsu.find(i) == i && deg[i] == 1) ans++;
cout << (ans+1)/2 << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23804 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23804 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23728 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
13 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23820 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23804 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23804 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23728 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
13 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23820 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23816 KB |
Output is correct |
26 |
Correct |
14 ms |
23996 KB |
Output is correct |
27 |
Correct |
14 ms |
24020 KB |
Output is correct |
28 |
Correct |
14 ms |
24116 KB |
Output is correct |
29 |
Correct |
14 ms |
24100 KB |
Output is correct |
30 |
Correct |
14 ms |
24064 KB |
Output is correct |
31 |
Correct |
13 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24148 KB |
Output is correct |
33 |
Correct |
14 ms |
23784 KB |
Output is correct |
34 |
Correct |
14 ms |
24020 KB |
Output is correct |
35 |
Correct |
14 ms |
24020 KB |
Output is correct |
36 |
Correct |
15 ms |
24020 KB |
Output is correct |
37 |
Correct |
15 ms |
24112 KB |
Output is correct |
38 |
Correct |
14 ms |
23828 KB |
Output is correct |
39 |
Correct |
14 ms |
24076 KB |
Output is correct |
40 |
Correct |
13 ms |
23956 KB |
Output is correct |
41 |
Correct |
13 ms |
24060 KB |
Output is correct |
42 |
Correct |
14 ms |
24044 KB |
Output is correct |
43 |
Correct |
14 ms |
24148 KB |
Output is correct |
44 |
Correct |
13 ms |
23816 KB |
Output is correct |
45 |
Correct |
14 ms |
24076 KB |
Output is correct |
46 |
Correct |
15 ms |
24020 KB |
Output is correct |
47 |
Correct |
14 ms |
23820 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
14 ms |
24088 KB |
Output is correct |
50 |
Correct |
14 ms |
24080 KB |
Output is correct |
51 |
Correct |
14 ms |
23960 KB |
Output is correct |
52 |
Correct |
13 ms |
24020 KB |
Output is correct |
53 |
Correct |
14 ms |
24024 KB |
Output is correct |
54 |
Correct |
14 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23804 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23804 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23728 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
13 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23820 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
53 ms |
31276 KB |
Output is correct |
27 |
Correct |
58 ms |
31052 KB |
Output is correct |
28 |
Correct |
14 ms |
24020 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
13 ms |
23792 KB |
Output is correct |
31 |
Correct |
61 ms |
31068 KB |
Output is correct |
32 |
Correct |
14 ms |
23996 KB |
Output is correct |
33 |
Correct |
66 ms |
36684 KB |
Output is correct |
34 |
Correct |
61 ms |
31052 KB |
Output is correct |
35 |
Correct |
15 ms |
23952 KB |
Output is correct |
36 |
Correct |
71 ms |
31744 KB |
Output is correct |
37 |
Correct |
14 ms |
23956 KB |
Output is correct |
38 |
Correct |
15 ms |
23972 KB |
Output is correct |
39 |
Correct |
53 ms |
31172 KB |
Output is correct |
40 |
Correct |
14 ms |
24188 KB |
Output is correct |
41 |
Correct |
61 ms |
30956 KB |
Output is correct |
42 |
Correct |
63 ms |
32784 KB |
Output is correct |
43 |
Correct |
13 ms |
23764 KB |
Output is correct |
44 |
Correct |
75 ms |
37004 KB |
Output is correct |
45 |
Correct |
67 ms |
33908 KB |
Output is correct |
46 |
Correct |
15 ms |
24020 KB |
Output is correct |
47 |
Correct |
16 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
29768 KB |
Output is correct |
2 |
Correct |
69 ms |
32772 KB |
Output is correct |
3 |
Correct |
14 ms |
24020 KB |
Output is correct |
4 |
Correct |
15 ms |
24060 KB |
Output is correct |
5 |
Correct |
13 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23812 KB |
Output is correct |
7 |
Correct |
15 ms |
23960 KB |
Output is correct |
8 |
Correct |
80 ms |
32656 KB |
Output is correct |
9 |
Correct |
14 ms |
24020 KB |
Output is correct |
10 |
Correct |
72 ms |
31440 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
64 ms |
31412 KB |
Output is correct |
13 |
Correct |
92 ms |
32568 KB |
Output is correct |
14 |
Correct |
78 ms |
34256 KB |
Output is correct |
15 |
Correct |
51 ms |
31216 KB |
Output is correct |
16 |
Correct |
14 ms |
24104 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
68 ms |
34208 KB |
Output is correct |
19 |
Correct |
91 ms |
37300 KB |
Output is correct |
20 |
Correct |
15 ms |
24060 KB |
Output is correct |
21 |
Correct |
14 ms |
23764 KB |
Output is correct |
22 |
Correct |
58 ms |
32816 KB |
Output is correct |
23 |
Correct |
13 ms |
24020 KB |
Output is correct |
24 |
Correct |
79 ms |
31852 KB |
Output is correct |
25 |
Correct |
110 ms |
36268 KB |
Output is correct |
26 |
Correct |
15 ms |
24084 KB |
Output is correct |
27 |
Correct |
15 ms |
24064 KB |
Output is correct |
28 |
Correct |
14 ms |
24020 KB |
Output is correct |
29 |
Correct |
15 ms |
24020 KB |
Output is correct |
30 |
Correct |
15 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23832 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23804 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23804 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23728 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
13 ms |
23820 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23820 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23820 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23816 KB |
Output is correct |
26 |
Correct |
14 ms |
23996 KB |
Output is correct |
27 |
Correct |
14 ms |
24020 KB |
Output is correct |
28 |
Correct |
14 ms |
24116 KB |
Output is correct |
29 |
Correct |
14 ms |
24100 KB |
Output is correct |
30 |
Correct |
14 ms |
24064 KB |
Output is correct |
31 |
Correct |
13 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24148 KB |
Output is correct |
33 |
Correct |
14 ms |
23784 KB |
Output is correct |
34 |
Correct |
14 ms |
24020 KB |
Output is correct |
35 |
Correct |
14 ms |
24020 KB |
Output is correct |
36 |
Correct |
15 ms |
24020 KB |
Output is correct |
37 |
Correct |
15 ms |
24112 KB |
Output is correct |
38 |
Correct |
14 ms |
23828 KB |
Output is correct |
39 |
Correct |
14 ms |
24076 KB |
Output is correct |
40 |
Correct |
13 ms |
23956 KB |
Output is correct |
41 |
Correct |
13 ms |
24060 KB |
Output is correct |
42 |
Correct |
14 ms |
24044 KB |
Output is correct |
43 |
Correct |
14 ms |
24148 KB |
Output is correct |
44 |
Correct |
13 ms |
23816 KB |
Output is correct |
45 |
Correct |
14 ms |
24076 KB |
Output is correct |
46 |
Correct |
15 ms |
24020 KB |
Output is correct |
47 |
Correct |
14 ms |
23820 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
14 ms |
24088 KB |
Output is correct |
50 |
Correct |
14 ms |
24080 KB |
Output is correct |
51 |
Correct |
14 ms |
23960 KB |
Output is correct |
52 |
Correct |
13 ms |
24020 KB |
Output is correct |
53 |
Correct |
14 ms |
24024 KB |
Output is correct |
54 |
Correct |
14 ms |
24052 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
53 ms |
31276 KB |
Output is correct |
57 |
Correct |
58 ms |
31052 KB |
Output is correct |
58 |
Correct |
14 ms |
24020 KB |
Output is correct |
59 |
Correct |
13 ms |
23764 KB |
Output is correct |
60 |
Correct |
13 ms |
23792 KB |
Output is correct |
61 |
Correct |
61 ms |
31068 KB |
Output is correct |
62 |
Correct |
14 ms |
23996 KB |
Output is correct |
63 |
Correct |
66 ms |
36684 KB |
Output is correct |
64 |
Correct |
61 ms |
31052 KB |
Output is correct |
65 |
Correct |
15 ms |
23952 KB |
Output is correct |
66 |
Correct |
71 ms |
31744 KB |
Output is correct |
67 |
Correct |
14 ms |
23956 KB |
Output is correct |
68 |
Correct |
15 ms |
23972 KB |
Output is correct |
69 |
Correct |
53 ms |
31172 KB |
Output is correct |
70 |
Correct |
14 ms |
24188 KB |
Output is correct |
71 |
Correct |
61 ms |
30956 KB |
Output is correct |
72 |
Correct |
63 ms |
32784 KB |
Output is correct |
73 |
Correct |
13 ms |
23764 KB |
Output is correct |
74 |
Correct |
75 ms |
37004 KB |
Output is correct |
75 |
Correct |
67 ms |
33908 KB |
Output is correct |
76 |
Correct |
15 ms |
24020 KB |
Output is correct |
77 |
Correct |
16 ms |
24012 KB |
Output is correct |
78 |
Correct |
49 ms |
29768 KB |
Output is correct |
79 |
Correct |
69 ms |
32772 KB |
Output is correct |
80 |
Correct |
14 ms |
24020 KB |
Output is correct |
81 |
Correct |
15 ms |
24060 KB |
Output is correct |
82 |
Correct |
13 ms |
23816 KB |
Output is correct |
83 |
Correct |
13 ms |
23812 KB |
Output is correct |
84 |
Correct |
15 ms |
23960 KB |
Output is correct |
85 |
Correct |
80 ms |
32656 KB |
Output is correct |
86 |
Correct |
14 ms |
24020 KB |
Output is correct |
87 |
Correct |
72 ms |
31440 KB |
Output is correct |
88 |
Correct |
13 ms |
23764 KB |
Output is correct |
89 |
Correct |
64 ms |
31412 KB |
Output is correct |
90 |
Correct |
92 ms |
32568 KB |
Output is correct |
91 |
Correct |
78 ms |
34256 KB |
Output is correct |
92 |
Correct |
51 ms |
31216 KB |
Output is correct |
93 |
Correct |
14 ms |
24104 KB |
Output is correct |
94 |
Correct |
13 ms |
23764 KB |
Output is correct |
95 |
Correct |
68 ms |
34208 KB |
Output is correct |
96 |
Correct |
91 ms |
37300 KB |
Output is correct |
97 |
Correct |
15 ms |
24060 KB |
Output is correct |
98 |
Correct |
14 ms |
23764 KB |
Output is correct |
99 |
Correct |
58 ms |
32816 KB |
Output is correct |
100 |
Correct |
13 ms |
24020 KB |
Output is correct |
101 |
Correct |
79 ms |
31852 KB |
Output is correct |
102 |
Correct |
110 ms |
36268 KB |
Output is correct |
103 |
Correct |
15 ms |
24084 KB |
Output is correct |
104 |
Correct |
15 ms |
24064 KB |
Output is correct |
105 |
Correct |
14 ms |
24020 KB |
Output is correct |
106 |
Correct |
15 ms |
24020 KB |
Output is correct |
107 |
Correct |
15 ms |
24052 KB |
Output is correct |
108 |
Correct |
353 ms |
69492 KB |
Output is correct |
109 |
Correct |
496 ms |
73752 KB |
Output is correct |
110 |
Correct |
475 ms |
82748 KB |
Output is correct |
111 |
Correct |
562 ms |
95588 KB |
Output is correct |
112 |
Correct |
545 ms |
87756 KB |
Output is correct |
113 |
Correct |
395 ms |
77308 KB |
Output is correct |
114 |
Correct |
414 ms |
59568 KB |
Output is correct |
115 |
Correct |
415 ms |
59580 KB |
Output is correct |
116 |
Correct |
477 ms |
65868 KB |
Output is correct |
117 |
Correct |
507 ms |
77336 KB |
Output is correct |
118 |
Correct |
417 ms |
62284 KB |
Output is correct |
119 |
Correct |
503 ms |
77328 KB |
Output is correct |
120 |
Correct |
541 ms |
88396 KB |
Output is correct |
121 |
Correct |
494 ms |
77260 KB |
Output is correct |
122 |
Correct |
529 ms |
71100 KB |
Output is correct |
123 |
Correct |
353 ms |
78780 KB |
Output is correct |
124 |
Correct |
552 ms |
75976 KB |
Output is correct |