답안 #262023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262023 2020-08-12T09:37:39 Z patrikpavic2 Mergers (JOI19_mergers) C++17
100 / 100
857 ms 97400 KB
#include <cstdio>
#include <cstdlib>
#include <vector>

#define PB push_back

using namespace std;

const int N = 5e5 + 500;

vector < int > v[N], v2[N];

int A[N], kol[N], siz[N], sol, bio[N], bla, n;
int hsh[N], par[N], zad[N];

inline int pr(int x){
	if(x == par[x]) return x;
	return par[x] = pr(par[x]);
}

inline void mrg(int x, int y){
	par[pr(x)] = pr(y);
}

int dfs(int x, int lst){
	int ret = hsh[x];
	for(int y : v[x]){ 
		if(y == lst) continue;
		ret += dfs(y, x);
	}
	//printf("x = %d ret = %d\n", x, ret);
	if(ret != 0)
		mrg(x, lst);
	return ret;
}

int f(int x, int lst){
	if((int)v2[x].size() == 1) return 1;
	if(x == lst){
		int dos = 0;
		for(int y : v2[x]){
			int nov = f(y, x);
			if(dos < nov) {nov ^= dos, dos ^= nov, nov ^= dos;}
			sol += nov, dos -= nov;
		}
		sol += dos;
		return 0;
	}
	int ret = 0;
	for(int y : v2[x])
		if(y != lst)
			ret += f(y, x);
	if(ret % 1){
		sol += ret / 2; ret = 1;
	}
	else{
		sol += (ret - 2) / 2;
		ret = 2;
	}
	return ret;
}

int main(){
	scanf("%d%d", &n, &bla);
	for(int i = 1;i < n;i++){
		int x, y; scanf("%d%d", &x, &y);
		v[x].PB(y), v[y].PB(x);
	}
	for(int i = 1;i <= n;i++){
		scanf("%d", A + i);
		zad[A[i]] = i; par[i] = i;
	}
	for(int i = 1;i <= n;i++){
		if(zad[A[i]] != i)
			hsh[i] = rand() * rand();
		else
			hsh[i] = -kol[A[i]];
		kol[A[i]] += hsh[i];
	}
	dfs(1, 1);
	for(int i = 1;i <= n;i++){
		for(int x : v[i])
			if(pr(x) != pr(i))
				v2[pr(i)].PB(pr(x));
	}
	if((int)v2[pr(1)].size() == 0){
		printf("0\n");
		return 0;	
	}
	
	int list = 0;
	for(int i = 1;i <= n;i++)
		if(v2[i].size() == 1)
			list++;
	printf("%d\n", (list + 1) / 2);	
	return 0;
	
	int root = -1;
	for(int i = 1;i <= n;i++)
		if(pr(i) == i && (int)v2[i].size() != 1)
			root = i;
	if(root == -1){
		printf("1\n");
		return 0;
	}
	f(root, root);
	printf("%d\n", sol);
	return 0;
}




















Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &bla);
  ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:66:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y; scanf("%d%d", &x, &y);
             ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", A + i);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 15 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 16 ms 23808 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 16 ms 23908 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 15 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 16 ms 23808 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 16 ms 23908 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 15 ms 23808 KB Output is correct
