답안 #796236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796236 2023-07-28T08:15:55 Z 박상훈(#10069) Security Guard (JOI23_guard) C++17
26 / 100
233 ms 83120 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr ll INF2 = 4e18;

ll ans;

vector<int> adj[200200], G[200200];
ll a[200200];
int par[200200], deg[200200];

struct DSU{
	int path[200200], mn[200200];
	void init(int n){for (int i=1;i<=n;i++) path[i] = i, mn[i] = a[i];}
	int find(int s){
		if (s==path[s]) return s;
		return path[s] = find(path[s]);
	}
	int merge(int s, int v){
		s = find(s), v = find(v);
		if (s==v) return 0;

		mn[s] = std::min(mn[s], mn[v]);
		path[v] = s;
		return 1;
	}
	int min(int s){return mn[find(s)];}
}dsu;


void dfs(int s, int pa){
	par[s] = pa;
	ans += a[pa];
	for (auto &v:G[s]) if (v!=pa) dfs(v, s);
}

vector<ll> naive(int n, int q, vector<pair<int, int>> E, ll ofs){
	vector<ll> ret(n, INF2);
	for (int z=0;z<(1<<(n-1));z++){
		dsu.init(n);

		ll val = 0;
		vector<ll> V;

		for (int i=0;i<n-1;i++) if (z&(1<<i)){
			dsu.merge(E[i].first, E[i].second);
			val += a[E[i].first] + a[E[i].second];
		}

		for (int i=1;i<=n;i++) if (dsu.find(i)==i){
			V.push_back(dsu.min(i));
			val += V.back();
		}

		sort(V.begin(), V.end());
		val += V[0] * (int)(V.size()-2);

		ret[(int)V.size()-1] = min(ret[(int)V.size()-1], val + ofs);
	}

	if ((int)ret.size() > q+1) ret.resize(q+1);
	while((int)ret.size() < q+1) ret.push_back(ret.back());

	return ret;
}

namespace N2{

vector<int> adj[3030];
vector<ll> dp[2][3030], nxt[2];

void merge(int s, int v){
	nxt[0].clear();
	nxt[1].clear();

	nxt[0].resize(dp[0][s].size() + dp[0][v].size(), INF2);
	nxt[1].resize(dp[0][s].size() + dp[0][v].size(), INF2);

	for (int i=0;i<(int)dp[0][s].size();i++){
		for (int j=0;j<(int)dp[0][v].size();j++){
			// connect
			nxt[0][i+j] = min(nxt[0][i+j], dp[0][s][i] + dp[0][v][j] + a[s] + a[v]);
			nxt[1][i+j] = min(nxt[1][i+j], dp[0][s][i] + dp[1][v][j] + a[s] + a[v]);
			nxt[1][i+j] = min(nxt[1][i+j], dp[1][s][i] + dp[0][v][j] + a[s] + a[v]);

			// disconnect
			nxt[0][i+j+1] = min(nxt[0][i+j+1], dp[0][s][i] + dp[1][v][j]);
			nxt[1][i+j+1] = min(nxt[1][i+j+1], dp[1][s][i] + dp[1][v][j]);
		}
	}

	swap(nxt[0], dp[0][s]);
	swap(nxt[1], dp[1][s]);
}

void dfs(int s, int pa = 0){
	dp[0][s].push_back(0);
	dp[1][s].push_back(a[s]);

	for (auto &v:adj[s]) if (v!=pa){
		dfs(v, s);
		merge(s, v);
	}
}

vector<ll> solve(int n, int q, vector<pair<int, int>> E, ll ofs){
	for (auto &[x, y]:E){
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	dfs(1);

	auto ret = dp[1][1];
	assert((int)ret.size()==n);
	for (int i=0;i<n;i++){
		ret[i] += ofs;
		ret[i] += (*min_element(a+1, a+n+1)) * (i-1);
	}

	if ((int)ret.size() > q+1) ret.resize(q+1);
	while((int)ret.size() < q+1) ret.push_back(ret.back());

	return ret;
}

} // namespace N2;

int main(){
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);

	for (int i=1;i<=n;i++) scanf("%lld", a+i);
	int root = max_element(a+1, a+n+1) - a;
	
	for (int i=1;i<=m;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	vector<array<ll, 3>> E;

	dsu.init(n);

	for (int i=1;i<=n;i++){
		for (auto &v:adj[i]) if (dsu.find(i) != dsu.find(v)){
			E.push_back({a[i] + a[v], i, v});
		}
	}

	sort(E.begin(), E.end());

	vector<pair<int, int>> Edge;
	for (auto &[z, x, y]:E) if (dsu.merge(x, y)){
		G[x].push_back(y);
		G[y].push_back(x);

		Edge.emplace_back(x, y);
	}

	ans = a[root];
	for (int i=1;i<=n;i++) ans -= a[i];

	auto rans = N2::solve(n, q, Edge, ans);
	for (auto &x:rans) printf("%lld\n", x);

	// for (auto &[v, w]:Edge) ans += a[v] + a[w];

	// printf("%lld\n", ans);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:135:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |  for (int i=1;i<=n;i++) scanf("%lld", a+i);
      |                         ~~~~~^~~~~~~~~~~~~
guard.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Runtime error 140 ms 77232 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Runtime error 140 ms 77232 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Runtime error 140 ms 77232 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Runtime error 140 ms 77232 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9828 KB Output is correct
2 Correct 4 ms 9940 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 4 ms 9940 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 4 ms 9940 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 23 ms 13756 KB Output is correct
9 Correct 23 ms 13764 KB Output is correct
10 Correct 23 ms 13636 KB Output is correct
11 Correct 24 ms 13828 KB Output is correct
12 Correct 23 ms 13780 KB Output is correct
13 Correct 23 ms 13764 KB Output is correct
14 Correct 24 ms 13836 KB Output is correct
15 Correct 23 ms 13764 KB Output is correct
16 Correct 23 ms 13776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9828 KB Output is correct
2 Correct 4 ms 9940 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 4 ms 9940 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 4 ms 9940 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 23 ms 13756 KB Output is correct
9 Correct 23 ms 13764 KB Output is correct
10 Correct 23 ms 13636 KB Output is correct
11 Correct 24 ms 13828 KB Output is correct
12 Correct 23 ms 13780 KB Output is correct
13 Correct 23 ms 13764 KB Output is correct
14 Correct 24 ms 13836 KB Output is correct
15 Correct 23 ms 13764 KB Output is correct
16 Correct 23 ms 13776 KB Output is correct
17 Correct 75 ms 50968 KB Output is correct
18 Correct 70 ms 36112 KB Output is correct
19 Correct 87 ms 25360 KB Output is correct
20 Correct 125 ms 29060 KB Output is correct
21 Correct 147 ms 31248 KB Output is correct
22 Correct 143 ms 51244 KB Output is correct
23 Correct 232 ms 41304 KB Output is correct
24 Correct 161 ms 49144 KB Output is correct
25 Correct 53 ms 15408 KB Output is correct
26 Correct 113 ms 26232 KB Output is correct
27 Correct 63 ms 17280 KB Output is correct
28 Correct 60 ms 31228 KB Output is correct
29 Correct 70 ms 36988 KB Output is correct
30 Correct 61 ms 28868 KB Output is correct
31 Correct 77 ms 83120 KB Output is correct
32 Correct 84 ms 73788 KB Output is correct
33 Correct 55 ms 16932 KB Output is correct
34 Correct 57 ms 16844 KB Output is correct
35 Correct 96 ms 24376 KB Output is correct
36 Correct 233 ms 42084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Runtime error 140 ms 77232 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -