Submission #745113

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

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

/*
- Make a bridge tree of the graph

- Store all the sizes of the nodes in bridge tree

- Case 1: 

All 3 nodes in the same node (all s, c, f)
(siz) * (siz - 1) * (siz - 2)

- Case 2: 2 nodes in the same node. 1 node away (s, c) or (c, f)
s / f cannot be the node that joins the bridge node and the away node
=> -1 candidate for f, same candidates for c
=> [[(component_siz - siz) * (siz - 1) * (siz - 1)]] * 2 (for the 2 options)

- Case 3: 1 node here, 2 nodes in different subtrees (c)
=> if bridge is from same node, only that node can be c
=> else, siz options

*/

const int INF = 1e18;

const int N = 1e5 + 5;
bool vis[N]; // visited track
vector<int> g[N], bcc[N]; // graph, bridge tree graph
int low[N], disc[N], tim = 1;
set<pair<int, int> > bridges;
int siz[N], component[N]; // size of bridge tree node i, component of node i of original graph
int tot_siz[N]; // total size of the bridge tree component
int ans = 0;

void dfs(int node, int par)
{
	low[node] = tim;
	disc[node] = tim++;
	vis[node] = true;
	for(int to: g[node])
	{
		if(to == par) continue;
		if(vis[to])
		{
			low[node] = min(low[node], disc[to]);
		}
		else
		{
			dfs(to, node);
			low[node] = min(low[node], low[to]);
			if(low[to] > disc[node])
			{
				bridges.insert({node, to});
				bridges.insert({to, node});
			}
		}
	}
}

void add_component(int node, int par, int comp)
{
	siz[comp] += 1;
	component[node] = comp;
	vis[node] = true;
	for(int to: g[node])
	{
		if(vis[to]) continue;
		if(bridges.count({node, to}))
		{
			continue;
		}
		add_component(to, node, comp);
	}
}

int FindSiz(int node)
{
	int ans = siz[node];
	vis[node] = true;
	for(int to: bcc[node])
	{
		if(vis[to]) continue;
		ans += FindSiz(to);
	}
	return ans;
}

void UpdateSiz(int node, int val)
{
	tot_siz[node] = val;
	for(int to: bcc[node])
	{
		if(tot_siz[to] != val)
		{
			UpdateSiz(to, val);
		}
	}
}

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])
		{
			dfs(i, -1);
		}
	}
	for(int i = 1; i <= n; i++)
	{
		vis[i] = false;
	}
	int cnt = 0;
	for(int i = 1; i <= n; i++)
	{
		if(!vis[i])
		{
			cnt += 1;
			add_component(i, -1, cnt);
		}
	}
	for(auto [u, v]: bridges)
	{
		bcc[component[u]].push_back(component[v]);
		bcc[component[v]].push_back(component[u]);
	}

	// implementing case 1:
	for(int i = 1; i <= cnt; i++)
	{
		ans += siz[i] * (siz[i] - 1) * (siz[i] - 2);
	}
	// implementation ends

	for(int i = 1; i <= cnt; i++)
	{
		vis[i] = false;
	}
	for(int i = 1; i <= cnt; i++)
	{
		if(!vis[i])
		{
			int temp = FindSiz(i);
			UpdateSiz(i, temp);
		}
	}

	// implementing case 2:
	for(int i = 1; i <= cnt; i++)
	{
		ans += (tot_siz[i] - siz[i]) * (siz[i] - 1) * (siz[i] - 1) * 2LL;
	}
	// implementation ends
    // case 3 to be implemented
	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;
}

Compilation message

count_triplets.cpp: In function 'void Solve()':
count_triplets.cpp:135:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  135 |  for(auto [u, v]: bridges)
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5036 KB Output is correct
6 Correct 4 ms 5044 KB Output is correct
7 Incorrect 3 ms 5032 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5036 KB Output is correct
6 Correct 4 ms 5044 KB Output is correct
7 Incorrect 3 ms 5032 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 21596 KB Output is correct
2 Correct 70 ms 22752 KB Output is correct
3 Incorrect 163 ms 26956 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 289 ms 30708 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 252 ms 30780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5036 KB Output is correct
6 Correct 4 ms 5044 KB Output is correct
7 Incorrect 3 ms 5032 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5036 KB Output is correct
6 Correct 4 ms 5044 KB Output is correct
7 Incorrect 3 ms 5032 KB Output isn't correct
8 Halted 0 ms 0 KB -