Submission #745135

#TimeUsernameProblemLanguageResultExecution timeMemory
745135b00norp철인 이종 경기 (APIO18_duathlon)C++14
66 / 100
282 ms36004 KiB
#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]; // graph
vector<array<int, 2> > bcc[N]; // bridge tree graph (to, connector)
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 subtree[N];
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)
{
	subtree[node] = siz[node];
	int ans = siz[node];
	vis[node] = true;
	for(auto [to, temp]: bcc[node])
	{
		if(vis[to]) continue;
		ans += FindSiz(to);
		subtree[node] += subtree[to];
	}
	return ans;
}

void UpdateSiz(int node, int val)
{
	tot_siz[node] = val;
	for(auto [to, temp]: 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], 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

	// implementing case 3:
	// cout << "subtree\n";
	// for(int i = 1; i <= n; i++)
	// {
	// 	cout << subtree[i] << " ";
	// }
	// cout << "\n";
	for(int i = 1; i <= cnt; i++)
	{
		// cout << "tot_siz[i] = " << tot_siz[i] << ", siz[i] = " << siz[i] << "\n";
		// cout << "subtree[i] = " << subtree[i] << endl;
		map<int, int> mp;
		for(auto [to, connector]: bcc[i])
		{
			// cout << "to = " << to << ", connector = " << connector << "\n";
			mp[connector] += subtree[to];
			if(subtree[to] > subtree[i])
			{
				mp[connector] -= subtree[to];
				mp[connector] += (tot_siz[i] - subtree[i]);
			}
		}
		// for(auto i: mp)
		// {
		// 	cout << "connector: " << i.first << ", connector size = " << i.second << "\n";
		// }
		for(auto [to, connector]: bcc[i])
		{
			int going = subtree[to];
			if(subtree[to] > subtree[i])
			{
				going = tot_siz[i] - subtree[i];
			}
			int excluding_this = tot_siz[i] - siz[i] - mp[connector];
			// cout << "excluding_this = " << excluding_this << "\n";
			int cur_ans = 0;
			// cout << "siz[to] = " << going << ", left = " << mp[connector] - subtree[to] << "\n";
			// only one node usable
			cur_ans += (going * (mp[connector] - going));
			// all usable
			cur_ans += (going * siz[i] * excluding_this);
			ans += cur_ans;
			// cout << "ans = " << ans << "\n";
		}
	}
	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 (stderr)

count_triplets.cpp: In function 'long long int FindSiz(long long int)':
count_triplets.cpp:87:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   87 |  for(auto [to, temp]: bcc[node])
      |           ^
count_triplets.cpp: In function 'void UpdateSiz(long long int, long long int)':
count_triplets.cpp:99:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   99 |  for(auto [to, temp]: bcc[node])
      |           ^
count_triplets.cpp: In function 'void Solve()':
count_triplets.cpp:139:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  139 |  for(auto [u, v]: bridges)
      |           ^
count_triplets.cpp:183:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  183 |   for(auto [to, connector]: bcc[i])
      |            ^
count_triplets.cpp:197:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  197 |   for(auto [to, connector]: bcc[i])
      |            ^
#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...