답안 #102942

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102942 2019-03-28T07:56:57 Z hugo_pm Mergers (JOI19_mergers) C++17
100 / 100
1262 ms 95076 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= b; --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)

#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second

const long long BIG = 1000000000000000000LL;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void cpr(string s, vector<int> v)
{ int i = 0; for (char c : s) { if (c == '$') cout << v[i++]; else cout << c; } cout << '\n'; }

void cpr(string s) { cpr(s, {}); }

void solve();
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

const int borne = 505*1000;
int drepr[borne];

int dfind(int x)
{
	if (drepr[x] == x) return x;
	else return drepr[x] = dfind(drepr[x]);
}

void dunir(int a, int b) // merge a dans b, b est le parent
{
	a = dfind(a); b = dfind(b);
	if (a == b) return;
	drepr[a] = b;
}

// ww

int nbNoeuds, nbEtats;
int par[borne], prof[borne];
vector<int> adj[borne];
int gpOfNod[borne];
int deg[borne];
vector<int> nodInGp[borne];

void combiner(int a, int b)
{
	a = dfind(a); b = dfind(b);
	while (a != b) {
		if (prof[a] < prof[b]) swap(a,b);
		dunir(a, par[a]);
		a = dfind(a);
	}
}

void dfsInit(int nod)
{
	for (int vo : adj[nod]) if (vo != par[nod]) {
		par[vo] = nod;
		prof[vo] = prof[nod] + 1;
		dfsInit(vo);
	}
}

