답안 #554117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554117 2022-04-27T16:55:46 Z MilosMilutinovic Mergers (JOI19_mergers) C++14
100 / 100
1525 ms 133724 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=500050;
int n,k,dep[N],cc[N],deg[N];
VI g[N],v[N];

#define LOG 20
int par[N][LOG];
void dfs1(int u,int p) {
	dep[u]=dep[p]+1;
	par[u][0]=p;
	rep(i,1,LOG) par[u][i]=par[par[u][i-1]][i-1];
	for (int v:g[u]) if (v!=p) dfs1(v,u);
}

int lca(int u,int v) {
	if (dep[u]>dep[v]) swap(u,v);
	per(i,0,LOG) if (dep[par[v][i]]>=dep[u]) v=par[v][i];
	if (u==v) return u;
	per(i,0,LOG) if (par[v][i]!=par[u][i]) v=par[v][i],u=par[u][i];
	return par[v][0];
}

void dfs2(int u,int p) {
	for (int v:g[u])
		if (v!=p) {
			dfs2(v,u);
			cc[u]+=cc[v];
		}
}

void solve(int u,int p,int prv) {
	for (int v:g[u]) {
		if (v==p) continue;
		if (cc[v]==0) {
			deg[v]++; deg[prv]++;
			solve(v,u,v);
		} else {
			solve(v,u,prv);
		}
	}
}

