# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
944594 |
2024-03-13T01:32:39 Z |
yeediot |
Mergers (JOI19_mergers) |
C++14 |
|
931 ms |
194204 KB |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=5e5+5;
vector<int>adj[mxn];
int c[mxn];
int cnt[mxn];
map<int,int>mp[mxn];
bool cut[mxn];
int sum[mxn];
int sm;
void dfs(int v,int pa=-1){
mp[v][c[v]]++;
if(mp[v][c[v]]==cnt[c[v]])mp[v].erase(mp[v].find(c[v]));
for(auto u:adj[v]){
if(u==pa)continue;
dfs(u,v);
if(sz(mp[v])<sz(mp[u]))swap(mp[u],mp[v]);
for(auto it:mp[u]){
mp[v][it.F]+=it.S;
if(mp[v][it.F]==cnt[it.F]){
mp[v].erase(mp[v].find(it.F));
}
}
}
if(!sz(mp[v]) and pa!=-1){
cut[v]=cut[pa]=1;
}
}
int ans=0;
void calc(int v,int pa){
int cc=0;
if(cut[v])sum[v]++;
for(auto u:adj[v]){
if(u==pa)continue;
calc(u,v);
sum[v]+=sum[u];
if(sum[u])cc++;
}
if(sm-sum[v])cc++;
if(cc==1 and cut[v])ans++;
}
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i=1;i<=n;i++){
cin>>c[i];
cnt[c[i]]++;
}
dfs(1,-1);
for(int i=1;i<=n;i++){
sm+=cut[i];
}
calc(1,-1);
cout<<(ans+1)/2<<'\n';
}
/*
input:
*/
Compilation message
mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41560 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
9 ms |
41396 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
39516 KB |
Output is correct |
6 |
Correct |
8 ms |
39516 KB |
Output is correct |
7 |
Correct |
8 ms |
39516 KB |
Output is correct |
8 |
Correct |
8 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
41564 KB |
Output is correct |
10 |
Correct |
9 ms |
41520 KB |
Output is correct |
11 |
Correct |
9 ms |
41564 KB |
Output is correct |
12 |
Correct |
8 ms |
41564 KB |
Output is correct |
13 |
Correct |
9 ms |
41580 KB |
Output is correct |
14 |
Correct |
9 ms |
41564 KB |
Output is correct |
15 |
Correct |
8 ms |
41564 KB |
Output is correct |
16 |
Correct |
9 ms |
41564 KB |
Output is correct |
17 |
Correct |
8 ms |
41564 KB |
Output is correct |
18 |
Correct |
8 ms |
39364 KB |
Output is correct |
19 |
Correct |
8 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
41400 KB |
Output is correct |
21 |
Correct |
9 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
39516 KB |
Output is correct |
23 |
Correct |
8 ms |
39352 KB |
Output is correct |
24 |
Correct |
8 ms |
41564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41560 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
9 ms |
41396 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
39516 KB |
Output is correct |
6 |
Correct |
8 ms |
39516 KB |
Output is correct |
7 |
Correct |
8 ms |
39516 KB |
Output is correct |
8 |
Correct |
8 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
41564 KB |
Output is correct |
10 |
Correct |
9 ms |
41520 KB |
Output is correct |
11 |
Correct |
9 ms |
41564 KB |
Output is correct |
12 |
Correct |
8 ms |
41564 KB |
Output is correct |
13 |
Correct |
9 ms |
41580 KB |
Output is correct |
14 |
Correct |
9 ms |
41564 KB |
Output is correct |
15 |
Correct |
8 ms |
41564 KB |
Output is correct |
16 |
Correct |
9 ms |
41564 KB |
Output is correct |
17 |
Correct |
8 ms |
41564 KB |
Output is correct |
18 |
Correct |
8 ms |
39364 KB |
Output is correct |
19 |
Correct |
8 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
41400 KB |
Output is correct |
21 |
Correct |
9 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
39516 KB |
Output is correct |
23 |
Correct |
8 ms |
39352 KB |
Output is correct |
24 |
Correct |
8 ms |
41564 KB |
Output is correct |
25 |
Correct |
8 ms |
41560 KB |
Output is correct |
26 |
Correct |
9 ms |
41820 KB |
Output is correct |
27 |
Correct |
10 ms |
41820 KB |
Output is correct |
28 |
Correct |
10 ms |
42012 KB |
Output is correct |
29 |
Correct |
10 ms |
41560 KB |
Output is correct |
30 |
Correct |
11 ms |
42076 KB |
Output is correct |
31 |
Correct |
8 ms |
41564 KB |
Output is correct |
32 |
Correct |
9 ms |
42076 KB |
Output is correct |
33 |
Correct |
8 ms |
41564 KB |
Output is correct |
34 |
Correct |
9 ms |
39772 KB |
Output is correct |
35 |
Correct |
9 ms |
41548 KB |
Output is correct |
36 |
Correct |
10 ms |
41820 KB |
Output is correct |
37 |
Correct |
11 ms |
42076 KB |
Output is correct |
38 |
Correct |
8 ms |
41564 KB |
Output is correct |
39 |
Correct |
11 ms |
42320 KB |
Output is correct |
40 |
Correct |
11 ms |
39772 KB |
Output is correct |
41 |
Correct |
10 ms |
41820 KB |
Output is correct |
42 |
Correct |
10 ms |
41852 KB |
Output is correct |
43 |
Correct |
10 ms |
40280 KB |
Output is correct |
44 |
Correct |
9 ms |
41572 KB |
Output is correct |
45 |
Correct |
9 ms |
41820 KB |
Output is correct |
46 |
Correct |
10 ms |
41820 KB |
Output is correct |
47 |
Correct |
8 ms |
41564 KB |
Output is correct |
48 |
Correct |
11 ms |
41820 KB |
Output is correct |
49 |
Correct |
9 ms |
41564 KB |
Output is correct |
50 |
Correct |
11 ms |
41820 KB |
Output is correct |
51 |
Correct |
10 ms |
41820 KB |
Output is correct |
52 |
Correct |
10 ms |
39800 KB |
Output is correct |
53 |
Correct |
10 ms |
41820 KB |
Output is correct |
54 |
Correct |
11 ms |
40160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41560 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
9 ms |
41396 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
39516 KB |
Output is correct |
6 |
Correct |
8 ms |
39516 KB |
Output is correct |
7 |
Correct |
8 ms |
39516 KB |
Output is correct |
8 |
Correct |
8 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
41564 KB |
Output is correct |
10 |
Correct |
9 ms |
41520 KB |
Output is correct |
11 |
Correct |
9 ms |
41564 KB |
Output is correct |
12 |
Correct |
8 ms |
41564 KB |
Output is correct |
13 |
Correct |
9 ms |
41580 KB |
Output is correct |
14 |
Correct |
9 ms |
41564 KB |
Output is correct |
15 |
Correct |
8 ms |
41564 KB |
Output is correct |
16 |
Correct |
9 ms |
41564 KB |
Output is correct |
17 |
Correct |
8 ms |
41564 KB |
Output is correct |
18 |
Correct |
8 ms |
39364 KB |
Output is correct |
19 |
Correct |
8 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
41400 KB |
Output is correct |
21 |
Correct |
9 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
39516 KB |
Output is correct |
23 |
Correct |
8 ms |
39352 KB |
Output is correct |
24 |
Correct |
8 ms |
41564 KB |
Output is correct |
25 |
Correct |
8 ms |
41560 KB |
Output is correct |
26 |
Correct |
59 ms |
50612 KB |
Output is correct |
27 |
Correct |
93 ms |
60080 KB |
Output is correct |
28 |
Correct |
10 ms |
41816 KB |
Output is correct |
29 |
Correct |
9 ms |
41564 KB |
Output is correct |
30 |
Correct |
9 ms |
41816 KB |
Output is correct |
31 |
Correct |
64 ms |
53392 KB |
Output is correct |
32 |
Correct |
9 ms |
40024 KB |
Output is correct |
33 |
Correct |
91 ms |
65108 KB |
Output is correct |
34 |
Correct |
86 ms |
60496 KB |
Output is correct |
35 |
Correct |
9 ms |
41820 KB |
Output is correct |
36 |
Correct |
69 ms |
55892 KB |
Output is correct |
37 |
Correct |
10 ms |
39772 KB |
Output is correct |
38 |
Correct |
11 ms |
41820 KB |
Output is correct |
39 |
Correct |
58 ms |
51092 KB |
Output is correct |
40 |
Correct |
10 ms |
40280 KB |
Output is correct |
41 |
Correct |
64 ms |
51664 KB |
Output is correct |
42 |
Correct |
79 ms |
59476 KB |
Output is correct |
43 |
Correct |
8 ms |
41560 KB |
Output is correct |
44 |
Correct |
83 ms |
66196 KB |
Output is correct |
45 |
Correct |
108 ms |
61264 KB |
Output is correct |
46 |
Correct |
11 ms |
39768 KB |
Output is correct |
47 |
Correct |
11 ms |
40036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
50600 KB |
Output is correct |
2 |
Correct |
46 ms |
48576 KB |
Output is correct |
3 |
Correct |
9 ms |
41816 KB |
Output is correct |
4 |
Correct |
10 ms |
41976 KB |
Output is correct |
5 |
Correct |
8 ms |
41564 KB |
Output is correct |
6 |
Correct |
8 ms |
41580 KB |
Output is correct |
7 |
Correct |
10 ms |
39800 KB |
Output is correct |
8 |
Correct |
80 ms |
56544 KB |
Output is correct |
9 |
Correct |
10 ms |
41816 KB |
Output is correct |
10 |
Correct |
65 ms |
53224 KB |
Output is correct |
11 |
Correct |
8 ms |
41564 KB |
Output is correct |
12 |
Correct |
82 ms |
54296 KB |
Output is correct |
13 |
Correct |
73 ms |
53424 KB |
Output is correct |
14 |
Correct |
59 ms |
48320 KB |
Output is correct |
15 |
Correct |
55 ms |
50500 KB |
Output is correct |
16 |
Correct |
10 ms |
41816 KB |
Output is correct |
17 |
Correct |
8 ms |
41560 KB |
Output is correct |
18 |
Correct |
61 ms |
50112 KB |
Output is correct |
19 |
Correct |
75 ms |
55888 KB |
Output is correct |
20 |
Correct |
10 ms |
41816 KB |
Output is correct |
21 |
Correct |
8 ms |
41564 KB |
Output is correct |
22 |
Correct |
80 ms |
52276 KB |
Output is correct |
23 |
Correct |
11 ms |
41820 KB |
Output is correct |
24 |
Correct |
84 ms |
53332 KB |
Output is correct |
25 |
Correct |
67 ms |
55120 KB |
Output is correct |
26 |
Correct |
9 ms |
41564 KB |
Output is correct |
27 |
Correct |
10 ms |
41820 KB |
Output is correct |
28 |
Correct |
10 ms |
41816 KB |
Output is correct |
29 |
Correct |
9 ms |
39752 KB |
Output is correct |
30 |
Correct |
10 ms |
41820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41560 KB |
Output is correct |
2 |
Correct |
8 ms |
41564 KB |
Output is correct |
3 |
Correct |
9 ms |
41396 KB |
Output is correct |
4 |
Correct |
8 ms |
41564 KB |
Output is correct |
5 |
Correct |
8 ms |
39516 KB |
Output is correct |
6 |
Correct |
8 ms |
39516 KB |
Output is correct |
7 |
Correct |
8 ms |
39516 KB |
Output is correct |
8 |
Correct |
8 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
41564 KB |
Output is correct |
10 |
Correct |
9 ms |
41520 KB |
Output is correct |
11 |
Correct |
9 ms |
41564 KB |
Output is correct |
12 |
Correct |
8 ms |
41564 KB |
Output is correct |
13 |
Correct |
9 ms |
41580 KB |
Output is correct |
14 |
Correct |
9 ms |
41564 KB |
Output is correct |
15 |
Correct |
8 ms |
41564 KB |
Output is correct |
16 |
Correct |
9 ms |
41564 KB |
Output is correct |
17 |
Correct |
8 ms |
41564 KB |
Output is correct |
18 |
Correct |
8 ms |
39364 KB |
Output is correct |
19 |
Correct |
8 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
41400 KB |
Output is correct |
21 |
Correct |
9 ms |
39516 KB |
Output is correct |
22 |
Correct |
8 ms |
39516 KB |
Output is correct |
23 |
Correct |
8 ms |
39352 KB |
Output is correct |
24 |
Correct |
8 ms |
41564 KB |
Output is correct |
25 |
Correct |
8 ms |
41560 KB |
Output is correct |
26 |
Correct |
9 ms |
41820 KB |
Output is correct |
27 |
Correct |
10 ms |
41820 KB |
Output is correct |
28 |
Correct |
10 ms |
42012 KB |
Output is correct |
29 |
Correct |
10 ms |
41560 KB |
Output is correct |
30 |
Correct |
11 ms |
42076 KB |
Output is correct |
31 |
Correct |
8 ms |
41564 KB |
Output is correct |
32 |
Correct |
9 ms |
42076 KB |
Output is correct |
33 |
Correct |
8 ms |
41564 KB |
Output is correct |
34 |
Correct |
9 ms |
39772 KB |
Output is correct |
35 |
Correct |
9 ms |
41548 KB |
Output is correct |
36 |
Correct |
10 ms |
41820 KB |
Output is correct |
37 |
Correct |
11 ms |
42076 KB |
Output is correct |
38 |
Correct |
8 ms |
41564 KB |
Output is correct |
39 |
Correct |
11 ms |
42320 KB |
Output is correct |
40 |
Correct |
11 ms |
39772 KB |
Output is correct |
41 |
Correct |
10 ms |
41820 KB |
Output is correct |
42 |
Correct |
10 ms |
41852 KB |
Output is correct |
43 |
Correct |
10 ms |
40280 KB |
Output is correct |
44 |
Correct |
9 ms |
41572 KB |
Output is correct |
45 |
Correct |
9 ms |
41820 KB |
Output is correct |
46 |
Correct |
10 ms |
41820 KB |
Output is correct |
47 |
Correct |
8 ms |
41564 KB |
Output is correct |
48 |
Correct |
11 ms |
41820 KB |
Output is correct |
49 |
Correct |
9 ms |
41564 KB |
Output is correct |
50 |
Correct |
11 ms |
41820 KB |
Output is correct |
51 |
Correct |
10 ms |
41820 KB |
Output is correct |
52 |
Correct |
10 ms |
39800 KB |
Output is correct |
53 |
Correct |
10 ms |
41820 KB |
Output is correct |
54 |
Correct |
11 ms |
40160 KB |
Output is correct |
55 |
Correct |
8 ms |
41560 KB |
Output is correct |
56 |
Correct |
59 ms |
50612 KB |
Output is correct |
57 |
Correct |
93 ms |
60080 KB |
Output is correct |
58 |
Correct |
10 ms |
41816 KB |
Output is correct |
59 |
Correct |
9 ms |
41564 KB |
Output is correct |
60 |
Correct |
9 ms |
41816 KB |
Output is correct |
61 |
Correct |
64 ms |
53392 KB |
Output is correct |
62 |
Correct |
9 ms |
40024 KB |
Output is correct |
63 |
Correct |
91 ms |
65108 KB |
Output is correct |
64 |
Correct |
86 ms |
60496 KB |
Output is correct |
65 |
Correct |
9 ms |
41820 KB |
Output is correct |
66 |
Correct |
69 ms |
55892 KB |
Output is correct |
67 |
Correct |
10 ms |
39772 KB |
Output is correct |
68 |
Correct |
11 ms |
41820 KB |
Output is correct |
69 |
Correct |
58 ms |
51092 KB |
Output is correct |
70 |
Correct |
10 ms |
40280 KB |
Output is correct |
71 |
Correct |
64 ms |
51664 KB |
Output is correct |
72 |
Correct |
79 ms |
59476 KB |
Output is correct |
73 |
Correct |
8 ms |
41560 KB |
Output is correct |
74 |
Correct |
83 ms |
66196 KB |
Output is correct |
75 |
Correct |
108 ms |
61264 KB |
Output is correct |
76 |
Correct |
11 ms |
39768 KB |
Output is correct |
77 |
Correct |
11 ms |
40036 KB |
Output is correct |
78 |
Correct |
61 ms |
50600 KB |
Output is correct |
79 |
Correct |
46 ms |
48576 KB |
Output is correct |
80 |
Correct |
9 ms |
41816 KB |
Output is correct |
81 |
Correct |
10 ms |
41976 KB |
Output is correct |
82 |
Correct |
8 ms |
41564 KB |
Output is correct |
83 |
Correct |
8 ms |
41580 KB |
Output is correct |
84 |
Correct |
10 ms |
39800 KB |
Output is correct |
85 |
Correct |
80 ms |
56544 KB |
Output is correct |
86 |
Correct |
10 ms |
41816 KB |
Output is correct |
87 |
Correct |
65 ms |
53224 KB |
Output is correct |
88 |
Correct |
8 ms |
41564 KB |
Output is correct |
89 |
Correct |
82 ms |
54296 KB |
Output is correct |
90 |
Correct |
73 ms |
53424 KB |
Output is correct |
91 |
Correct |
59 ms |
48320 KB |
Output is correct |
92 |
Correct |
55 ms |
50500 KB |
Output is correct |
93 |
Correct |
10 ms |
41816 KB |
Output is correct |
94 |
Correct |
8 ms |
41560 KB |
Output is correct |
95 |
Correct |
61 ms |
50112 KB |
Output is correct |
96 |
Correct |
75 ms |
55888 KB |
Output is correct |
97 |
Correct |
10 ms |
41816 KB |
Output is correct |
98 |
Correct |
8 ms |
41564 KB |
Output is correct |
99 |
Correct |
80 ms |
52276 KB |
Output is correct |
100 |
Correct |
11 ms |
41820 KB |
Output is correct |
101 |
Correct |
84 ms |
53332 KB |
Output is correct |
102 |
Correct |
67 ms |
55120 KB |
Output is correct |
103 |
Correct |
9 ms |
41564 KB |
Output is correct |
104 |
Correct |
10 ms |
41820 KB |
Output is correct |
105 |
Correct |
10 ms |
41816 KB |
Output is correct |
106 |
Correct |
9 ms |
39752 KB |
Output is correct |
107 |
Correct |
10 ms |
41820 KB |
Output is correct |
108 |
Correct |
704 ms |
105940 KB |
Output is correct |
109 |
Correct |
672 ms |
137260 KB |
Output is correct |
110 |
Correct |
701 ms |
159684 KB |
Output is correct |
111 |
Correct |
692 ms |
120696 KB |
Output is correct |
112 |
Correct |
598 ms |
106836 KB |
Output is correct |
113 |
Correct |
458 ms |
83672 KB |
Output is correct |
114 |
Correct |
536 ms |
103480 KB |
Output is correct |
115 |
Correct |
568 ms |
102948 KB |
Output is correct |
116 |
Correct |
852 ms |
181520 KB |
Output is correct |
117 |
Correct |
536 ms |
76556 KB |
Output is correct |
118 |
Correct |
544 ms |
103040 KB |
Output is correct |
119 |
Correct |
526 ms |
76628 KB |
Output is correct |
120 |
Correct |
567 ms |
108116 KB |
Output is correct |
121 |
Correct |
542 ms |
76596 KB |
Output is correct |
122 |
Correct |
931 ms |
194204 KB |
Output is correct |
123 |
Correct |
324 ms |
77368 KB |
Output is correct |
124 |
Correct |
548 ms |
73788 KB |
Output is correct |