void solve()
{
	form(i, borne) { drepr[i] = i; par[i] = -1; }
	cin >> nbNoeuds >> nbEtats;
	form(i, nbNoeuds-1) {
		int u, v;
		cin >> u >> v;
		--u; --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	prof[0] = 0;
	dfsInit(0);

	form(nod, nbNoeuds) {
		cin >> gpOfNod[nod]; --gpOfNod[nod];
		nodInGp[gpOfNod[nod]].push_back(nod);
	}

	form(grp, nbEtats) {
		int sz = nodInGp[grp].size();
		form2(i, 1, sz) {
			combiner(nodInGp[grp][0], nodInGp[grp][i]);
		}
	}

	form(nod, nbNoeuds) {
		for (int vo : adj[nod]) {
			if (dfind(nod) != dfind(vo)) ++deg[dfind(nod)];
		}
	}

	int lf = 0;
	form(nod, nbNoeuds) if (dfind(nod) == nod && deg[nod] == 1) ++lf;
	cout << (lf+1)/2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 32000 KB Output is correct
2 Correct 37 ms 31924 KB Output is correct
3 Correct 35 ms 31992 KB Output is correct
4 Correct 34 ms 31992 KB Output is correct
5 Correct 33 ms 32000 KB Output is correct
6 Correct 35 ms 31992 KB Output is correct
7 Correct 38 ms 32000 KB Output is correct
8 Correct 32 ms 31940 KB Output is correct
9 Correct 36 ms 31992 KB Output is correct
10 Correct 32 ms 31992 KB Output is correct
11 Correct 34 ms 31992 KB Output is correct
12 Correct 33 ms 31992 KB Output is correct
13 Correct 37 ms 31992 KB Output is correct
14 Correct 36 ms 31992 KB Output is correct
15 Correct 38 ms 31992 KB Output is correct
16 Correct 33 ms 32000 KB Output is correct
17 Correct 39 ms 32020 KB Output is correct
18 Correct 32 ms 32000 KB Output is correct
19 Correct 37 ms 31956 KB Output is correct
20 Correct 37 ms 31992 KB Output is correct
21 Correct 35 ms 31992 KB Output is correct
22 Correct 31 ms 31992 KB Output is correct
23 Correct 32 ms 32000 KB Output is correct
24 Correct 30 ms 31992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 32000 KB Output is correct
2 Correct 37 ms 31924 KB Output is correct
3 Correct 35 ms 31992 KB Output is correct
4 Correct 34 ms 31992 KB Output is correct
5 Correct 33 ms 32000 KB Output is correct
6 Correct 35 ms 31992 KB Output is correct
7 Correct 38 ms 32000 KB Output is correct
8 Correct 32 ms 31940 KB Output is correct
9 Correct 36 ms 31992 KB Output is correct
10 Correct 32 ms 31992 KB Output is correct
11 Correct 34 ms 31992 KB Output is correct
12 Correct 33 ms 31992 KB Output is correct
13 Correct 37 ms 31992 KB Output is correct
14 Correct 36 ms 31992 KB Output is correct
15 Correct 38 ms 31992 KB Output is correct
16 Correct 33 ms 32000 KB Output is correct
17 Correct 39 ms 32020 KB Output is correct
18 Correct 32 ms 32000 KB Output is correct
19 Correct 37 ms 31956 KB Output is correct
20 Correct 37 ms 31992 KB Output is correct
21 Correct 35 ms 31992 KB Output is correct
22 Correct 31 ms 31992 KB Output is correct
23 Correct 32 ms 32000 KB Output is correct
24 Correct 30 ms 31992 KB Output is correct
25 Correct 36 ms 31920 KB Output is correct
26 Correct 35 ms 32248 KB Output is correct
27 Correct 36 ms 32248 KB Output is correct
28 Correct 33 ms 32300 KB Output is correct
29 Correct 38 ms 32296 KB Output is correct
30 Correct 37 ms 32260 KB Output is correct
31 Correct 33 ms 31872 KB Output is correct
32 Correct 32 ms 32376 KB Output is correct
33 Correct 33 ms 31992 KB Output is correct
34 Correct 33 ms 32248 KB Output is correct
35 Correct 35 ms 32332 KB Output is correct
36 Correct 40 ms 32248 KB Output is correct
37 Correct 33 ms 32248 KB Output is correct
38 Correct 33 ms 31992 KB Output is correct
39 Correct 40 ms 32208 KB Output is correct
40 Correct 38 ms 32256 KB Output is correct
41 Correct 37 ms 32164 KB Output is correct
42 Correct 36 ms 32256 KB Output is correct
43 Correct 41 ms 32248 KB Output is correct
44 Correct 33 ms 31992 KB Output is correct
45 Correct 35 ms 32260 KB Output is correct
46 Correct 34 ms 32256 KB Output is correct
47 Correct 38 ms 32020 KB Output is correct
48 Correct 38 ms 32292 KB Output is correct
49 Correct 36 ms 32276 KB Output is correct
50 Correct 36 ms 32348 KB Output is correct
51 Correct 34 ms 32248 KB Output is correct
52 Correct 36 ms 32228 KB Output is correct
53 Correct 39 ms 32340 KB Output is correct
54 Correct 34 ms 32256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 32000 KB Output is correct
2 Correct 37 ms 31924 KB Output is correct
3 Correct 35 ms 31992 KB Output is correct
4 Correct 34 ms 31992 KB Output is correct
5 Correct 33 ms 32000 KB Output is correct
6 Correct 35 ms 31992 KB Output is correct
7 Correct 38 ms 32000 KB Output is correct
8 Correct 32 ms 31940 KB Output is correct
9 Correct 36 ms 31992 KB Output is correct
10 Correct 32 ms 31992 KB Output is correct
11 Correct 34 ms 31992 KB Output is correct
12 Correct 33 ms 31992 KB Output is correct
13 Correct 37 ms 31992 KB Output is correct
14 Correct 36 ms 31992 KB Output is correct
15 Correct 38 ms 31992 KB Output is correct
16 Correct 33 ms 32000 KB Output is correct
17 Correct 39 ms 32020 KB Output is correct
18 Correct 32 ms 32000 KB Output is correct
19 Correct 37 ms 31956 KB Output is correct
20 Correct 37 ms 31992 KB Output is correct
21 Correct 35 ms 31992 KB Output is correct
22 Correct 31 ms 31992 KB Output is correct
23 Correct 32 ms 32000 KB Output is correct
24 Correct 30 ms 31992 KB Output is correct
25 Correct 33 ms 32056 KB Output is correct
26 Correct 128 ms 40108 KB Output is correct
27 Correct 147 ms 39664 KB Output is correct
28 Correct 39 ms 32264 KB Output is correct
29 Correct 33 ms 31992 KB Output is correct
30 Correct 32 ms 31920 KB Output is correct
31 Correct 151 ms 39852 KB Output is correct
32 Correct 35 ms 32196 KB Output is correct
33 Correct 189 ms 42052 KB Output is correct
34 Correct 190 ms 39796 KB Output is correct
35 Correct 39 ms 32424 KB Output is correct
36 Correct 206 ms 39544 KB Output is correct
37 Correct 44 ms 32252 KB Output is correct
38 Correct 40 ms 32248 KB Output is correct
39 Correct 132 ms 39912 KB Output is correct
40 Correct 38 ms 32376 KB Output is correct
41 Correct 151 ms 39508 KB Output is correct
42 Correct 176 ms 40028 KB Output is correct
43 Correct 33 ms 31992 KB Output is correct
44 Correct 190 ms 41980 KB Output is correct
45 Correct 163 ms 41848 KB Output is correct
46 Correct 35 ms 32248 KB Output is correct
47 Correct 36 ms 32248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 39912 KB Output is correct
2 Correct 163 ms 43136 KB Output is correct
3 Correct 35 ms 32248 KB Output is correct
4 Correct 35 ms 32116 KB Output is correct
5 Correct 38 ms 31964 KB Output is correct
6 Correct 32 ms 32016 KB Output is correct
7 Correct 33 ms 32248 KB Output is correct
8 Correct 167 ms 41632 KB Output is correct
9 Correct 34 ms 32256 KB Output is correct
10 Correct 145 ms 40768 KB Output is correct
11 Correct 33 ms 32000 KB Output is correct
12 Correct 166 ms 40156 KB Output is correct
13 Correct 174 ms 41580 KB Output is correct
14 Correct 195 ms 43000 KB Output is correct
15 Correct 144 ms 39912 KB Output is correct
16 Correct 36 ms 32248 KB Output is correct
17 Correct 35 ms 32124 KB Output is correct
18 Correct 166 ms 42828 KB Output is correct
19 Correct 157 ms 43988 KB Output is correct
20 Correct 34 ms 32248 KB Output is correct
21 Correct 31 ms 32000 KB Output is correct
22 Correct 173 ms 41692 KB Output is correct
23 Correct 36 ms 32248 KB Output is correct
24 Correct 197 ms 40960 KB Output is correct
25 Correct 237 ms 44156 KB Output is correct
26 Correct 43 ms 32248 KB Output is correct
27 Correct 37 ms 32248 KB Output is correct
28 Correct 39 ms 32376 KB Output is correct
29 Correct 38 ms 32248 KB Output is correct
30 Correct 37 ms 32248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 32000 KB Output is correct
2 Correct 37 ms 31924 KB Output is correct
3 Correct 35 ms 31992 KB Output is correct
4 Correct 34 ms 31992 KB Output is correct
5 Correct 33 ms 32000 KB Output is correct
6 Correct 35 ms 31992 KB Output is correct
7 Correct 38 ms 32000 KB Output is correct
8 Correct 32 ms 31940 KB Output is correct
9 Correct 36 ms 31992 KB Output is correct
10 Correct 32 ms 31992 KB Output is correct
11 Correct 34 ms 31992 KB Output is correct
12 Correct 33 ms 31992 KB Output is correct
13 Correct 37 ms 31992 KB Output is correct
14 Correct 36 ms 31992 KB Output is correct
15 Correct 38 ms 31992 KB Output is correct
16 Correct 33 ms 32000 KB Output is correct
17 Correct 39 ms 32020 KB Output is correct
18 Correct 32 ms 32000 KB Output is correct
19 Correct 37 ms 31956 KB Output is correct
20 Correct 37 ms 31992 KB Output is correct
21 Correct 35 ms 31992 KB Output is correct
22 Correct 31 ms 31992 KB Output is correct
23 Correct 32 ms 32000 KB Output is correct
24 Correct 30 ms 31992 KB Output is correct
25 Correct 36 ms 31920 KB Output is correct
26 Correct 35 ms 32248 KB Output is correct
27 Correct 36 ms 32248 KB Output is correct
28 Correct 33 ms 32300 KB Output is correct
29 Correct 38 ms 32296 KB Output is correct
30 Correct 37 ms 32260 KB Output is correct
31 Correct 33 ms 31872 KB Output is correct
32 Correct 32 ms 32376 KB Output is correct
33 Correct 33 ms 31992 KB Output is correct
34 Correct 33 ms 32248 KB Output is correct
35 Correct 35 ms 32332 KB Output is correct
36 Correct 40 ms 32248 KB Output is correct
37 Correct 33 ms 32248 KB Output is correct
38 Correct 33 ms 31992 KB Output is correct
39 Correct 40 ms 32208 KB Output is correct
40 Correct 38 ms 32256 KB Output is correct
41 Correct 37 ms 32164 KB Output is correct
42 Correct 36 ms 32256 KB Output is correct
43 Correct 41 ms 32248 KB Output is correct
44 Correct 33 ms 31992 KB Output is correct
45 Correct 35 ms 32260 KB Output is correct
46 Correct 34 ms 32256 KB Output is correct
47 Correct 38 ms 32020 KB Output is correct
48 Correct 38 ms 32292 KB Output is correct
49 Correct 36 ms 32276 KB Output is correct
50 Correct 36 ms 32348 KB Output is correct
51 Correct 34 ms 32248 KB Output is correct
52 Correct 36 ms 32228 KB Output is correct
53 Correct 39 ms 32340 KB Output is correct
54 Correct 34 ms 32256 KB Output is correct
55 Correct 33 ms 32056 KB Output is correct
56 Correct 128 ms 40108 KB Output is correct
57 Correct 147 ms 39664 KB Output is correct
58 Correct 39 ms 32264 KB Output is correct
59 Correct 33 ms 31992 KB Output is correct
60 Correct 32 ms 31920 KB Output is correct
61 Correct 151 ms 39852 KB Output is correct
62 Correct 35 ms 32196 KB Output is correct
63 Correct 189 ms 42052 KB Output is correct
64 Correct 190 ms 39796 KB Output is correct
65 Correct 39 ms 32424 KB Output is correct
66 Correct 206 ms 39544 KB Output is correct
67 Correct 44 ms 32252 KB Output is correct
68 Correct 40 ms 32248 KB Output is correct
69 Correct 132 ms 39912 KB Output is correct
70 Correct 38 ms 32376 KB Output is correct
71 Correct 151 ms 39508 KB Output is correct
72 Correct 176 ms 40028 KB Output is correct
73 Correct 33 ms 31992 KB Output is correct
74 Correct 190 ms 41980 KB Output is correct
75 Correct 163 ms 41848 KB Output is correct
76 Correct 35 ms 32248 KB Output is correct
77 Correct 36 ms 32248 KB Output is correct
78 Correct 113 ms 39912 KB Output is correct
79 Correct 163 ms 43136 KB Output is correct
80 Correct 35 ms 32248 KB Output is correct
81 Correct 35 ms 32116 KB Output is correct
82 Correct 38 ms 31964 KB Output is correct
83 Correct 32 ms 32016 KB Output is correct
84 Correct 33 ms 32248 KB Output is correct
85 Correct 167 ms 41632 KB Output is correct
86 Correct 34 ms 32256 KB Output is correct
87 Correct 145 ms 40768 KB Output is correct
88 Correct 33 ms 32000 KB Output is correct
89 Correct 166 ms 40156 KB Output is correct
90 Correct 174 ms 41580 KB Output is correct
91 Correct 195 ms 43000 KB Output is correct
92 Correct 144 ms 39912 KB Output is correct
93 Correct 36 ms 32248 KB Output is correct
94 Correct 35 ms 32124 KB Output is correct
95 Correct 166 ms 42828 KB Output is correct
96 Correct 157 ms 43988 KB Output is correct
97 Correct 34 ms 32248 KB Output is correct
98 Correct 31 ms 32000 KB Output is correct
99 Correct 173 ms 41692 KB Output is correct
100 Correct 36 ms 32248 KB Output is correct
101 Correct 197 ms 40960 KB Output is correct
102 Correct 237 ms 44156 KB Output is correct
103 Correct 43 ms 32248 KB Output is correct
104 Correct 37 ms 32248 KB Output is correct
105 Correct 39 ms 32376 KB Output is correct
106 Correct 38 ms 32248 KB Output is correct
107 Correct 37 ms 32248 KB Output is correct
108 Correct 819 ms 81956 KB Output is correct
109 Correct 1108 ms 74764 KB Output is correct
110 Correct 1106 ms 79068 KB Output is correct
111 Correct 942 ms 94428 KB Output is correct
112 Correct 1262 ms 94800 KB Output is correct
113 Correct 765 ms 87548 KB Output is correct
114 Correct 873 ms 70288 KB Output is correct
115 Correct 929 ms 70176 KB Output is correct
116 Correct 1227 ms 78064 KB Output is correct
117 Correct 983 ms 88244 KB Output is correct
118 Correct 932 ms 74488 KB Output is correct
119 Correct 906 ms 88180 KB Output is correct
120 Correct 1006 ms 95076 KB Output is correct
121 Correct 916 ms 88028 KB Output is correct
122 Correct 1202 ms 82640 KB Output is correct
123 Correct 717 ms 89136 KB Output is correct
124 Correct 1142 ms 84200 KB Output is correct