Submission #152209

# Submission time Handle Problem Language Result Execution time Memory
152209 2019-09-06T19:16:05 Z ryansee Mergers (JOI19_mergers) C++14
100 / 100
2487 ms 250136 KB
#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second	
#define db 0
#define EPS (1e-7)    //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (500006)
#define ll /*long long*/ int 
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define MSB(bm) ((bm==0)?-1:(63-__builtin_clzll((bm))))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
// #define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
// #define degug(...) 663
#define cerr if(0)cout
#endif
#warning "// FOR has been inclusive now!!!!"
ll n, k, A[MAXN];
vector<pi> v[MAXN];
vector<ll> g[MAXN];
int o[MAXN];
bitset<MAXN>cut;
struct ufds_ {
	int p[MAXN];
	ufds_ (){
		FOR(i,0,MAXN-1) p[i]=i;
	}
	void merge(ll x,ll y) { p[find(y)]=find(x); }
	ll find(ll x) { return (p[x]==x)?x:p[x]=find(p[x]); }
	bool same(ll a,ll b){ return find(a)==find(b); }
} ufds;
// vector<ll> rs; bool special[MAXN];
// void add_up(ll x,ll p) {
    // ++ cnt[A[x]];
	// if((cnt[A[x]] ^ o[A[x]]) && cnt[A[x]]==1) -- corr;
	// else if(cnt[A[x]]==o[A[x]]&&cnt[A[x]]-1 != 0) ++ corr;
	// for(auto i:v[x]) if(i.f^p){
	    // add_up(i.f, x);
	// }
// }
map<ll, ll> *mp[MAXN]; int corr[MAXN], sz[MAXN];
void dpdp(ll x,ll p) {
	for(auto i:v[x]) if(i.f^p) dpdp(i.f,x);
    mp[x]=new map<ll,ll>;
    corr[x]=k;
    ll ptr=x;
    for(auto i:v[x]) if(i.f^p){
		if(sz[i.f]>sz[ptr]) ptr=i.f;
	}
	mp[x]=mp[ptr]; corr[x]=corr[ptr]; sz[x]=sz[ptr];
	for(auto i:v[x])if(i.f!=p&&i.f!=ptr){ sz[x]+=sz[i.f];
		for(auto j:*mp[i.f]){
			FOR(i,1,j.s){
				++ (*mp[x])[j.f];
				if((mp[x]->at(j.f) ^ o[j.f]) && mp[x]->at(j.f)==1) -- corr[x];
				else if(mp[x]->at(j.f)==o[j.f]&&mp[x]->at(j.f)-1 != 0) ++ corr[x];
			}
		}
	}
	++ (*mp[x])[A[x]]; sz[x]++;
	if((mp[x]->at(A[x]) ^ o[A[x]]) && mp[x]->at(A[x])==1) -- corr[x];
	else if(mp[x]->at(A[x])==o[A[x]]&&mp[x]->at(A[x])-1 != 0) ++ corr[x];
}
void dfs(ll x,ll p) {
	for(auto i:v[x]) if(i.f^p) {
	    if(corr[i.f]==k) {
			cut[i.s]=1;
		} else ufds.merge(A[x], A[i.f]);
		dfs(i.f,x);
	}
	return;
}

