Submission #474495

# Submission time Handle Problem Language Result Execution time Memory
474495 2021-09-18T13:55:23 Z jamielim None (KOI18_family) C++14
100 / 100
601 ms 73504 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,ii> i4;
const int MOD=1000000007;
const int INF=1012345678;
const ll LLINF=1012345678012345678LL;
const double PI=3.1415926536;
const double EPS=1e-14;

int n[2],k;
vector<int> adj[2][300005];
int arr[2][300005];
int par[2][300005];
vector<pair<int,ii> > sorted;

void dfs(int x,int tree){
	if(x!=0&&x<=k){
		arr[tree][x]=1;
		par[tree][x]=x;
	}
	if(SZ(adj[tree][x])>0){
		for(int i=0;i<SZ(adj[tree][x]);i++){
			int y=adj[tree][x][i];
			dfs(y,tree);
			arr[tree][x]+=arr[tree][y];
			par[tree][x]=par[tree][y];
		}
		sorted.pb(arr[tree][x],mp(tree,x));
	}
}

int p[300005],sz[300005];
int root(int x){return p[x]=(p[x]==x?x:root(p[x]));}

int main(){
	scanf("%d%d%d",&n[0],&n[1],&k);
	int pa;
	for(int a=0;a<2;a++){
		for(int i=1;i<=n[a];i++){
			scanf("%d",&pa);
			adj[a][pa].pb(i);
		}
	}
	dfs(0,0);
	dfs(0,1);
	sort(ALL(sorted));
	for(int i=1;i<=k;i++){p[i]=i;sz[i]=1;}
	for(pair<int,ii> i:sorted){
		for(int j:adj[i.se.fi][i.se.se]){
			int ra=root(par[i.se.fi][i.se.se]),rb=root(par[i.se.fi][j]);
			if(ra==rb)continue;
			p[ra]=rb;
			sz[rb]+=sz[ra];
		}
		if(SZ(adj[i.se.fi][i.se.se])<=1)continue;
		if(sz[root(par[i.se.fi][i.se.se])]!=i.fi){
			printf("NO\n");
			return 0;
		}
	}
	printf("YES\n");
}

Compilation message