int main() {
	scanf("%d%d",&n,&k);
	rep(i,1,n) {
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].pb(v);
		g[v].pb(u);
	}
	rep(i,1,n+1) {
		int s;
		scanf("%d",&s);
		v[s].pb(i);
	}
	dfs1(1,0);
	rep(i,1,k+1) rep(j,1,v[i].size()) {
		cc[v[i][j]]++;
		cc[v[i][j-1]]++;
		cc[lca(v[i][j],v[i][j-1])]-=2;
	}
	dfs2(1,0);
	solve(1,0,1);
	int cnt=0;
	rep(i,1,n+1) if (deg[i]==1) cnt++;
	printf("%d",(cnt+1)/2);
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:3:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    3 | #define rep(i,a,n) for (int i=a;i<n;i++)
......
   77 |  rep(i,1,k+1) rep(j,1,v[i].size()) {
      |                   ~~~~~~~~~~~~~~~ 
mergers.cpp:77:15: note: in expansion of macro 'rep'
   77 |  rep(i,1,k+1) rep(j,1,v[i].size()) {
      |               ^~~
mergers.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |  scanf("%d%d",&n,&k);
      |  ~~~~~^~~~~~~~~~~~~~
mergers.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |   scanf("%d%d",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
mergers.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |   scanf("%d",&s);
      |   ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23748 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23812 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23784 KB Output is correct
6 Correct 12 ms 23772 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23812 KB Output is correct
9 Correct 12 ms 23844 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23800 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 12 ms 23832 KB Output is correct
15 Correct 12 ms 23816 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 13 ms 23840 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23808 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23816 KB Output is correct
24 Correct 13 ms 23820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23748 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23812 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23784 KB Output is correct
6 Correct 12 ms 23772 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23812 KB Output is correct
9 Correct 12 ms 23844 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23800 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 12 ms 23832 KB Output is correct
15 Correct 12 ms 23816 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 13 ms 23840 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23808 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23816 KB Output is correct
24 Correct 13 ms 23820 KB Output is correct
25 Correct 12 ms 23804 KB Output is correct
26 Correct 15 ms 24292 KB Output is correct
27 Correct 15 ms 24216 KB Output is correct
28 Correct 14 ms 24340 KB Output is correct
29 Correct 14 ms 24320 KB Output is correct
30 Correct 14 ms 24224 KB Output is correct
31 Correct 12 ms 23808 KB Output is correct
32 Correct 14 ms 24404 KB Output is correct
33 Correct 12 ms 23812 KB Output is correct
34 Correct 14 ms 24148 KB Output is correct
35 Correct 14 ms 24296 KB Output is correct
36 Correct 14 ms 24148 KB Output is correct
37 Correct 14 ms 24276 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 14 ms 24176 KB Output is correct
40 Correct 15 ms 24136 KB Output is correct
41 Correct 15 ms 24204 KB Output is correct
42 Correct 14 ms 24276 KB Output is correct
43 Correct 14 ms 24404 KB Output is correct
44 Correct 12 ms 23736 KB Output is correct
45 Correct 14 ms 24276 KB Output is correct
46 Correct 15 ms 24208 KB Output is correct
47 Correct 13 ms 23764 KB Output is correct
48 Correct 14 ms 24196 KB Output is correct
49 Correct 14 ms 24280 KB Output is correct
50 Correct 14 ms 24276 KB Output is correct
51 Correct 14 ms 24148 KB Output is correct
52 Correct 16 ms 24148 KB Output is correct
53 Correct 14 ms 24276 KB Output is correct
54 Correct 14 ms 24212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23748 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23812 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23784 KB Output is correct
6 Correct 12 ms 23772 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23812 KB Output is correct
9 Correct 12 ms 23844 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23800 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 12 ms 23832 KB Output is correct
15 Correct 12 ms 23816 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 13 ms 23840 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23808 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23816 KB Output is correct
24 Correct 13 ms 23820 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 95 ms 37940 KB Output is correct
27 Correct 120 ms 37612 KB Output is correct
28 Correct 14 ms 24212 KB Output is correct
29 Correct 13 ms 23744 KB Output is correct
30 Correct 12 ms 23724 KB Output is correct
31 Correct 120 ms 37716 KB Output is correct
32 Correct 13 ms 24148 KB Output is correct
33 Correct 162 ms 44668 KB Output is correct
34 Correct 121 ms 37560 KB Output is correct
35 Correct 15 ms 24148 KB Output is correct
36 Correct 147 ms 38424 KB Output is correct
37 Correct 14 ms 24148 KB Output is correct
38 Correct 14 ms 24148 KB Output is correct
39 Correct 91 ms 37828 KB Output is correct
40 Correct 14 ms 24404 KB Output is correct
41 Correct 118 ms 37448 KB Output is correct
42 Correct 183 ms 40008 KB Output is correct
43 Correct 14 ms 23764 KB Output is correct
44 Correct 164 ms 44972 KB Output is correct
45 Correct 154 ms 41256 KB Output is correct
46 Correct 16 ms 24148 KB Output is correct
47 Correct 14 ms 24208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 37952 KB Output is correct
2 Correct 88 ms 40740 KB Output is correct
3 Correct 14 ms 24208 KB Output is correct
4 Correct 14 ms 24220 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23816 KB Output is correct
7 Correct 14 ms 24204 KB Output is correct
8 Correct 126 ms 39276 KB Output is correct
9 Correct 15 ms 24156 KB Output is correct
10 Correct 118 ms 38120 KB Output is correct
11 Correct 11 ms 23764 KB Output is correct
12 Correct 130 ms 38036 KB Output is correct
13 Correct 130 ms 39208 KB Output is correct
14 Correct 115 ms 40976 KB Output is correct
15 Correct 93 ms 37884 KB Output is correct
16 Correct 14 ms 24276 KB Output is correct
17 Correct 12 ms 23808 KB Output is correct
18 Correct 93 ms 40784 KB Output is correct
19 Correct 134 ms 44804 KB Output is correct
20 Correct 15 ms 24276 KB Output is correct
21 Correct 13 ms 23732 KB Output is correct
22 Correct 102 ms 39580 KB Output is correct
23 Correct 15 ms 24160 KB Output is correct
24 Correct 128 ms 38476 KB Output is correct
25 Correct 126 ms 43412 KB Output is correct
26 Correct 13 ms 24276 KB Output is correct
27 Correct 14 ms 24352 KB Output is correct
28 Correct 14 ms 24200 KB Output is correct
29 Correct 13 ms 24216 KB Output is correct
30 Correct 14 ms 24248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23748 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23812 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23784 KB Output is correct
6 Correct 12 ms 23772 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23812 KB Output is correct
9 Correct 12 ms 23844 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23800 KB Output is correct
13 Correct 12 ms 23808 KB Output is correct
14 Correct 12 ms 23832 KB Output is correct
15 Correct 12 ms 23816 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 13 ms 23840 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23808 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23816 KB Output is correct
24 Correct 13 ms 23820 KB Output is correct
25 Correct 12 ms 23804 KB Output is correct
26 Correct 15 ms 24292 KB Output is correct
27 Correct 15 ms 24216 KB Output is correct
28 Correct 14 ms 24340 KB Output is correct
29 Correct 14 ms 24320 KB Output is correct
30 Correct 14 ms 24224 KB Output is correct
31 Correct 12 ms 23808 KB Output is correct
32 Correct 14 ms 24404 KB Output is correct
33 Correct 12 ms 23812 KB Output is correct
34 Correct 14 ms 24148 KB Output is correct
35 Correct 14 ms 24296 KB Output is correct
36 Correct 14 ms 24148 KB Output is correct
37 Correct 14 ms 24276 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 14 ms 24176 KB Output is correct
40 Correct 15 ms 24136 KB Output is correct
41 Correct 15 ms 24204 KB Output is correct
42 Correct 14 ms 24276 KB Output is correct
43 Correct 14 ms 24404 KB Output is correct
44 Correct 12 ms 23736 KB Output is correct
45 Correct 14 ms 24276 KB Output is correct
46 Correct 15 ms 24208 KB Output is correct
47 Correct 13 ms 23764 KB Output is correct
48 Correct 14 ms 24196 KB Output is correct
49 Correct 14 ms 24280 KB Output is correct
50 Correct 14 ms 24276 KB Output is correct
51 Correct 14 ms 24148 KB Output is correct
52 Correct 16 ms 24148 KB Output is correct
53 Correct 14 ms 24276 KB Output is correct
54 Correct 14 ms 24212 KB Output is correct
55 Correct 12 ms 23764 KB Output is correct
56 Correct 95 ms 37940 KB Output is correct
57 Correct 120 ms 37612 KB Output is correct
58 Correct 14 ms 24212 KB Output is correct
59 Correct 13 ms 23744 KB Output is correct
60 Correct 12 ms 23724 KB Output is correct
61 Correct 120 ms 37716 KB Output is correct
62 Correct 13 ms 24148 KB Output is correct
63 Correct 162 ms 44668 KB Output is correct
64 Correct 121 ms 37560 KB Output is correct
65 Correct 15 ms 24148 KB Output is correct
66 Correct 147 ms 38424 KB Output is correct
67 Correct 14 ms 24148 KB Output is correct
68 Correct 14 ms 24148 KB Output is correct
69 Correct 91 ms 37828 KB Output is correct
70 Correct 14 ms 24404 KB Output is correct
71 Correct 118 ms 37448 KB Output is correct
72 Correct 183 ms 40008 KB Output is correct
73 Correct 14 ms 23764 KB Output is correct
74 Correct 164 ms 44972 KB Output is correct
75 Correct 154 ms 41256 KB Output is correct
76 Correct 16 ms 24148 KB Output is correct
77 Correct 14 ms 24208 KB Output is correct
78 Correct 92 ms 37952 KB Output is correct
79 Correct 88 ms 40740 KB Output is correct
80 Correct 14 ms 24208 KB Output is correct
81 Correct 14 ms 24220 KB Output is correct
82 Correct 12 ms 23764 KB Output is correct
83 Correct 13 ms 23816 KB Output is correct
84 Correct 14 ms 24204 KB Output is correct
85 Correct 126 ms 39276 KB Output is correct
86 Correct 15 ms 24156 KB Output is correct
87 Correct 118 ms 38120 KB Output is correct
88 Correct 11 ms 23764 KB Output is correct
89 Correct 130 ms 38036 KB Output is correct
90 Correct 130 ms 39208 KB Output is correct
91 Correct 115 ms 40976 KB Output is correct
92 Correct 93 ms 37884 KB Output is correct
93 Correct 14 ms 24276 KB Output is correct
94 Correct 12 ms 23808 KB Output is correct
95 Correct 93 ms 40784 KB Output is correct
96 Correct 134 ms 44804 KB Output is correct
97 Correct 15 ms 24276 KB Output is correct
98 Correct 13 ms 23732 KB Output is correct
99 Correct 102 ms 39580 KB Output is correct
100 Correct 15 ms 24160 KB Output is correct
101 Correct 128 ms 38476 KB Output is correct
102 Correct 126 ms 43412 KB Output is correct
103 Correct 13 ms 24276 KB Output is correct
104 Correct 14 ms 24352 KB Output is correct
105 Correct 14 ms 24200 KB Output is correct
106 Correct 13 ms 24216 KB Output is correct
107 Correct 14 ms 24248 KB Output is correct
108 Correct 629 ms 102712 KB Output is correct
109 Correct 1525 ms 110840 KB Output is correct
110 Correct 1474 ms 121900 KB Output is correct
111 Correct 1024 ms 133724 KB Output is correct
112 Correct 927 ms 123852 KB Output is correct
113 Correct 532 ms 110556 KB Output is correct
114 Correct 844 ms 92780 KB Output is correct
115 Correct 951 ms 92700 KB Output is correct
116 Correct 1163 ms 99088 KB Output is correct
117 Correct 881 ms 110528 KB Output is correct
118 Correct 925 ms 94880 KB Output is correct
119 Correct 902 ms 110600 KB Output is correct
120 Correct 1080 ms 124544 KB Output is correct
121 Correct 870 ms 110600 KB Output is correct
122 Correct 1043 ms 104284 KB Output is correct
123 Correct 588 ms 110148 KB Output is correct
124 Correct 1079 ms 108744 KB Output is correct