답안 #140841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
140841 2019-08-05T15:09:51 Z Utaha Mergers (JOI19_mergers) C++14
100 / 100
1342 ms 136944 KB
/*input
7 7
7 3
6 3
1 3
4 3
5 3
3 2
7
4
5
6
2
3
1

*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define EL cout<<'\n'
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
	REP(i,SZ(V)) out<<V[i]<<((i!=SZ(V)-1)?" ":"");
	return out;
}
#define version 20190726
//}}}
const ll maxn=500005;
const ll maxlg=20;
const ll INF64=1e18;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}

int n,k;
vector<int> edge[maxn];
int c[maxn];
int tt[maxn];

int cnt=-1;
map<int,int> mp[maxn];
vector<int> num;
int dfs(int u,int par){
	int mx=-1;
	int ret=0;
	for(int v:edge[u]) if(v!=par){
		ret+=dfs(v,u);
		if(mx==-1||SZ(mp[mx])<SZ(mp[v])) mx=v;
	}
	for(int v:edge[u]) if(v!=par&&v!=mx){
		for(pii j:mp[v]){
			mp[mx][j.F]+=j.S;
			if(mp[mx][j.F]==tt[j.F]){
				mp[mx].erase(j.F);
			}
		}
	}
	if(mx!=-1) mp[mx].swap(mp[u]);
	mp[u][c[u]]++;
	if(mp[u][c[u]]==tt[c[u]]) mp[u].erase(c[u]);

	if(u&&SZ(mp[u])==0){
		cnt++;
		num.pb(ret);
		// cout<<u<<' '<<ret<<'\n';
		ret++;
	}
	return ret;
}

int main(){
	IOS;
	cin>>n>>k;
	REP(i,n-1){
		int u,v;
		cin>>u>>v;
		u--;v--;
		edge[u].pb(v);
		edge[v].pb(u);
	}
	REP(i,n) cin>>c[i],c[i]--,tt[c[i]]++;
	int tmp=dfs(0,-1);
	int ans=0;
	for(int i:num) ans+=(i==tmp-1)+(i==0);
	cout<<(ans+1)/2<<'\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35704 KB Output is correct
2 Correct 34 ms 35576 KB Output is correct
3 Correct 34 ms 35576 KB Output is correct
4 Correct 34 ms 35576 KB Output is correct
5 Correct 33 ms 35576 KB Output is correct
6 Correct 33 ms 35576 KB Output is correct
7 Correct 34 ms 35576 KB Output is correct
8 Correct 35 ms 35676 KB Output is correct
9 Correct 34 ms 35576 KB Output is correct
10 Correct 34 ms 35548 KB Output is correct
11 Correct 34 ms 35548 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 40 ms 35576 KB Output is correct
14 Correct 92 ms 35704 KB Output is correct
15 Correct 34 ms 35576 KB Output is correct
16 Correct 33 ms 35576 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35576 KB Output is correct
19 Correct 35 ms 35576 KB Output is correct
20 Correct 34 ms 35508 KB Output is correct
21 Correct 34 ms 35576 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 33 ms 35576 KB Output is correct
24 Correct 34 ms 35576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35704 KB Output is correct
2 Correct 34 ms 35576 KB Output is correct
3 Correct 34 ms 35576 KB Output is correct
4 Correct 34 ms 35576 KB Output is correct
5 Correct 33 ms 35576 KB Output is correct
6 Correct 33 ms 35576 KB Output is correct
7 Correct 34 ms 35576 KB Output is correct
8 Correct 35 ms 35676 KB Output is correct
9 Correct 34 ms 35576 KB Output is correct
10 Correct 34 ms 35548 KB Output is correct
11 Correct 34 ms 35548 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 40 ms 35576 KB Output is correct
14 Correct 92 ms 35704 KB Output is correct
15 Correct 34 ms 35576 KB Output is correct
16 Correct 33 ms 35576 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35576 KB Output is correct
19 Correct 35 ms 35576 KB Output is correct
20 Correct 34 ms 35508 KB Output is correct
21 Correct 34 ms 35576 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 33 ms 35576 KB Output is correct
24 Correct 34 ms 35576 KB Output is correct
25 Correct 34 ms 35632 KB Output is correct
26 Correct 35 ms 35836 KB Output is correct
27 Correct 36 ms 35704 KB Output is correct
28 Correct 36 ms 35960 KB Output is correct
29 Correct 42 ms 35852 KB Output is correct
30 Correct 38 ms 35960 KB Output is correct
31 Correct 43 ms 35576 KB Output is correct
32 Correct 37 ms 36088 KB Output is correct
33 Correct 34 ms 35576 KB Output is correct
34 Correct 36 ms 35832 KB Output is correct
35 Correct 36 ms 35832 KB Output is correct
36 Correct 35 ms 35832 KB Output is correct
37 Correct 37 ms 35960 KB Output is correct
38 Correct 34 ms 35576 KB Output is correct
39 Correct 37 ms 36088 KB Output is correct
40 Correct 36 ms 35832 KB Output is correct
41 Correct 36 ms 35832 KB Output is correct
42 Correct 37 ms 35960 KB Output is correct
43 Correct 36 ms 36216 KB Output is correct
44 Correct 34 ms 35580 KB Output is correct
45 Correct 37 ms 35960 KB Output is correct
46 Correct 37 ms 35836 KB Output is correct
47 Correct 34 ms 35576 KB Output is correct
48 Correct 36 ms 35832 KB Output is correct
49 Correct 36 ms 35832 KB Output is correct
50 Correct 37 ms 35944 KB Output is correct
51 Correct 36 ms 35704 KB Output is correct
52 Correct 44 ms 35832 KB Output is correct
53 Correct 37 ms 35960 KB Output is correct
54 Correct 37 ms 35960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35704 KB Output is correct
2 Correct 34 ms 35576 KB Output is correct
3 Correct 34 ms 35576 KB Output is correct
4 Correct 34 ms 35576 KB Output is correct
5 Correct 33 ms 35576 KB Output is correct
6 Correct 33 ms 35576 KB Output is correct
7 Correct 34 ms 35576 KB Output is correct
8 Correct 35 ms 35676 KB Output is correct
9 Correct 34 ms 35576 KB Output is correct
10 Correct 34 ms 35548 KB Output is correct
11 Correct 34 ms 35548 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 40 ms 35576 KB Output is correct
14 Correct 92 ms 35704 KB Output is correct
15 Correct 34 ms 35576 KB Output is correct
16 Correct 33 ms 35576 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35576 KB Output is correct
19 Correct 35 ms 35576 KB Output is correct
20 Correct 34 ms 35508 KB Output is correct
21 Correct 34 ms 35576 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 33 ms 35576 KB Output is correct
24 Correct 34 ms 35576 KB Output is correct
25 Correct 35 ms 35576 KB Output is correct
26 Correct 117 ms 45652 KB Output is correct
27 Correct 174 ms 48604 KB Output is correct
28 Correct 40 ms 35704 KB Output is correct
29 Correct 35 ms 35576 KB Output is correct
30 Correct 34 ms 35580 KB Output is correct
31 Correct 169 ms 43100 KB Output is correct
32 Correct 36 ms 35832 KB Output is correct
33 Correct 155 ms 53624 KB Output is correct
34 Correct 169 ms 47480 KB Output is correct
35 Correct 36 ms 35832 KB Output is correct
36 Correct 135 ms 42204 KB Output is correct
37 Correct 36 ms 35832 KB Output is correct
38 Correct 36 ms 35832 KB Output is correct
39 Correct 122 ms 45552 KB Output is correct
40 Correct 36 ms 36088 KB Output is correct
41 Correct 127 ms 42972 KB Output is correct
42 Correct 130 ms 44920 KB Output is correct
43 Correct 34 ms 35576 KB Output is correct
44 Correct 133 ms 54108 KB Output is correct
45 Correct 145 ms 49784 KB Output is correct
46 Correct 36 ms 35832 KB Output is correct
47 Correct 37 ms 36088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 45604 KB Output is correct
2 Correct 110 ms 42224 KB Output is correct
3 Correct 36 ms 35832 KB Output is correct
4 Correct 40 ms 35832 KB Output is correct
5 Correct 34 ms 35576 KB Output is correct
6 Correct 34 ms 35576 KB Output is correct
7 Correct 36 ms 35832 KB Output is correct
8 Correct 164 ms 45632 KB Output is correct
9 Correct 36 ms 35832 KB Output is correct
10 Correct 131 ms 43024 KB Output is correct
11 Correct 34 ms 35576 KB Output is correct
12 Correct 124 ms 40824 KB Output is correct
13 Correct 140 ms 42744 KB Output is correct
14 Correct 134 ms 41972 KB Output is correct
15 Correct 126 ms 45588 KB Output is correct
16 Correct 36 ms 35832 KB Output is correct
17 Correct 35 ms 35576 KB Output is correct
18 Correct 124 ms 43120 KB Output is correct
19 Correct 164 ms 49216 KB Output is correct
20 Correct 37 ms 35836 KB Output is correct
21 Correct 34 ms 35576 KB Output is correct
22 Correct 158 ms 46188 KB Output is correct
23 Correct 36 ms 35832 KB Output is correct
24 Correct 130 ms 41208 KB Output is correct
25 Correct 141 ms 46708 KB Output is correct
26 Correct 42 ms 35704 KB Output is correct
27 Correct 44 ms 35832 KB Output is correct
28 Correct 36 ms 35704 KB Output is correct
29 Correct 36 ms 35832 KB Output is correct
30 Correct 36 ms 35876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35704 KB Output is correct
2 Correct 34 ms 35576 KB Output is correct
3 Correct 34 ms 35576 KB Output is correct
4 Correct 34 ms 35576 KB Output is correct
5 Correct 33 ms 35576 KB Output is correct
6 Correct 33 ms 35576 KB Output is correct
7 Correct 34 ms 35576 KB Output is correct
8 Correct 35 ms 35676 KB Output is correct
9 Correct 34 ms 35576 KB Output is correct
10 Correct 34 ms 35548 KB Output is correct
11 Correct 34 ms 35548 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 40 ms 35576 KB Output is correct
14 Correct 92 ms 35704 KB Output is correct
15 Correct 34 ms 35576 KB Output is correct
16 Correct 33 ms 35576 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35576 KB Output is correct
19 Correct 35 ms 35576 KB Output is correct
20 Correct 34 ms 35508 KB Output is correct
21 Correct 34 ms 35576 KB Output is correct
22 Correct 34 ms 35576 KB Output is correct
23 Correct 33 ms 35576 KB Output is correct
24 Correct 34 ms 35576 KB Output is correct
25 Correct 34 ms 35632 KB Output is correct
26 Correct 35 ms 35836 KB Output is correct
27 Correct 36 ms 35704 KB Output is correct
28 Correct 36 ms 35960 KB Output is correct
29 Correct 42 ms 35852 KB Output is correct
30 Correct 38 ms 35960 KB Output is correct
31 Correct 43 ms 35576 KB Output is correct
32 Correct 37 ms 36088 KB Output is correct
33 Correct 34 ms 35576 KB Output is correct
34 Correct 36 ms 35832 KB Output is correct
35 Correct 36 ms 35832 KB Output is correct
36 Correct 35 ms 35832 KB Output is correct
37 Correct 37 ms 35960 KB Output is correct
38 Correct 34 ms 35576 KB Output is correct
39 Correct 37 ms 36088 KB Output is correct
40 Correct 36 ms 35832 KB Output is correct
41 Correct 36 ms 35832 KB Output is correct
42 Correct 37 ms 35960 KB Output is correct
43 Correct 36 ms 36216 KB Output is correct
44 Correct 34 ms 35580 KB Output is correct
45 Correct 37 ms 35960 KB Output is correct
46 Correct 37 ms 35836 KB Output is correct
47 Correct 34 ms 35576 KB Output is correct
48 Correct 36 ms 35832 KB Output is correct
49 Correct 36 ms 35832 KB Output is correct
50 Correct 37 ms 35944 KB Output is correct
51 Correct 36 ms 35704 KB Output is correct
52 Correct 44 ms 35832 KB Output is correct
53 Correct 37 ms 35960 KB Output is correct
54 Correct 37 ms 35960 KB Output is correct
55 Correct 35 ms 35576 KB Output is correct
56 Correct 117 ms 45652 KB Output is correct
57 Correct 174 ms 48604 KB Output is correct
58 Correct 40 ms 35704 KB Output is correct
59 Correct 35 ms 35576 KB Output is correct
60 Correct 34 ms 35580 KB Output is correct
61 Correct 169 ms 43100 KB Output is correct
62 Correct 36 ms 35832 KB Output is correct
63 Correct 155 ms 53624 KB Output is correct
64 Correct 169 ms 47480 KB Output is correct
65 Correct 36 ms 35832 KB Output is correct
66 Correct 135 ms 42204 KB Output is correct
67 Correct 36 ms 35832 KB Output is correct
68 Correct 36 ms 35832 KB Output is correct
69 Correct 122 ms 45552 KB Output is correct
70 Correct 36 ms 36088 KB Output is correct
71 Correct 127 ms 42972 KB Output is correct
72 Correct 130 ms 44920 KB Output is correct
73 Correct 34 ms 35576 KB Output is correct
74 Correct 133 ms 54108 KB Output is correct
75 Correct 145 ms 49784 KB Output is correct
76 Correct 36 ms 35832 KB Output is correct
77 Correct 37 ms 36088 KB Output is correct
78 Correct 124 ms 45604 KB Output is correct
79 Correct 110 ms 42224 KB Output is correct
80 Correct 36 ms 35832 KB Output is correct
81 Correct 40 ms 35832 KB Output is correct
82 Correct 34 ms 35576 KB Output is correct
83 Correct 34 ms 35576 KB Output is correct
84 Correct 36 ms 35832 KB Output is correct
85 Correct 164 ms 45632 KB Output is correct
86 Correct 36 ms 35832 KB Output is correct
87 Correct 131 ms 43024 KB Output is correct
88 Correct 34 ms 35576 KB Output is correct
89 Correct 124 ms 40824 KB Output is correct
90 Correct 140 ms 42744 KB Output is correct
91 Correct 134 ms 41972 KB Output is correct
92 Correct 126 ms 45588 KB Output is correct
93 Correct 36 ms 35832 KB Output is correct
94 Correct 35 ms 35576 KB Output is correct
95 Correct 124 ms 43120 KB Output is correct
96 Correct 164 ms 49216 KB Output is correct
97 Correct 37 ms 35836 KB Output is correct
98 Correct 34 ms 35576 KB Output is correct
99 Correct 158 ms 46188 KB Output is correct
100 Correct 36 ms 35832 KB Output is correct
101 Correct 130 ms 41208 KB Output is correct
102 Correct 141 ms 46708 KB Output is correct
103 Correct 42 ms 35704 KB Output is correct
104 Correct 44 ms 35832 KB Output is correct
105 Correct 36 ms 35704 KB Output is correct
106 Correct 36 ms 35832 KB Output is correct
107 Correct 36 ms 35876 KB Output is correct
108 Correct 921 ms 92352 KB Output is correct
109 Correct 715 ms 94200 KB Output is correct
110 Correct 712 ms 114552 KB Output is correct
111 Correct 880 ms 109824 KB Output is correct
112 Correct 830 ms 92140 KB Output is correct
113 Correct 614 ms 74424 KB Output is correct
114 Correct 686 ms 73168 KB Output is correct
115 Correct 679 ms 73080 KB Output is correct
116 Correct 1119 ms 123188 KB Output is correct
117 Correct 743 ms 68164 KB Output is correct
118 Correct 680 ms 73320 KB Output is correct
119 Correct 730 ms 67872 KB Output is correct
120 Correct 806 ms 93568 KB Output is correct
121 Correct 747 ms 67800 KB Output is correct
122 Correct 1342 ms 136944 KB Output is correct
123 Correct 556 ms 70396 KB Output is correct
124 Correct 796 ms 67608 KB Output is correct