답안 #1014572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014572 2024-07-05T07:38:07 Z NintsiChkhaidze Mergers (JOI19_mergers) C++17
48 / 100
3000 ms 49852 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define int ll
using namespace std;

const int N = 3e5 + 5,inf = 1e18;
vector <pii> v[N],vec[N];
int col[N],tin[N],timer,d[25][N],dep[N];
int edge[N],val[N],n,D,I;

void dfs(int x,int par){
	d[0][x] = par;
	dep[x] = dep[par] + 1;
	tin[x] = ++timer;

	for (auto [to, id]: v[x]){
		if (to != par) {
			edge[to] = id;
			dfs(to,x);
		}
	}
}

int lca(int x,int y){
	if (dep[x] < dep[y ]) swap(x,y);
	for (int i = 20; i >= 0; i--)
		if (dep[d[i][x]] >= dep[y]) x = d[i][x];
	if (x == y) return x;

	for (int i = 20; i >= 0; i--)
		if (d[i][x] != d[i][y]) x = d[i][x],y = d[i][y];

	return d[0][x];
}

void getpath(int x,int y){
	int c = lca(x,y);
	while (x != c){
		val[edge[x]] = 0;
		x = d[0][x];
	}

	while (y != c){
		val[edge[y]] = 0;
		y = d[0][y];
	}
}	


void DFS(int x,int par,int dd){
	if (dd >= D) D = dd,I = x;

	for (auto [to,id]: v[x]){
		if (to != par) DFS(to,x,dd + val[id]);
	}
}

signed main() {
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

	int k;
	cin>>n>>k;

	for (int i = 1; i < n; i++){
		int a,b;
		cin>>a>>b;
		v[a].pb({b,i});
		v[b].pb({a,i});
		val[i] = 1;
	}

	dfs(1,1);
	for (int j = 1; j <= 20 ; j++)
		for (int i = 1; i <= n; i++)
			d[j][i] = d[j - 1][d[j - 1][i]];

	for (int i = 1; i <= n; i++)
		cin >> col[i],vec[col[i]].pb({tin[i],i});

	for (int i = 1; i <= k; i++){
		sort(vec[i].begin(),vec[i].end());
		for (int j = 1; j < vec[i].size(); j++){
			getpath(vec[i][j].s,vec[i][j - 1].s);
		}
	}

	int sum = 0;
	for (int i = 1; i < n; i++)
		sum += val[i];

	int ans = 0;
	while (sum > 0){
		++ans;
		I = D = 0; DFS(1,1,0);
		int x1 = I; 
		I = D = 0; DFS(x1,x1,0);
		int y1 = I;

		getpath(x1,y1);
		sum -= D;
	}

	cout<<ans;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:87:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for (int j = 1; j < vec[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 3 ms 15192 KB Output is correct
3 Correct 2 ms 15196 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 2 ms 15056 KB Output is correct
7 Correct 3 ms 15060 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 15192 KB Output is correct
10 Correct 2 ms 15192 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17240 KB Output is correct
14 Correct 2 ms 15196 KB Output is correct
15 Correct 2 ms 15192 KB Output is correct
16 Correct 2 ms 15196 KB Output is correct
17 Correct 3 ms 19292 KB Output is correct
18 Correct 2 ms 15196 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 2 ms 15196 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 19292 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 2 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 3 ms 15192 KB Output is correct
3 Correct 2 ms 15196 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 2 ms 15056 KB Output is correct
7 Correct 3 ms 15060 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 15192 KB Output is correct
10 Correct 2 ms 15192 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17240 KB Output is correct
14 Correct 2 ms 15196 KB Output is correct
15 Correct 2 ms 15192 KB Output is correct
16 Correct 2 ms 15196 KB Output is correct
17 Correct 3 ms 19292 KB Output is correct
18 Correct 2 ms 15196 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 2 ms 15196 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 19292 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 2 ms 15196 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 17 ms 16144 KB Output is correct
27 Correct 5 ms 15964 KB Output is correct
28 Correct 37 ms 16220 KB Output is correct
29 Correct 28 ms 20060 KB Output is correct
30 Correct 4 ms 15960 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 4 ms 16316 KB Output is correct
33 Correct 4 ms 15196 KB Output is correct
34 Correct 5 ms 16056 KB Output is correct
35 Correct 30 ms 18008 KB Output is correct
36 Correct 5 ms 15964 KB Output is correct
37 Correct 9 ms 18164 KB Output is correct
38 Correct 4 ms 15192 KB Output is correct
39 Correct 10 ms 15960 KB Output is correct
40 Correct 4 ms 15960 KB Output is correct
41 Correct 4 ms 16176 KB Output is correct
42 Correct 10 ms 15964 KB Output is correct
43 Correct 5 ms 18268 KB Output is correct
44 Correct 4 ms 19128 KB Output is correct
45 Correct 5 ms 15964 KB Output is correct
46 Correct 11 ms 18012 KB Output is correct
47 Correct 3 ms 15196 KB Output is correct
48 Correct 7 ms 15964 KB Output is correct
49 Correct 34 ms 18012 KB Output is correct
50 Correct 36 ms 16220 KB Output is correct
51 Correct 11 ms 18264 KB Output is correct
52 Correct 4 ms 15964 KB Output is correct
53 Correct 9 ms 18012 KB Output is correct
54 Correct 5 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 3 ms 15192 KB Output is correct
3 Correct 2 ms 15196 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 2 ms 15056 KB Output is correct
7 Correct 3 ms 15060 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 15192 KB Output is correct
10 Correct 2 ms 15192 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17240 KB Output is correct
14 Correct 2 ms 15196 KB Output is correct
15 Correct 2 ms 15192 KB Output is correct
16 Correct 2 ms 15196 KB Output is correct
17 Correct 3 ms 19292 KB Output is correct
18 Correct 2 ms 15196 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 2 ms 15196 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 19292 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 2 ms 15196 KB Output is correct
25 Correct 3 ms 17240 KB Output is correct
26 Correct 68 ms 45300 KB Output is correct
27 Correct 94 ms 44100 KB Output is correct
28 Correct 6 ms 15960 KB Output is correct
29 Correct 3 ms 15052 KB Output is correct
30 Correct 2 ms 15036 KB Output is correct
31 Correct 129 ms 43860 KB Output is correct
32 Correct 4 ms 15964 KB Output is correct
33 Correct 165 ms 48576 KB Output is correct
34 Correct 92 ms 45756 KB Output is correct
35 Correct 5 ms 18264 KB Output is correct
36 Correct 198 ms 44688 KB Output is correct
37 Correct 5 ms 20056 KB Output is correct
38 Correct 4 ms 15964 KB Output is correct
39 Correct 64 ms 43540 KB Output is correct
40 Correct 5 ms 18264 KB Output is correct
41 Correct 72 ms 45364 KB Output is correct
42 Correct 154 ms 47696 KB Output is correct
43 Correct 3 ms 15196 KB Output is correct
44 Correct 91 ms 49852 KB Output is correct
45 Correct 196 ms 48720 KB Output is correct
46 Correct 6 ms 15964 KB Output is correct
47 Correct 5 ms 16096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 43852 KB Output is correct
2 Execution timed out 3075 ms 44988 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 3 ms 15192 KB Output is correct
3 Correct 2 ms 15196 KB Output is correct
4 Correct 3 ms 15192 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 2 ms 15056 KB Output is correct
7 Correct 3 ms 15060 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 15192 KB Output is correct
10 Correct 2 ms 15192 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17240 KB Output is correct
14 Correct 2 ms 15196 KB Output is correct
15 Correct 2 ms 15192 KB Output is correct
16 Correct 2 ms 15196 KB Output is correct
17 Correct 3 ms 19292 KB Output is correct
18 Correct 2 ms 15196 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 2 ms 15196 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 19292 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 2 ms 15196 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 17 ms 16144 KB Output is correct
27 Correct 5 ms 15964 KB Output is correct
28 Correct 37 ms 16220 KB Output is correct
29 Correct 28 ms 20060 KB Output is correct
30 Correct 4 ms 15960 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 4 ms 16316 KB Output is correct
33 Correct 4 ms 15196 KB Output is correct
34 Correct 5 ms 16056 KB Output is correct
35 Correct 30 ms 18008 KB Output is correct
36 Correct 5 ms 15964 KB Output is correct
37 Correct 9 ms 18164 KB Output is correct
38 Correct 4 ms 15192 KB Output is correct
39 Correct 10 ms 15960 KB Output is correct
40 Correct 4 ms 15960 KB Output is correct
41 Correct 4 ms 16176 KB Output is correct
42 Correct 10 ms 15964 KB Output is correct
43 Correct 5 ms 18268 KB Output is correct
44 Correct 4 ms 19128 KB Output is correct
45 Correct 5 ms 15964 KB Output is correct
46 Correct 11 ms 18012 KB Output is correct
47 Correct 3 ms 15196 KB Output is correct
48 Correct 7 ms 15964 KB Output is correct
49 Correct 34 ms 18012 KB Output is correct
50 Correct 36 ms 16220 KB Output is correct
51 Correct 11 ms 18264 KB Output is correct
52 Correct 4 ms 15964 KB Output is correct
53 Correct 9 ms 18012 KB Output is correct
54 Correct 5 ms 18012 KB Output is correct
55 Correct 3 ms 17240 KB Output is correct
56 Correct 68 ms 45300 KB Output is correct
57 Correct 94 ms 44100 KB Output is correct
58 Correct 6 ms 15960 KB Output is correct
59 Correct 3 ms 15052 KB Output is correct
60 Correct 2 ms 15036 KB Output is correct
61 Correct 129 ms 43860 KB Output is correct
62 Correct 4 ms 15964 KB Output is correct
63 Correct 165 ms 48576 KB Output is correct
64 Correct 92 ms 45756 KB Output is correct
65 Correct 5 ms 18264 KB Output is correct
66 Correct 198 ms 44688 KB Output is correct
67 Correct 5 ms 20056 KB Output is correct
68 Correct 4 ms 15964 KB Output is correct
69 Correct 64 ms 43540 KB Output is correct
70 Correct 5 ms 18264 KB Output is correct
71 Correct 72 ms 45364 KB Output is correct
72 Correct 154 ms 47696 KB Output is correct
73 Correct 3 ms 15196 KB Output is correct
74 Correct 91 ms 49852 KB Output is correct
75 Correct 196 ms 48720 KB Output is correct
76 Correct 6 ms 15964 KB Output is correct
77 Correct 5 ms 16096 KB Output is correct
78 Correct 67 ms 43852 KB Output is correct
79 Execution timed out 3075 ms 44988 KB Time limit exceeded
80 Halted 0 ms 0 KB -