// bitset<MAXN> vis;
// void solve(ll x, ll tc, ll num) {
// 	if(vis[x]) return;
// 	if(special[x] && num^1) return;
// 	vis[x]=1;
// 	ufds.merge(A[x], tc);
// 	for(auto i:v[x]){
// 		solve(i, tc, num+1);
// 	}
// }
vector<pi> e;
// bitset<MAXN> vis;
// void tryy(ll x){
// 	if(vis[x]) return;
// 	vis[x]=1;
// 	for(auto i:v2[x]) if(!cut[i.s]) {
// 		ufds.merge(A[x], A[i.f]); tryy(i.f);
// 	}
// }
int main()
{
	FAST
	cin>>n>>k;
	FOR(i,1,n-1){
		ll a, b;cin>>a>>b;
		v[a].eb(b, i), v[b].eb(a, i);
		e.eb(a, b);
	}
	FOR(i,1,n) cin>>A[i];
	FOR(i,1,n) ++ o[A[i]];
	dpdp(1,1);
	dfs(1,1);
// 	FOR(i,1,n) tryy(i);
// 	for(auto i:rs) { if(0)cerr<<i<<'\n'; solve(i, A[i], 1); }
	for(auto i:e){
		ll a=ufds.find(A[i.f]), b=ufds.find(A[i.s]);
		if(a==b) continue;
		g[a].eb(b), g[b].eb(a);
	}
	FOR(i,1,k) sort(all(g[i])), g[i].resize(unique(all(g[i]))-g[i].begin());
	ll leaves=0; FOR(i,1,k) if(ufds.find(i)==i&&g[i].size()==1) ++leaves;
	cout<<(ll)ceill(leaves/(ld)2);
}

Compilation message

mergers.cpp:42:2: warning: #warning "// FOR has been inclusive now!!!!" [-Wcpp]
 #warning "// FOR has been inclusive now!!!!"
  ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 26 ms 25976 KB Output is correct
