Submission #745037

# Submission time Handle Problem Language Result Execution time Memory
745037 2023-05-19T10:30:01 Z b00norp Duathlon (APIO18_duathlon) C++14
0 / 100
1000 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e18, N = 1e5 + 5;
vector<int> g[N];
int siz;
bool vis[N];
int subtree[N];
int ans = 0;

void dfsini(int node)
{
	vis[node] = true;
	subtree[node] = 1;
	siz++;
	for(int to: g[node])
	{
		if(vis[to]) continue;
		dfsini(to);
		subtree[node] += subtree[to];
	}
}

void dfs_calc(int node, int par = -1)
{
	// considering this is c

	// s is outside the subtree of node

	ans += (siz - subtree[node]) * (subtree[node] - 1);

	// f is outside the subtree of node

	ans += (subtree[node] - 1) * (siz - subtree[node]);

	for(int to: g[node])
	{
		if(to == par) continue;
		dfs_calc(to, node);
		// s is in subtree of this node
		ans += (subtree[to]) * (subtree[node] - subtree[to] - 1);
		// f is in subtree of this node
		ans += (subtree[node] - subtree[to] - 1) * (subtree[to]);
	}
}

void Solve() 
{
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= m; i++)
	{
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i = 1; i <= n; i++)
	{
		if(!vis[i])
		{
			siz = 0;
			dfsini(i);
			dfs_calc(i);
		}
	}
	cout << ans << "\n";
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    //cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 542 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 542 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 478492 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 50 ms 8480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 8528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 542 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 542 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -