Submission #533760

# Submission time Handle Problem Language Result Execution time Memory
533760 2022-03-07T07:09:25 Z 1bin Road Closures (APIO21_roads) C++14
12 / 100
220 ms 87596 KB
#include <bits/stdc++.h>
#include <cassert>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
set<pair<ll, ll>> adj[NMAX];
int dg[NMAX], vis[NMAX];
ll dp[NMAX][2], S[NMAX];
vector<int> D[NMAX];
set<int> X;
multiset<ll> A[NMAX], C[NMAX], t[NMAX];

void dfs(int now, int k) {
	vis[now] = 1;
	ll& ret = dp[now][0] = 0;

	vector<ll> tmp;
	ll c = 0;
	for (auto& p : adj[now]) {
		ll nx = p.first, w = p.second;
		if (vis[nx]) continue;
		dfs(nx, k);
		ret += dp[nx][0] + w;
		ll t = dp[nx][1] - dp[nx][0] - w;
		c++;
		tmp.emplace_back(t);
	}

	while (t[now].size() && A[now].size() < k - c) {
		auto it = t[now].begin();
		A[now].emplace(*it); S[now] += *it;
		t[now].erase(it);
	}
	while (A[now].size() && A[now].size() > k - c) {
		auto it = --A[now].end(); S[now] -= *it;
		t[now].emplace(*it);
		A[now].erase(it);
	}
	ret += S[now];
	if (k - c <= 0 && A[now].size() > 0) assert(0);
	/*cout << "now is : " << now << ' ' << ret << '\n';
	for (ll x : A[now]) cout << x << ' ';
	cout << '\n';
	for (ll x : C[now]) cout << x << ' ';
	cout << '\n';*/

	for (ll x : tmp) C[now].emplace(x);
	auto it = t[now].begin();
	for (int i = 0; i < c && t[now].size(); i++) C[now].emplace(*it++);

	if (!c) {
		//cout << k << ' ' << now << ' ' << ret << '\n';
		//cout << ret << '\n';
		dp[now][1] = ret - *A[now].rbegin();
		return;
	}
	it = C[now].begin();
	int sz = k - 1 - A[now].size();
	//if (sz < 0 && c >= k && A[now].size()) assert(0);
	while(sz--) ret += min(*it++, 0LL);
	dp[now][1] = ret;
	ret += min(*it, 0LL);

	//cout << ret << '\n';
	//cout << "Now is : " << now << '\n';
	//for(ll x : C[now]) cout << x << ' ';
	//cout << '\n';
	C[now].clear();
	return;
}

vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
	vector<ll> ans(N, 0);
	for (int i = 0; i < N - 1; i++) {
		adj[U[i]].emplace(V[i], W[i]);
		adj[V[i]].emplace(U[i], W[i]);
		dg[U[i]]++; dg[V[i]]++;
		ans[0] += W[i];
	}
	for (int i = 0; i < N; i++) {
		D[dg[i]].emplace_back(i);
		X.emplace(i);
	}

	for (int k = 1; k < N; k++) {
		// 노드 삭제하기
		//cout << "NOW TURN IS : " << k << '\n';
		for (int x : D[k]) {
			X.erase(x);
			//cout << "erase : " << x << '\n';
			for (auto& p : adj[x]) {
				ll nx = p.first, w = p.second;
				adj[nx].erase({ x, w });
				S[nx] += w; t[nx].emplace(-w);
			}
		}

		// 각 트리에 대해서 탐색
		for (int x : X) if (!vis[x]) {
			dfs(x, k);
			ans[k] += dp[x][0];
		}
		for (int x : X) vis[x] = 0;
	}
	return ans;
}


/*
5
0 1 1
0 2 4
0 3 3
2 4 2
*/

/*
4
0 1 5
0 3 5
0 2 10
*/

/*
5
0 1 5
1 2 6
2 3 1
3 4 8
*/

/*
5
0 1 1
0 2 2
0 3 3
0 4 4
*/

//int main(void) {
//	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//
//	freopen("input.txt", "r", stdin);
//	int N;
//	cin >> N;
//	vector<int> U(N), V(N), W(N);
//	for(int i = 0; i < N - 1; i++) cin >> U[i] >> V[i] >> W[i];
//	vector<ll> ans = minimum_closure_costs(N, U, V, W);
//	for(int i = 0; i < N; i++) cout << ans[i] << ' ';
//	return 0;
//}

Compilation message

roads.cpp: In function 'void dfs(int, int)':
roads.cpp:32:40: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   32 |  while (t[now].size() && A[now].size() < k - c) {
      |                          ~~~~~~~~~~~~~~^~~~~~~
roads.cpp:37:40: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   37 |  while (A[now].size() && A[now].size() > k - c) {
      |                          ~~~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 21324 KB Output is correct
2 Correct 13 ms 21836 KB Output is correct
3 Correct 13 ms 21840 KB Output is correct
4 Correct 13 ms 21836 KB Output is correct
5 Correct 10 ms 21420 KB Output is correct
6 Correct 11 ms 21452 KB Output is correct
7 Correct 10 ms 21452 KB Output is correct
8 Correct 13 ms 21708 KB Output is correct
9 Correct 12 ms 21880 KB Output is correct
10 Correct 10 ms 21452 KB Output is correct
11 Correct 11 ms 21408 KB Output is correct
12 Correct 117 ms 34048 KB Output is correct
13 Correct 202 ms 42556 KB Output is correct
14 Correct 184 ms 40448 KB Output is correct
15 Correct 200 ms 42544 KB Output is correct
16 Correct 220 ms 42932 KB Output is correct
17 Correct 202 ms 42920 KB Output is correct
18 Correct 11 ms 21324 KB Output is correct
19 Correct 174 ms 40548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21324 KB Output is correct
2 Correct 94 ms 55564 KB Output is correct
3 Correct 108 ms 59808 KB Output is correct
4 Correct 112 ms 62400 KB Output is correct
5 Correct 111 ms 62520 KB Output is correct
6 Correct 12 ms 22092 KB Output is correct
7 Correct 13 ms 22172 KB Output is correct
8 Correct 12 ms 22152 KB Output is correct
9 Correct 11 ms 21452 KB Output is correct
10 Correct 11 ms 21524 KB Output is correct
11 Correct 11 ms 21452 KB Output is correct
12 Correct 67 ms 45996 KB Output is correct
13 Correct 106 ms 62436 KB Output is correct
14 Correct 10 ms 21324 KB Output is correct
15 Correct 107 ms 58408 KB Output is correct
16 Correct 107 ms 62400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21368 KB Output is correct
2 Correct 11 ms 21452 KB Output is correct
3 Correct 11 ms 21372 KB Output is correct
4 Runtime error 26 ms 43340 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21368 KB Output is correct
2 Correct 11 ms 21452 KB Output is correct
3 Correct 11 ms 21372 KB Output is correct
4 Runtime error 26 ms 43340 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 133 ms 87596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 133 ms 87596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 21324 KB Output is correct
2 Correct 13 ms 21836 KB Output is correct
3 Correct 13 ms 21840 KB Output is correct
4 Correct 13 ms 21836 KB Output is correct
5 Correct 10 ms 21420 KB Output is correct
6 Correct 11 ms 21452 KB Output is correct
7 Correct 10 ms 21452 KB Output is correct
8 Correct 13 ms 21708 KB Output is correct
9 Correct 12 ms 21880 KB Output is correct
10 Correct 10 ms 21452 KB Output is correct
11 Correct 11 ms 21408 KB Output is correct
12 Correct 117 ms 34048 KB Output is correct
13 Correct 202 ms 42556 KB Output is correct
14 Correct 184 ms 40448 KB Output is correct
15 Correct 200 ms 42544 KB Output is correct
16 Correct 220 ms 42932 KB Output is correct
17 Correct 202 ms 42920 KB Output is correct
18 Correct 11 ms 21324 KB Output is correct
19 Correct 174 ms 40548 KB Output is correct
20 Correct 11 ms 21324 KB Output is correct
21 Correct 94 ms 55564 KB Output is correct
22 Correct 108 ms 59808 KB Output is correct
23 Correct 112 ms 62400 KB Output is correct
24 Correct 111 ms 62520 KB Output is correct
25 Correct 12 ms 22092 KB Output is correct
26 Correct 13 ms 22172 KB Output is correct
27 Correct 12 ms 22152 KB Output is correct
28 Correct 11 ms 21452 KB Output is correct
29 Correct 11 ms 21524 KB Output is correct
30 Correct 11 ms 21452 KB Output is correct
31 Correct 67 ms 45996 KB Output is correct
32 Correct 106 ms 62436 KB Output is correct
33 Correct 10 ms 21324 KB Output is correct
34 Correct 107 ms 58408 KB Output is correct
35 Correct 107 ms 62400 KB Output is correct
36 Correct 11 ms 21368 KB Output is correct
37 Correct 11 ms 21452 KB Output is correct
38 Correct 11 ms 21372 KB Output is correct
39 Runtime error 26 ms 43340 KB Execution killed with signal 6
40 Halted 0 ms 0 KB -