3 Correct 26 ms 25848 KB Output is correct
4 Correct 26 ms 25848 KB Output is correct
5 Correct 26 ms 25848 KB Output is correct
6 Correct 28 ms 25976 KB Output is correct
7 Correct 31 ms 25848 KB Output is correct
8 Correct 26 ms 25848 KB Output is correct
9 Correct 27 ms 25848 KB Output is correct
10 Correct 26 ms 25852 KB Output is correct
11 Correct 26 ms 25848 KB Output is correct
12 Correct 26 ms 25848 KB Output is correct
13 Correct 25 ms 25848 KB Output is correct
14 Correct 26 ms 25852 KB Output is correct
15 Correct 30 ms 25848 KB Output is correct
16 Correct 27 ms 25780 KB Output is correct
17 Correct 28 ms 25976 KB Output is correct
18 Correct 25 ms 25848 KB Output is correct
19 Correct 26 ms 25848 KB Output is correct
20 Correct 25 ms 25848 KB Output is correct
21 Correct 26 ms 25848 KB Output is correct
22 Correct 28 ms 25888 KB Output is correct
23 Correct 26 ms 25820 KB Output is correct
24 Correct 25 ms 25812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 26 ms 25976 KB Output is correct
3 Correct 26 ms 25848 KB Output is correct
4 Correct 26 ms 25848 KB Output is correct
5 Correct 26 ms 25848 KB Output is correct
6 Correct 28 ms 25976 KB Output is correct
7 Correct 31 ms 25848 KB Output is correct
8 Correct 26 ms 25848 KB Output is correct
9 Correct 27 ms 25848 KB Output is correct
10 Correct 26 ms 25852 KB Output is correct
11 Correct 26 ms 25848 KB Output is correct
12 Correct 26 ms 25848 KB Output is correct
13 Correct 25 ms 25848 KB Output is correct
14 Correct 26 ms 25852 KB Output is correct
15 Correct 30 ms 25848 KB Output is correct
16 Correct 27 ms 25780 KB Output is correct
17 Correct 28 ms 25976 KB Output is correct
18 Correct 25 ms 25848 KB Output is correct
19 Correct 26 ms 25848 KB Output is correct
20 Correct 25 ms 25848 KB Output is correct
21 Correct 26 ms 25848 KB Output is correct
22 Correct 28 ms 25888 KB Output is correct
23 Correct 26 ms 25820 KB Output is correct
24 Correct 25 ms 25812 KB Output is correct
25 Correct 26 ms 25976 KB Output is correct
26 Correct 31 ms 26616 KB Output is correct
27 Correct 28 ms 26360 KB Output is correct
28 Correct 30 ms 26744 KB Output is correct
29 Correct 32 ms 26900 KB Output is correct
30 Correct 30 ms 26620 KB Output is correct
31 Correct 25 ms 25848 KB Output is correct
32 Correct 30 ms 26744 KB Output is correct
33 Correct 26 ms 25848 KB Output is correct
34 Correct 28 ms 26364 KB Output is correct
35 Correct 32 ms 26872 KB Output is correct
36 Correct 28 ms 26344 KB Output is correct
37 Correct 30 ms 26616 KB Output is correct
38 Correct 26 ms 25828 KB Output is correct
39 Correct 31 ms 26732 KB Output is correct
40 Correct 28 ms 26360 KB Output is correct
41 Correct 28 ms 26360 KB Output is correct
42 Correct 29 ms 26488 KB Output is correct
43 Correct 27 ms 26616 KB Output is correct
44 Correct 25 ms 25848 KB Output is correct
45 Correct 30 ms 26616 KB Output is correct
46 Correct 31 ms 26548 KB Output is correct
47 Correct 26 ms 25820 KB Output is correct
48 Correct 29 ms 26360 KB Output is correct
49 Correct 30 ms 26632 KB Output is correct
50 Correct 30 ms 26744 KB Output is correct
51 Correct 29 ms 26360 KB Output is correct
52 Correct 28 ms 26360 KB Output is correct
53 Correct 29 ms 26488 KB Output is correct
54 Correct 29 ms 26496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 26 ms 25976 KB Output is correct
3 Correct 26 ms 25848 KB Output is correct
4 Correct 26 ms 25848 KB Output is correct
5 Correct 26 ms 25848 KB Output is correct
6 Correct 28 ms 25976 KB Output is correct
7 Correct 31 ms 25848 KB Output is correct
8 Correct 26 ms 25848 KB Output is correct
9 Correct 27 ms 25848 KB Output is correct
10 Correct 26 ms 25852 KB Output is correct
11 Correct 26 ms 25848 KB Output is correct
12 Correct 26 ms 25848 KB Output is correct
13 Correct 25 ms 25848 KB Output is correct
14 Correct 26 ms 25852 KB Output is correct
15 Correct 30 ms 25848 KB Output is correct
16 Correct 27 ms 25780 KB Output is correct
17 Correct 28 ms 25976 KB Output is correct
18 Correct 25 ms 25848 KB Output is correct
19 Correct 26 ms 25848 KB Output is correct
20 Correct 25 ms 25848 KB Output is correct
21 Correct 26 ms 25848 KB Output is correct
22 Correct 28 ms 25888 KB Output is correct
23 Correct 26 ms 25820 KB Output is correct
24 Correct 25 ms 25812 KB Output is correct
25 Correct 26 ms 25848 KB Output is correct
26 Correct 129 ms 44832 KB Output is correct
27 Correct 196 ms 47980 KB Output is correct
28 Correct 28 ms 26400 KB Output is correct
29 Correct 25 ms 25848 KB Output is correct
30 Correct 26 ms 25820 KB Output is correct
31 Correct 150 ms 42348 KB Output is correct
32 Correct 28 ms 26360 KB Output is correct
33 Correct 174 ms 52596 KB Output is correct
34 Correct 190 ms 46824 KB Output is correct
35 Correct 28 ms 26268 KB Output is correct
36 Correct 144 ms 40936 KB Output is correct
37 Correct 28 ms 26360 KB Output is correct
38 Correct 30 ms 26488 KB Output is correct
39 Correct 165 ms 44868 KB Output is correct
40 Correct 29 ms 26616 KB Output is correct
41 Correct 162 ms 42252 KB Output is correct
42 Correct 168 ms 43756 KB Output is correct
43 Correct 26 ms 25848 KB Output is correct
44 Correct 164 ms 52972 KB Output is correct
45 Correct 170 ms 49768 KB Output is correct
46 Correct 27 ms 26364 KB Output is correct
47 Correct 29 ms 26488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 44884 KB Output is correct
2 Correct 228 ms 53932 KB Output is correct
3 Correct 30 ms 26628 KB Output is correct
4 Correct 27 ms 26232 KB Output is correct
5 Correct 25 ms 25848 KB Output is correct
6 Correct 26 ms 25848 KB Output is correct
7 Correct 27 ms 26488 KB Output is correct
8 Correct 286 ms 54760 KB Output is correct
9 Correct 28 ms 26360 KB Output is correct
10 Correct 161 ms 42552 KB Output is correct
11 Correct 26 ms 25976 KB Output is correct
12 Correct 161 ms 39888 KB Output is correct
13 Correct 296 ms 54308 KB Output is correct
14 Correct 358 ms 68072 KB Output is correct
15 Correct 133 ms 44900 KB Output is correct
16 Correct 29 ms 26488 KB Output is correct
17 Correct 25 ms 25848 KB Output is correct
18 Correct 246 ms 52696 KB Output is correct
19 Correct 340 ms 57552 KB Output is correct
20 Correct 30 ms 26616 KB Output is correct
21 Correct 26 ms 25848 KB Output is correct
22 Correct 197 ms 48224 KB Output is correct
23 Correct 35 ms 26488 KB Output is correct
24 Correct 194 ms 42992 KB Output is correct
25 Correct 295 ms 57448 KB Output is correct
26 Correct 30 ms 26744 KB Output is correct
27 Correct 31 ms 26872 KB Output is correct
28 Correct 29 ms 26360 KB Output is correct
29 Correct 27 ms 26360 KB Output is correct
30 Correct 29 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 25848 KB Output is correct
2 Correct 26 ms 25976 KB Output is correct
3 Correct 26 ms 25848 KB Output is correct
4 Correct 26 ms 25848 KB Output is correct
5 Correct 26 ms 25848 KB Output is correct
6 Correct 28 ms 25976 KB Output is correct
7 Correct 31 ms 25848 KB Output is correct
8 Correct 26 ms 25848 KB Output is correct
9 Correct 27 ms 25848 KB Output is correct
10 Correct 26 ms 25852 KB Output is correct
11 Correct 26 ms 25848 KB Output is correct
12 Correct 26 ms 25848 KB Output is correct
13 Correct 25 ms 25848 KB Output is correct
14 Correct 26 ms 25852 KB Output is correct
15 Correct 30 ms 25848 KB Output is correct
16 Correct 27 ms 25780 KB Output is correct
17 Correct 28 ms 25976 KB Output is correct
18 Correct 25 ms 25848 KB Output is correct
19 Correct 26 ms 25848 KB Output is correct
20 Correct 25 ms 25848 KB Output is correct
21 Correct 26 ms 25848 KB Output is correct
22 Correct 28 ms 25888 KB Output is correct
23 Correct 26 ms 25820 KB Output is correct
24 Correct 25 ms 25812 KB Output is correct
25 Correct 26 ms 25976 KB Output is correct
26 Correct 31 ms 26616 KB Output is correct
27 Correct 28 ms 26360 KB Output is correct
28 Correct 30 ms 26744 KB Output is correct
29 Correct 32 ms 26900 KB Output is correct
30 Correct 30 ms 26620 KB Output is correct
31 Correct 25 ms 25848 KB Output is correct
32 Correct 30 ms 26744 KB Output is correct
33 Correct 26 ms 25848 KB Output is correct
34 Correct 28 ms 26364 KB Output is correct
35 Correct 32 ms 26872 KB Output is correct
36 Correct 28 ms 26344 KB Output is correct
37 Correct 30 ms 26616 KB Output is correct
38 Correct 26 ms 25828 KB Output is correct
39 Correct 31 ms 26732 KB Output is correct
40 Correct 28 ms 26360 KB Output is correct
41 Correct 28 ms 26360 KB Output is correct
42 Correct 29 ms 26488 KB Output is correct
43 Correct 27 ms 26616 KB Output is correct
44 Correct 25 ms 25848 KB Output is correct
45 Correct 30 ms 26616 KB Output is correct
46 Correct 31 ms 26548 KB Output is correct
47 Correct 26 ms 25820 KB Output is correct
48 Correct 29 ms 26360 KB Output is correct
49 Correct 30 ms 26632 KB Output is correct
50 Correct 30 ms 26744 KB Output is correct
51 Correct 29 ms 26360 KB Output is correct
52 Correct 28 ms 26360 KB Output is correct
53 Correct 29 ms 26488 KB Output is correct
54 Correct 29 ms 26496 KB Output is correct
55 Correct 26 ms 25848 KB Output is correct
56 Correct 129 ms 44832 KB Output is correct
57 Correct 196 ms 47980 KB Output is correct
58 Correct 28 ms 26400 KB Output is correct
59 Correct 25 ms 25848 KB Output is correct
60 Correct 26 ms 25820 KB Output is correct
61 Correct 150 ms 42348 KB Output is correct
62 Correct 28 ms 26360 KB Output is correct
63 Correct 174 ms 52596 KB Output is correct
64 Correct 190 ms 46824 KB Output is correct
65 Correct 28 ms 26268 KB Output is correct
66 Correct 144 ms 40936 KB Output is correct
67 Correct 28 ms 26360 KB Output is correct
68 Correct 30 ms 26488 KB Output is correct
69 Correct 165 ms 44868 KB Output is correct
70 Correct 29 ms 26616 KB Output is correct
71 Correct 162 ms 42252 KB Output is correct
72 Correct 168 ms 43756 KB Output is correct
73 Correct 26 ms 25848 KB Output is correct
74 Correct 164 ms 52972 KB Output is correct
75 Correct 170 ms 49768 KB Output is correct
76 Correct 27 ms 26364 KB Output is correct
77 Correct 29 ms 26488 KB Output is correct
78 Correct 126 ms 44884 KB Output is correct
79 Correct 228 ms 53932 KB Output is correct
80 Correct 30 ms 26628 KB Output is correct
81 Correct 27 ms 26232 KB Output is correct
82 Correct 25 ms 25848 KB Output is correct
83 Correct 26 ms 25848 KB Output is correct
84 Correct 27 ms 26488 KB Output is correct
85 Correct 286 ms 54760 KB Output is correct
86 Correct 28 ms 26360 KB Output is correct
87 Correct 161 ms 42552 KB Output is correct
88 Correct 26 ms 25976 KB Output is correct
89 Correct 161 ms 39888 KB Output is correct
90 Correct 296 ms 54308 KB Output is correct
91 Correct 358 ms 68072 KB Output is correct
92 Correct 133 ms 44900 KB Output is correct
93 Correct 29 ms 26488 KB Output is correct
94 Correct 25 ms 25848 KB Output is correct
95 Correct 246 ms 52696 KB Output is correct
96 Correct 340 ms 57552 KB Output is correct
97 Correct 30 ms 26616 KB Output is correct
98 Correct 26 ms 25848 KB Output is correct
99 Correct 197 ms 48224 KB Output is correct
100 Correct 35 ms 26488 KB Output is correct
101 Correct 194 ms 42992 KB Output is correct
102 Correct 295 ms 57448 KB Output is correct
103 Correct 30 ms 26744 KB Output is correct
104 Correct 31 ms 26872 KB Output is correct
105 Correct 29 ms 26360 KB Output is correct
106 Correct 27 ms 26360 KB Output is correct
107 Correct 29 ms 26460 KB Output is correct
108 Correct 1250 ms 135580 KB Output is correct
109 Correct 1016 ms 127504 KB Output is correct
110 Correct 1020 ms 147912 KB Output is correct
111 Correct 2339 ms 188216 KB Output is correct
112 Correct 2487 ms 178984 KB Output is correct
113 Correct 1574 ms 161088 KB Output is correct
114 Correct 899 ms 108904 KB Output is correct
115 Correct 923 ms 108816 KB Output is correct
116 Correct 1622 ms 172756 KB Output is correct
117 Correct 2366 ms 250136 KB Output is correct
118 Correct 939 ms 108480 KB Output is correct
119 Correct 2349 ms 244336 KB Output is correct
120 Correct 2084 ms 185672 KB Output is correct
121 Correct 2392 ms 245132 KB Output is correct
122 Correct 2079 ms 220764 KB Output is correct
123 Correct 1597 ms 167120 KB Output is correct
124 Correct 2242 ms 193468 KB Output is correct