Submission #440856

#TimeUsernameProblemLanguageResultExecution timeMemory
440856kekw_orzDuathlon (APIO18_duathlon)C++14
23 / 100
1134 ms1048580 KiB
// ¯\_(ツ)_/¯
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)                      (x).begin(),(x).end()
#define X                           first
#define Y                           second
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

vector<int> adj[MAXN];
bool vis[MAXN];
ll sub_g[MAXN], n, m, ans, tot;

void pre_dfs(int v, int p) {
	if (p == 0) tot = 0;
	tot++;
	for (int u : adj[v])
		if (u != p)
			pre_dfs(u, v);
}

void dfs(int v, int p) {
	vis[v] = true;
	sub_g[v] = 1;

	ans += (tot - 1) * (tot - 2);
	for (int u : adj[v]) {
		if (u == p) continue;
		
		dfs(u, v);
		sub_g[v] += sub_g[u];
		ans -= sub_g[u] * (sub_g[u] - 1);
	}

	ll t = tot - sub_g[v];
	ans -= t * (t - 1);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	for (int i = 1; i <= n; i++)
		if (!vis[i])
			pre_dfs(i, 0), dfs(i, 0);

	cout << ans << endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...