26 Correct 17 ms 24064 KB Output is correct
27 Correct 18 ms 24064 KB Output is correct
28 Correct 18 ms 24192 KB Output is correct
29 Correct 21 ms 24184 KB Output is correct
30 Correct 18 ms 24064 KB Output is correct
31 Correct 16 ms 23808 KB Output is correct
32 Correct 20 ms 24192 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 18 ms 24184 KB Output is correct
36 Correct 20 ms 24064 KB Output is correct
37 Correct 18 ms 24064 KB Output is correct
38 Correct 18 ms 23936 KB Output is correct
39 Correct 18 ms 24064 KB Output is correct
40 Correct 19 ms 24064 KB Output is correct
41 Correct 17 ms 24064 KB Output is correct
42 Correct 18 ms 24064 KB Output is correct
43 Correct 18 ms 24192 KB Output is correct
44 Correct 15 ms 23808 KB Output is correct
45 Correct 18 ms 24064 KB Output is correct
46 Correct 19 ms 24064 KB Output is correct
47 Correct 17 ms 23808 KB Output is correct
48 Correct 18 ms 24064 KB Output is correct
49 Correct 17 ms 24192 KB Output is correct
50 Correct 18 ms 24192 KB Output is correct
51 Correct 18 ms 24064 KB Output is correct
52 Correct 17 ms 24064 KB Output is correct
53 Correct 20 ms 24064 KB Output is correct
54 Correct 17 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 15 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 16 ms 23808 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 16 ms 23908 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 15 ms 23808 KB Output is correct
26 Correct 83 ms 30060 KB Output is correct
27 Correct 96 ms 29688 KB Output is correct
28 Correct 18 ms 24064 KB Output is correct
29 Correct 15 ms 23808 KB Output is correct
30 Correct 16 ms 23808 KB Output is correct
31 Correct 129 ms 29688 KB Output is correct
32 Correct 17 ms 24064 KB Output is correct
33 Correct 111 ms 36984 KB Output is correct
34 Correct 99 ms 29688 KB Output is correct
35 Correct 17 ms 24064 KB Output is correct
36 Correct 102 ms 30456 KB Output is correct
37 Correct 18 ms 24064 KB Output is correct
38 Correct 17 ms 24064 KB Output is correct
39 Correct 77 ms 29932 KB Output is correct
40 Correct 18 ms 24192 KB Output is correct
41 Correct 103 ms 29624 KB Output is correct
42 Correct 109 ms 32080 KB Output is correct
43 Correct 16 ms 23808 KB Output is correct
44 Correct 104 ms 37112 KB Output is correct
45 Correct 104 ms 33400 KB Output is correct
46 Correct 17 ms 24064 KB Output is correct
47 Correct 17 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 28528 KB Output is correct
2 Correct 88 ms 32876 KB Output is correct
3 Correct 20 ms 24064 KB Output is correct
4 Correct 18 ms 24064 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 24064 KB Output is correct
8 Correct 114 ms 30812 KB Output is correct
9 Correct 18 ms 24064 KB Output is correct
10 Correct 107 ms 29816 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 129 ms 29804 KB Output is correct
13 Correct 120 ms 30968 KB Output is correct
14 Correct 130 ms 34040 KB Output is correct
15 Correct 81 ms 29936 KB Output is correct
16 Correct 20 ms 24064 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 87 ms 33900 KB Output is correct
19 Correct 118 ms 37880 KB Output is correct
20 Correct 18 ms 24064 KB Output is correct
21 Correct 16 ms 23808 KB Output is correct
22 Correct 85 ms 31344 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 105 ms 30200 KB Output is correct
25 Correct 112 ms 36472 KB Output is correct
26 Correct 17 ms 24192 KB Output is correct
27 Correct 18 ms 24192 KB Output is correct
28 Correct 19 ms 24064 KB Output is correct
29 Correct 17 ms 24064 KB Output is correct
30 Correct 17 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 15 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 16 ms 23808 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 16 ms 23908 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 15 ms 23808 KB Output is correct
26 Correct 17 ms 24064 KB Output is correct
27 Correct 18 ms 24064 KB Output is correct
28 Correct 18 ms 24192 KB Output is correct
29 Correct 21 ms 24184 KB Output is correct
30 Correct 18 ms 24064 KB Output is correct
31 Correct 16 ms 23808 KB Output is correct
32 Correct 20 ms 24192 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 18 ms 24184 KB Output is correct
36 Correct 20 ms 24064 KB Output is correct
37 Correct 18 ms 24064 KB Output is correct
38 Correct 18 ms 23936 KB Output is correct
39 Correct 18 ms 24064 KB Output is correct
40 Correct 19 ms 24064 KB Output is correct
41 Correct 17 ms 24064 KB Output is correct
42 Correct 18 ms 24064 KB Output is correct
43 Correct 18 ms 24192 KB Output is correct
44 Correct 15 ms 23808 KB Output is correct
45 Correct 18 ms 24064 KB Output is correct
46 Correct 19 ms 24064 KB Output is correct
47 Correct 17 ms 23808 KB Output is correct
48 Correct 18 ms 24064 KB Output is correct
49 Correct 17 ms 24192 KB Output is correct
50 Correct 18 ms 24192 KB Output is correct
51 Correct 18 ms 24064 KB Output is correct
52 Correct 17 ms 24064 KB Output is correct
53 Correct 20 ms 24064 KB Output is correct
54 Correct 17 ms 24056 KB Output is correct
55 Correct 15 ms 23808 KB Output is correct
56 Correct 83 ms 30060 KB Output is correct
57 Correct 96 ms 29688 KB Output is correct
58 Correct 18 ms 24064 KB Output is correct
59 Correct 15 ms 23808 KB Output is correct
60 Correct 16 ms 23808 KB Output is correct
61 Correct 129 ms 29688 KB Output is correct
62 Correct 17 ms 24064 KB Output is correct
63 Correct 111 ms 36984 KB Output is correct
64 Correct 99 ms 29688 KB Output is correct
65 Correct 17 ms 24064 KB Output is correct
66 Correct 102 ms 30456 KB Output is correct
67 Correct 18 ms 24064 KB Output is correct
68 Correct 17 ms 24064 KB Output is correct
69 Correct 77 ms 29932 KB Output is correct
70 Correct 18 ms 24192 KB Output is correct
71 Correct 103 ms 29624 KB Output is correct
72 Correct 109 ms 32080 KB Output is correct
73 Correct 16 ms 23808 KB Output is correct
74 Correct 104 ms 37112 KB Output is correct
75 Correct 104 ms 33400 KB Output is correct
76 Correct 17 ms 24064 KB Output is correct
77 Correct 17 ms 24064 KB Output is correct
78 Correct 95 ms 28528 KB Output is correct
79 Correct 88 ms 32876 KB Output is correct
80 Correct 20 ms 24064 KB Output is correct
81 Correct 18 ms 24064 KB Output is correct
82 Correct 17 ms 23808 KB Output is correct
83 Correct 16 ms 23808 KB Output is correct
84 Correct 17 ms 24064 KB Output is correct
85 Correct 114 ms 30812 KB Output is correct
86 Correct 18 ms 24064 KB Output is correct
87 Correct 107 ms 29816 KB Output is correct
88 Correct 16 ms 23808 KB Output is correct
89 Correct 129 ms 29804 KB Output is correct
90 Correct 120 ms 30968 KB Output is correct
91 Correct 130 ms 34040 KB Output is correct
92 Correct 81 ms 29936 KB Output is correct
93 Correct 20 ms 24064 KB Output is correct
94 Correct 16 ms 23808 KB Output is correct
95 Correct 87 ms 33900 KB Output is correct
96 Correct 118 ms 37880 KB Output is correct
97 Correct 18 ms 24064 KB Output is correct
98 Correct 16 ms 23808 KB Output is correct
99 Correct 85 ms 31344 KB Output is correct
100 Correct 17 ms 24064 KB Output is correct
101 Correct 105 ms 30200 KB Output is correct
102 Correct 112 ms 36472 KB Output is correct
103 Correct 17 ms 24192 KB Output is correct
104 Correct 18 ms 24192 KB Output is correct
105 Correct 19 ms 24064 KB Output is correct
106 Correct 17 ms 24064 KB Output is correct
107 Correct 17 ms 24064 KB Output is correct
108 Correct 498 ms 60388 KB Output is correct
109 Correct 706 ms 71544 KB Output is correct
110 Correct 727 ms 82680 KB Output is correct
111 Correct 832 ms 97400 KB Output is correct
112 Correct 784 ms 82420 KB Output is correct
113 Correct 558 ms 75076 KB Output is correct
114 Correct 720 ms 53624 KB Output is correct
115 Correct 702 ms 53656 KB Output is correct
116 Correct 802 ms 56312 KB Output is correct
117 Correct 718 ms 76024 KB Output is correct
118 Correct 646 ms 53880 KB Output is correct
119 Correct 732 ms 75948 KB Output is correct
120 Correct 769 ms 89336 KB Output is correct
121 Correct 857 ms 75936 KB Output is correct
122 Correct 698 ms 61128 KB Output is correct
123 Correct 482 ms 78912 KB Output is correct
124 Correct 732 ms 68984 KB Output is correct