답안 #886779

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
886779 2023-12-12T23:03:41 Z WonderfulWhale 시간이 돈 (balkan11_timeismoney) C++17
80 / 100
2000 ms 1496 KB
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

int n, m;
vector<pair<int, pii>> G[209];
vector<pair<pii, pii>> edges;
vector<int> cur;
vector<int> G2[209];
bool used[10009];

bool vis[209];

int p[209];
int Find(int x) {
	return p[x]==x?x:p[x]=Find(p[x]);
}
void Union(int a, int b) {
	p[Find(a)] = Find(b);
}

void dfs(int x, int y) {
	vis[x] = true;
	for(int z:G2[x])
		if(z!=y) 
			dfs(z, x);
}

bool improve() {
	int sum1=0, sum2=0;
	for(int i=0;i<n;i++) G2[i].clear();
	for(int i=0;i<n-1;i++) {
		auto e = edges[cur[i]];
		G2[e.st.st].pb(e.st.nd);
		G2[e.st.nd].pb(e.st.st);
		sum1 += e.nd.st, sum2 += e.nd.nd;
	}
	for(int i=0;i<n-1;i++) {
		auto e = edges[cur[i]];
		memset(vis, 0, sizeof(vis));
		dfs(e.st.st, e.st.nd);
		for(int j=0;j<m;j++) {
			if(!used[j]) {
				auto ne = edges[j];
				if(vis[ne.st.st]^vis[ne.st.nd]) {
					int nsum1 = sum1-e.nd.st+ne.nd.st;
					int nsum2 = sum2-e.nd.nd+ne.nd.nd;
					if(nsum1*nsum2<sum1*sum2) {
						used[cur[i]] = false;
						cur[i] = j;
						used[j] = true;
						return true;
					}
				}
			}
		}
	}
	return false;
}
	
int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	srand(694202137);

	cin >> n >> m;
	iota(p, p+n, 0);
	for(int i=0;i<m;i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		G[a].pb({b, {c, d}});
		G[b].pb({a, {c, d}});
		edges.pb({{a, b}, {c, d}});
	}
	random_shuffle(all(edges));
	for(int i=0;i<m;i++) {
		auto [x, y] = edges[i];
		if(Find(x.st)!=Find(x.nd)) {
			used[i] = true;
			cur.pb(i);
			Union(x.st, x.nd);
		} 
	}
	while(improve()) {}
	int sum1=0, sum2=0;
	vector<pii> ans;
	for(int x:cur) {
		sum1 += edges[x].nd.st;
		sum2 += edges[x].nd.nd;
		ans.pb(edges[x].st);
	}
	cout << sum1 << " " << sum2 << "\n";
	for(pii x:ans) 
		cout << x.st << " " << x.nd << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 12 ms 348 KB Output is correct
6 Correct 17 ms 348 KB Output is correct
7 Correct 335 ms 692 KB Output is correct
8 Correct 1718 ms 1496 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 16 ms 532 KB Output is correct
15 Correct 17 ms 348 KB Output is correct
16 Correct 503 ms 688 KB Output is correct
17 Incorrect 534 ms 604 KB Output isn't correct
18 Incorrect 457 ms 688 KB Output isn't correct
19 Execution timed out 2040 ms 1492 KB Time limit exceeded
20 Execution timed out 2043 ms 1496 KB Time limit exceeded