Submission #203563

#TimeUsernameProblemLanguageResultExecution timeMemory
203563Kevin_Zhang_TWDuathlon (APIO18_duathlon)C++17
31 / 100
176 ms54716 KiB
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010;
int n, m;
vector<int> edge[maxn];
int low[maxn], st[maxn], stl, dep[maxn];
int cnt_b;
ll gsz;
vector<int> c[maxn];
bool is_ap[maxn];
ll res;
int h;
vector<int> tree_edge[maxn];
int sz[maxn];
void dfs2(int now, int last = 0){
	sz[now] = c[now].size() + (now <= n);
	for (int u : tree_edge[now]) if(u != last) {
		dfs2(u, now);
		sz[now] += sz[u] - 1;
	}
	ll o = res;
	if (now > n){
		ll apc = 0, cur = c[now].size();
		//cout << "now ";
		for (int u : c[now]) {
			apc += is_ap[u];
			//cout << u << ' ';
		}
		//cout << '\n';
		res += (cur-apc) * (gsz - cur + apc) * (cur-apc-1) * 2;
		res += (cur-apc) * (cur-apc-1) * (cur-2);
		for (int u : tree_edge[now]) if(u != last)
			res += sz[u] * (gsz - sz[u] - cur + apc) * (cur-2);
		if (last)
			res += (gsz - sz[now]+1) * (sz[now] - cur +apc-1) * (cur-2);
	}
	else {
		//cout << "now " << now << '\n';
		auto add = [&](ll a, ll all){
			//cout << "ad " << a << '\n';
			res += (a-1) * (all-a);
		};
		for (int u : tree_edge[now]) if(u != last)
			add(sz[u], gsz);
		if (last)
			add(gsz - sz[now]+1, gsz);
		assert(last);
	}
//	cout << "dif + " << res-o << '\n';
//	cout << '\n';
}
void cal(){
	for (int i = h;i < cnt_b;++i){
		for (int u : c[i]) if (is_ap[u]) {
			tree_edge[u].pb(i);
			tree_edge[i].pb(u);
		}
	}
	dfs2(h);
	gsz = 0, h = cnt_b;
}
void dfs(int now, int last = 0){
	static int d;
	++gsz;
	low[now] = dep[now] = ++d;
	st[stl++] = now;
	int cnt_c = 0;
	for (int u : edge[now]) if (u != last) {
		if (!low[u]){
			dfs(u, now);
			++cnt_c;
			low[now] = min(low[now], low[u]);
			if (low[u] >= dep[now]){
				do {
					c[cnt_b].pb( st[--stl] );
				} while (st[stl] != u);
				c[cnt_b++].pb(now);
				is_ap[now] = true;
			}
		}
		else 
			low[now] = min(low[now], dep[u]);
	}
	if (!last && cnt_c < 2)
		is_ap[now] = false;
	--d;
	if (last == 0)
		cal();
}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	for ( int a, b, i = 0 ; i < m ; ++i) {
		cin >> a >> b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	h = cnt_b = n+1;
	for (int i = 1;i <= n;++i) if (!low[i])
		dfs(i);
	cout << res << '\n';
}

Compilation message (stderr)

count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:23:5: warning: unused variable 'o' [-Wunused-variable]
  ll o = res;
     ^
#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...