family.cpp: In function 'int main()':
family.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |  scanf("%d%d%d",&n[0],&n[1],&k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
family.cpp:49:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |    scanf("%d",&pa);
      |    ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14304 KB Output is correct
2 Correct 11 ms 14392 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14400 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14396 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14404 KB Output is correct
14 Correct 9 ms 14424 KB Output is correct
15 Correct 9 ms 14412 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 9 ms 14416 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14412 KB Output is correct
23 Correct 10 ms 14400 KB Output is correct
24 Correct 9 ms 14412 KB Output is correct
25 Correct 9 ms 14412 KB Output is correct
26 Correct 9 ms 14400 KB Output is correct
27 Correct 9 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 9 ms 14332 KB Output is correct
30 Correct 9 ms 14400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14304 KB Output is correct
2 Correct 11 ms 14392 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14400 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14396 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14404 KB Output is correct
14 Correct 9 ms 14424 KB Output is correct
15 Correct 9 ms 14412 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 9 ms 14416 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14412 KB Output is correct
23 Correct 10 ms 14400 KB Output is correct
24 Correct 9 ms 14412 KB Output is correct
25 Correct 9 ms 14412 KB Output is correct
26 Correct 9 ms 14400 KB Output is correct
27 Correct 9 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 9 ms 14332 KB Output is correct
30 Correct 9 ms 14400 KB Output is correct
31 Correct 9 ms 14400 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14364 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 9 ms 14412 KB Output is correct
36 Correct 9 ms 14344 KB Output is correct
37 Correct 9 ms 14380 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 9 ms 14368 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 9 ms 14412 KB Output is correct
44 Correct 10 ms 14316 KB Output is correct
45 Correct 9 ms 14400 KB Output is correct
46 Correct 9 ms 14392 KB Output is correct
47 Correct 9 ms 14412 KB Output is correct
48 Correct 12 ms 14400 KB Output is correct
49 Correct 9 ms 14412 KB Output is correct
50 Correct 9 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14304 KB Output is correct
2 Correct 11 ms 14392 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14400 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14396 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14404 KB Output is correct
14 Correct 9 ms 14424 KB Output is correct
15 Correct 9 ms 14412 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 9 ms 14416 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14412 KB Output is correct
23 Correct 10 ms 14400 KB Output is correct
24 Correct 9 ms 14412 KB Output is correct
25 Correct 9 ms 14412 KB Output is correct
26 Correct 9 ms 14400 KB Output is correct
27 Correct 9 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 9 ms 14332 KB Output is correct
30 Correct 9 ms 14400 KB Output is correct
31 Correct 9 ms 14400 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14364 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 9 ms 14412 KB Output is correct
36 Correct 9 ms 14344 KB Output is correct
37 Correct 9 ms 14380 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 9 ms 14368 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 9 ms 14412 KB Output is correct
44 Correct 10 ms 14316 KB Output is correct
45 Correct 9 ms 14400 KB Output is correct
46 Correct 9 ms 14392 KB Output is correct
47 Correct 9 ms 14412 KB Output is correct
48 Correct 12 ms 14400 KB Output is correct
49 Correct 9 ms 14412 KB Output is correct
50 Correct 9 ms 14412 KB Output is correct
51 Correct 11 ms 14616 KB Output is correct
52 Correct 12 ms 14736 KB Output is correct
53 Correct 12 ms 14796 KB Output is correct
54 Correct 14 ms 14668 KB Output is correct
55 Correct 10 ms 14648 KB Output is correct
56 Correct 12 ms 14596 KB Output is correct
57 Correct 11 ms 14540 KB Output is correct
58 Correct 12 ms 14668 KB Output is correct
59 Correct 13 ms 14668 KB Output is correct
60 Correct 12 ms 14524 KB Output is correct
61 Correct 12 ms 15052 KB Output is correct
62 Correct 14 ms 15052 KB Output is correct
63 Correct 12 ms 14536 KB Output is correct
64 Correct 11 ms 14588 KB Output is correct
65 Correct 9 ms 14396 KB Output is correct
66 Correct 12 ms 14668 KB Output is correct
67 Correct 11 ms 14668 KB Output is correct
68 Correct 11 ms 14796 KB Output is correct
69 Correct 13 ms 14796 KB Output is correct
70 Correct 11 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14304 KB Output is correct
2 Correct 11 ms 14392 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14400 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14396 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14404 KB Output is correct
14 Correct 9 ms 14424 KB Output is correct
15 Correct 9 ms 14412 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 9 ms 14416 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14412 KB Output is correct
23 Correct 10 ms 14400 KB Output is correct
24 Correct 9 ms 14412 KB Output is correct
25 Correct 9 ms 14412 KB Output is correct
26 Correct 9 ms 14400 KB Output is correct
27 Correct 9 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 9 ms 14332 KB Output is correct
30 Correct 9 ms 14400 KB Output is correct
31 Correct 9 ms 14400 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14364 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 9 ms 14412 KB Output is correct
36 Correct 9 ms 14344 KB Output is correct
37 Correct 9 ms 14380 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 9 ms 14368 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 9 ms 14412 KB Output is correct
44 Correct 10 ms 14316 KB Output is correct
45 Correct 9 ms 14400 KB Output is correct
46 Correct 9 ms 14392 KB Output is correct
47 Correct 9 ms 14412 KB Output is correct
48 Correct 12 ms 14400 KB Output is correct
49 Correct 9 ms 14412 KB Output is correct
50 Correct 9 ms 14412 KB Output is correct
51 Correct 11 ms 14616 KB Output is correct
52 Correct 12 ms 14736 KB Output is correct
53 Correct 12 ms 14796 KB Output is correct
54 Correct 14 ms 14668 KB Output is correct
55 Correct 10 ms 14648 KB Output is correct
56 Correct 12 ms 14596 KB Output is correct
57 Correct 11 ms 14540 KB Output is correct
58 Correct 12 ms 14668 KB Output is correct
59 Correct 13 ms 14668 KB Output is correct
60 Correct 12 ms 14524 KB Output is correct
61 Correct 12 ms 15052 KB Output is correct
62 Correct 14 ms 15052 KB Output is correct
63 Correct 12 ms 14536 KB Output is correct
64 Correct 11 ms 14588 KB Output is correct
65 Correct 9 ms 14396 KB Output is correct
66 Correct 12 ms 14668 KB Output is correct
67 Correct 11 ms 14668 KB Output is correct
68 Correct 11 ms 14796 KB Output is correct
69 Correct 13 ms 14796 KB Output is correct
70 Correct 11 ms 14540 KB Output is correct
71 Correct 61 ms 19584 KB Output is correct
72 Correct 47 ms 18020 KB Output is correct
73 Correct 33 ms 17864 KB Output is correct
74 Correct 62 ms 23104 KB Output is correct
75 Correct 64 ms 21820 KB Output is correct
76 Correct 577 ms 73504 KB Output is correct
77 Correct 601 ms 55772 KB Output is correct
78 Correct 299 ms 31452 KB Output is correct
79 Correct 428 ms 55772 KB Output is correct
80 Correct 518 ms 53284 KB Output is correct
81 Correct 48 ms 19512 KB Output is correct
82 Correct 52 ms 17944 KB Output is correct
83 Correct 40 ms 17984 KB Output is correct
84 Correct 66 ms 22812 KB Output is correct
85 Correct 65 ms 21820 KB Output is correct
86 Correct 325 ms 55208 KB Output is correct
87 Correct 354 ms 55224 KB Output is correct
88 Correct 268 ms 31356 KB Output is correct
89 Correct 432 ms 54444 KB Output is correct
90 Correct 416 ms 53584 KB Output is correct