Submission #973865

# Submission time Handle Problem Language Result Execution time Memory
973865 2024-05-02T12:04:57 Z dubabuba Duathlon (APIO18_duathlon) C++14
0 / 100
1000 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair

const int mxn = 2e5 + 10;
vector<int> adj[mxn];
vector<pii> edges;

i64 kv_sum[mxn]; int col[mxn];
i64 sum_kv[mxn]; int chi[mxn];
int n, m;

int colour(int u) {
	if(col[u] < 0) return u;
	return col[u] = colour(col[u]);
}

bool unite(int u, int v) {
	u = colour(u);
	v = colour(v);
	if(u == v) return 0;

	if(col[u] > col[v]) swap(u, v);
	col[u] += col[v];
	col[v] = u;
	return 1;
}

bool vis[mxn];
void dfs_build(int u, int par) {
	vis[u] = 1;
	for(int v : adj[u]) {
		if(v == par) continue;
		dfs_build(v, u);
		chi[u] += chi[v];
		kv_sum[u] += chi[v] * chi[v];
		sum_kv[u] += chi[v];
	}
	sum_kv[u] *= sum_kv[u];
	chi[u]++;
}

i64 ans = 0;
void dfs_solve(int u, int par) {
	vis[u] = 1;
	for(int v : adj[u]) {
		if(v == par) continue;
		dfs_solve(v, u);
	}

	i64 t = sum_kv[u] * sum_kv[u] - kv_sum[u] * kv_sum[u];
	ans += t;

	i64 total = -col[colour(u)];
	i64 s = total - chi[u];
	i64 f = chi[u] - 1;
	ans += 2 * s * f;

	// cout << u << ": " << t << ", " << s << ' ' << f << endl;
}

int main() {
	cin >> n >> m;
	memset(col, -1, sizeof col);
	for(int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		unite(u, v);
	}

	for(int u = 1; u <= n; u++)
		if(!vis[u])
			dfs_build(u, u);
	memset(vis, 0, sizeof vis);
	for(int u = 1; u <= n; u++)
		if(!vis[u])
			dfs_solve(u, u);
	cout << ans << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 800 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 800 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1128 ms 923496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 66 ms 14316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 65 ms 14492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 800 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 800 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -