답안 #104518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104518 2019-04-07T10:40:25 Z Pajaraja Mergers (JOI19_mergers) C++17
100 / 100
1468 ms 84856 KB
#include <bits/stdc++.h>
#define MAXN 1000007
using namespace std;
vector<int> g[MAXN];
int mi[MAXN],mo[MAXN],fa[MAXN],in[MAXN],out[MAXN],mif[MAXN],mof[MAXN],res,t,cnt;
bool c[MAXN];
void dfs1(int s,int f)
{
	in[s]=++t;
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs1(g[s][i],s);
	out[s]=++t;
}
void dfs2(int s,int f)
{
	mi[s]=mif[fa[s]]; mo[s]=mof[fa[s]];
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs2(g[s][i],s);
	mi[f]=min(mi[f],mi[s]);
	mo[f]=max(mo[f],mo[s]);
	if(mi[s]==in[s] && out[s]==mo[s] && s!=1) c[s]=true;
}
bool dfs3(int s,int f)
{
	bool d=false;
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(dfs3(g[s][i],s)) d=true;
	if(c[s] && !d) {res++; return true;}
	if(d) return true;
	return false;
}
void dfs4(int s,int f)
{
	if(c[s]) {cnt++; return;};
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs4(g[s][i],s);
}
int main()
{
	int n,k; scanf("%d %d",&n,&k);
	for(int i=0;i<n-1;i++) {int t1,t2; scanf("%d%d",&t1,&t2); g[t1].push_back(t2); g[t2].push_back(t1);}
	for(int i=1;i<=n;i++) scanf("%d",&fa[i]);
	dfs1(1,1);
	fill(mif,mif+MAXN,MAXN);
	for(int i=1;i<=n;i++) mif[fa[i]]=min(mif[fa[i]],in[i]);
	for(int i=1;i<=n;i++) mof[fa[i]]=max(mof[fa[i]],out[i]);
	dfs2(1,1);
	dfs3(1,1);
	dfs4(1,1);
	if(cnt==1) res++;
	printf("%d",(res+1)/2);
}

Compilation message

mergers.cpp: In function 'void dfs1(int, int)':
mergers.cpp:10:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs1(g[s][i],s);
              ~^~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:16:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs2(g[s][i],s);
              ~^~~~~~~~~~~~
mergers.cpp: In function 'bool dfs3(int, int)':
mergers.cpp:24:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(dfs3(g[s][i],s)) d=true;
              ~^~~~~~~~~~~~
mergers.cpp: In function 'void dfs4(int, int)':
mergers.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs4(g[s][i],s);
              ~^~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:36:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n,k; scanf("%d %d",&n,&k);
           ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:37:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0;i<n-1;i++) {int t1,t2; scanf("%d%d",&t1,&t2); g[t1].push_back(t2); g[t2].push_back(t1);}
                                     ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:38:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%d",&fa[i]);
                        ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 27776 KB Output is correct
2 Correct 35 ms 27768 KB Output is correct
3 Correct 29 ms 27776 KB Output is correct
4 Correct 31 ms 27776 KB Output is correct
5 Correct 37 ms 27768 KB Output is correct
6 Correct 31 ms 27768 KB Output is correct
7 Correct 35 ms 27740 KB Output is correct
8 Correct 32 ms 27748 KB Output is correct
9 Correct 31 ms 27772 KB Output is correct
10 Correct 31 ms 27776 KB Output is correct
11 Correct 30 ms 27768 KB Output is correct
12 Correct 34 ms 27776 KB Output is correct
13 Correct 31 ms 27692 KB Output is correct
14 Correct 33 ms 27768 KB Output is correct
15 Correct 31 ms 27768 KB Output is correct
16 Correct 31 ms 27768 KB Output is correct
17 Correct 33 ms 27776 KB Output is correct
18 Correct 31 ms 27776 KB Output is correct
19 Correct 32 ms 27768 KB Output is correct
20 Correct 32 ms 27776 KB Output is correct
21 Correct 32 ms 27768 KB Output is correct
22 Correct 31 ms 27768 KB Output is correct
23 Correct 31 ms 27768 KB Output is correct
24 Correct 33 ms 27768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 27776 KB Output is correct
2 Correct 35 ms 27768 KB Output is correct
3 Correct 29 ms 27776 KB Output is correct
4 Correct 31 ms 27776 KB Output is correct
5 Correct 37 ms 27768 KB Output is correct
6 Correct 31 ms 27768 KB Output is correct
7 Correct 35 ms 27740 KB Output is correct
8 Correct 32 ms 27748 KB Output is correct
9 Correct 31 ms 27772 KB Output is correct
10 Correct 31 ms 27776 KB Output is correct
11 Correct 30 ms 27768 KB Output is correct
12 Correct 34 ms 27776 KB Output is correct
13 Correct 31 ms 27692 KB Output is correct
14 Correct 33 ms 27768 KB Output is correct
15 Correct 31 ms 27768 KB Output is correct
16 Correct 31 ms 27768 KB Output is correct
17 Correct 33 ms 27776 KB Output is correct
18 Correct 31 ms 27776 KB Output is correct
19 Correct 32 ms 27768 KB Output is correct
20 Correct 32 ms 27776 KB Output is correct
21 Correct 32 ms 27768 KB Output is correct
22 Correct 31 ms 27768 KB Output is correct
23 Correct 31 ms 27768 KB Output is correct
24 Correct 33 ms 27768 KB Output is correct
25 Correct 32 ms 27808 KB Output is correct
26 Correct 33 ms 27992 KB Output is correct
27 Correct 38 ms 28100 KB Output is correct
28 Correct 33 ms 28024 KB Output is correct
29 Correct 39 ms 28032 KB Output is correct
30 Correct 33 ms 27996 KB Output is correct
31 Correct 30 ms 27768 KB Output is correct
32 Correct 36 ms 28152 KB Output is correct
33 Correct 36 ms 27768 KB Output is correct
34 Correct 33 ms 27996 KB Output is correct
35 Correct 31 ms 28032 KB Output is correct
36 Correct 31 ms 28024 KB Output is correct
37 Correct 33 ms 27996 KB Output is correct
38 Correct 33 ms 27768 KB Output is correct
39 Correct 34 ms 27968 KB Output is correct
40 Correct 33 ms 28024 KB Output is correct
41 Correct 37 ms 28024 KB Output is correct
42 Correct 34 ms 28024 KB Output is correct
43 Correct 32 ms 28152 KB Output is correct
44 Correct 32 ms 27876 KB Output is correct
45 Correct 33 ms 28060 KB Output is correct
46 Correct 31 ms 27964 KB Output is correct
47 Correct 30 ms 27824 KB Output is correct
48 Correct 39 ms 28024 KB Output is correct
49 Correct 35 ms 28024 KB Output is correct
50 Correct 34 ms 27996 KB Output is correct
51 Correct 35 ms 28024 KB Output is correct
52 Correct 31 ms 28024 KB Output is correct
53 Correct 36 ms 28152 KB Output is correct
54 Correct 32 ms 27936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 27776 KB Output is correct
2 Correct 35 ms 27768 KB Output is correct
3 Correct 29 ms 27776 KB Output is correct
4 Correct 31 ms 27776 KB Output is correct
5 Correct 37 ms 27768 KB Output is correct
6 Correct 31 ms 27768 KB Output is correct
7 Correct 35 ms 27740 KB Output is correct
8 Correct 32 ms 27748 KB Output is correct
9 Correct 31 ms 27772 KB Output is correct
10 Correct 31 ms 27776 KB Output is correct
11 Correct 30 ms 27768 KB Output is correct
12 Correct 34 ms 27776 KB Output is correct
13 Correct 31 ms 27692 KB Output is correct
14 Correct 33 ms 27768 KB Output is correct
15 Correct 31 ms 27768 KB Output is correct
16 Correct 31 ms 27768 KB Output is correct
17 Correct 33 ms 27776 KB Output is correct
18 Correct 31 ms 27776 KB Output is correct
19 Correct 32 ms 27768 KB Output is correct
20 Correct 32 ms 27776 KB Output is correct
21 Correct 32 ms 27768 KB Output is correct
22 Correct 31 ms 27768 KB Output is correct
23 Correct 31 ms 27768 KB Output is correct
24 Correct 33 ms 27768 KB Output is correct
25 Correct 34 ms 27904 KB Output is correct
26 Correct 117 ms 34672 KB Output is correct
27 Correct 196 ms 34468 KB Output is correct
28 Correct 33 ms 28024 KB Output is correct
29 Correct 34 ms 27768 KB Output is correct
30 Correct 30 ms 27776 KB Output is correct
31 Correct 166 ms 34524 KB Output is correct
32 Correct 33 ms 27896 KB Output is correct
33 Correct 254 ms 40084 KB Output is correct
34 Correct 156 ms 34604 KB Output is correct
35 Correct 33 ms 27964 KB Output is correct
36 Correct 217 ms 35028 KB Output is correct
37 Correct 32 ms 27904 KB Output is correct
38 Correct 30 ms 27904 KB Output is correct
39 Correct 116 ms 34784 KB Output is correct
40 Correct 31 ms 28160 KB Output is correct
41 Correct 181 ms 34316 KB Output is correct
42 Correct 218 ms 36216 KB Output is correct
43 Correct 32 ms 27852 KB Output is correct
44 Correct 292 ms 40372 KB Output is correct
45 Correct 211 ms 37368 KB Output is correct
46 Correct 36 ms 28024 KB Output is correct
47 Correct 31 ms 27896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 34768 KB Output is correct
2 Correct 164 ms 35476 KB Output is correct
3 Correct 36 ms 28024 KB Output is correct
4 Correct 32 ms 28028 KB Output is correct
5 Correct 31 ms 27768 KB Output is correct
6 Correct 33 ms 27776 KB Output is correct
7 Correct 33 ms 28152 KB Output is correct
8 Correct 171 ms 35164 KB Output is correct
9 Correct 30 ms 27904 KB Output is correct
10 Correct 159 ms 34624 KB Output is correct
11 Correct 32 ms 27776 KB Output is correct
12 Correct 211 ms 34680 KB Output is correct
13 Correct 178 ms 34960 KB Output is correct
14 Correct 207 ms 35296 KB Output is correct
15 Correct 108 ms 34672 KB Output is correct
16 Correct 32 ms 28024 KB Output is correct
17 Correct 31 ms 27776 KB Output is correct
18 Correct 127 ms 35524 KB Output is correct
19 Correct 251 ms 38328 KB Output is correct
20 Correct 32 ms 27896 KB Output is correct
21 Correct 33 ms 27852 KB Output is correct
22 Correct 140 ms 35320 KB Output is correct
23 Correct 34 ms 27972 KB Output is correct
24 Correct 197 ms 34764 KB Output is correct
25 Correct 178 ms 37212 KB Output is correct
26 Correct 31 ms 28024 KB Output is correct
27 Correct 32 ms 28024 KB Output is correct
28 Correct 34 ms 27896 KB Output is correct
29 Correct 35 ms 28028 KB Output is correct
30 Correct 33 ms 28032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 27776 KB Output is correct
2 Correct 35 ms 27768 KB Output is correct
3 Correct 29 ms 27776 KB Output is correct
4 Correct 31 ms 27776 KB Output is correct
5 Correct 37 ms 27768 KB Output is correct
6 Correct 31 ms 27768 KB Output is correct
7 Correct 35 ms 27740 KB Output is correct
8 Correct 32 ms 27748 KB Output is correct
9 Correct 31 ms 27772 KB Output is correct
10 Correct 31 ms 27776 KB Output is correct
11 Correct 30 ms 27768 KB Output is correct
12 Correct 34 ms 27776 KB Output is correct
13 Correct 31 ms 27692 KB Output is correct
14 Correct 33 ms 27768 KB Output is correct
15 Correct 31 ms 27768 KB Output is correct
16 Correct 31 ms 27768 KB Output is correct
17 Correct 33 ms 27776 KB Output is correct
18 Correct 31 ms 27776 KB Output is correct
19 Correct 32 ms 27768 KB Output is correct
20 Correct 32 ms 27776 KB Output is correct
21 Correct 32 ms 27768 KB Output is correct
22 Correct 31 ms 27768 KB Output is correct
23 Correct 31 ms 27768 KB Output is correct
24 Correct 33 ms 27768 KB Output is correct
25 Correct 32 ms 27808 KB Output is correct
26 Correct 33 ms 27992 KB Output is correct
27 Correct 38 ms 28100 KB Output is correct
28 Correct 33 ms 28024 KB Output is correct
29 Correct 39 ms 28032 KB Output is correct
30 Correct 33 ms 27996 KB Output is correct
31 Correct 30 ms 27768 KB Output is correct
32 Correct 36 ms 28152 KB Output is correct
33 Correct 36 ms 27768 KB Output is correct
34 Correct 33 ms 27996 KB Output is correct
35 Correct 31 ms 28032 KB Output is correct
36 Correct 31 ms 28024 KB Output is correct
37 Correct 33 ms 27996 KB Output is correct
38 Correct 33 ms 27768 KB Output is correct
39 Correct 34 ms 27968 KB Output is correct
40 Correct 33 ms 28024 KB Output is correct
41 Correct 37 ms 28024 KB Output is correct
42 Correct 34 ms 28024 KB Output is correct
43 Correct 32 ms 28152 KB Output is correct
44 Correct 32 ms 27876 KB Output is correct
45 Correct 33 ms 28060 KB Output is correct
46 Correct 31 ms 27964 KB Output is correct
47 Correct 30 ms 27824 KB Output is correct
48 Correct 39 ms 28024 KB Output is correct
49 Correct 35 ms 28024 KB Output is correct
50 Correct 34 ms 27996 KB Output is correct
51 Correct 35 ms 28024 KB Output is correct
52 Correct 31 ms 28024 KB Output is correct
53 Correct 36 ms 28152 KB Output is correct
54 Correct 32 ms 27936 KB Output is correct
55 Correct 34 ms 27904 KB Output is correct
56 Correct 117 ms 34672 KB Output is correct
57 Correct 196 ms 34468 KB Output is correct
58 Correct 33 ms 28024 KB Output is correct
59 Correct 34 ms 27768 KB Output is correct
60 Correct 30 ms 27776 KB Output is correct
61 Correct 166 ms 34524 KB Output is correct
62 Correct 33 ms 27896 KB Output is correct
63 Correct 254 ms 40084 KB Output is correct
64 Correct 156 ms 34604 KB Output is correct
65 Correct 33 ms 27964 KB Output is correct
66 Correct 217 ms 35028 KB Output is correct
67 Correct 32 ms 27904 KB Output is correct
68 Correct 30 ms 27904 KB Output is correct
69 Correct 116 ms 34784 KB Output is correct
70 Correct 31 ms 28160 KB Output is correct
71 Correct 181 ms 34316 KB Output is correct
72 Correct 218 ms 36216 KB Output is correct
73 Correct 32 ms 27852 KB Output is correct
74 Correct 292 ms 40372 KB Output is correct
75 Correct 211 ms 37368 KB Output is correct
76 Correct 36 ms 28024 KB Output is correct
77 Correct 31 ms 27896 KB Output is correct
78 Correct 134 ms 34768 KB Output is correct
79 Correct 164 ms 35476 KB Output is correct
80 Correct 36 ms 28024 KB Output is correct
81 Correct 32 ms 28028 KB Output is correct
82 Correct 31 ms 27768 KB Output is correct
83 Correct 33 ms 27776 KB Output is correct
84 Correct 33 ms 28152 KB Output is correct
85 Correct 171 ms 35164 KB Output is correct
86 Correct 30 ms 27904 KB Output is correct
87 Correct 159 ms 34624 KB Output is correct
88 Correct 32 ms 27776 KB Output is correct
89 Correct 211 ms 34680 KB Output is correct
90 Correct 178 ms 34960 KB Output is correct
91 Correct 207 ms 35296 KB Output is correct
92 Correct 108 ms 34672 KB Output is correct
93 Correct 32 ms 28024 KB Output is correct
94 Correct 31 ms 27776 KB Output is correct
95 Correct 127 ms 35524 KB Output is correct
96 Correct 251 ms 38328 KB Output is correct
97 Correct 32 ms 27896 KB Output is correct
98 Correct 33 ms 27852 KB Output is correct
99 Correct 140 ms 35320 KB Output is correct
100 Correct 34 ms 27972 KB Output is correct
101 Correct 197 ms 34764 KB Output is correct
102 Correct 178 ms 37212 KB Output is correct
103 Correct 31 ms 28024 KB Output is correct
104 Correct 32 ms 28024 KB Output is correct
105 Correct 34 ms 27896 KB Output is correct
106 Correct 35 ms 28028 KB Output is correct
107 Correct 33 ms 28032 KB Output is correct
108 Correct 649 ms 66404 KB Output is correct
109 Correct 1374 ms 75772 KB Output is correct
110 Correct 1468 ms 84856 KB Output is correct
111 Correct 1347 ms 84584 KB Output is correct
112 Correct 1257 ms 76792 KB Output is correct
113 Correct 645 ms 67680 KB Output is correct
114 Correct 964 ms 61636 KB Output is correct
115 Correct 1049 ms 61532 KB Output is correct
116 Correct 946 ms 64376 KB Output is correct
117 Correct 1125 ms 66380 KB Output is correct
118 Correct 947 ms 62136 KB Output is correct
119 Correct 1144 ms 66296 KB Output is correct
120 Correct 1214 ms 77360 KB Output is correct
121 Correct 992 ms 66244 KB Output is correct
122 Correct 1027 ms 65588 KB Output is correct
123 Correct 592 ms 67788 KB Output is correct
124 Correct 1226 ms 65308 KB Output is correct