Submission #370786

# Submission time Handle Problem Language Result Execution time Memory
370786 2021-02-24T15:35:30 Z luciocf Duathlon (APIO18_duathlon) C++14
31 / 100
156 ms 41376 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 3e5+10;

int n;
int in[maxn], low[maxn], id[maxn], tt, cc;

bool is[maxn];

int sz[maxn], sub[maxn];
int comp_tree[maxn], sz_comp[maxn];

bool solved[maxn];

vector<int> grafo[maxn], tree[maxn], stk;

vector<vector<int>> comp;

void dfs(int u, int p)
{
	in[u] = low[u] = ++tt;
	stk.push_back(u);

	for (auto v: grafo[u])
	{
		if (v == p) continue;

		if (in[v]) low[u] = min(low[u], in[v]);
		else
		{
			dfs(v, u);

			low[u] = min(low[u], low[v]);

			if (low[v] >= in[u])
			{
				is[u] = (in[u] > 1 || in[v] > 2);

				comp.push_back({u});

				while (stk.back() != u)
				{
					comp.back().push_back(stk.back());
					stk.pop_back();
				}
			}
		}
	}
}

void build(void)
{
	for (int i = 1; i <= n; i++)
	{
		if (is[i])
		{
			id[i] = ++cc;
			sz[cc]++;
		}
	}


	for (auto &C: comp)
	{	
		cc++;

		for (auto u: C)
		{
			if (!is[u])
			{
				id[u] = cc;
				sz[cc]++;
			}
			else
			{
				tree[id[u]].push_back(cc);
				tree[cc].push_back(id[u]);
			}
		}
	}
}

ll ans;

ll choose2(int n)
{
	return 1ll*n*(n-1)/2ll;
}

ll choose3(int n)
{
	return 1ll*n*(n-1)*(n-2)/6ll;
}

void calc(int u, int p, int cc)
{
	comp_tree[u] = cc;
	sz_comp[cc] += sz[u];

	sub[u] = sz[u];

	for (auto v: tree[u])
	{
		if (v == p) continue;

		calc(v, u, cc);

		sub[u] += sub[v];
	}
}

void solve(int u, int p)
{
	solved[comp_tree[u]] = 1;

	if (sz[u] >= 3) ans += 6ll*choose3(sz[u]);
	if (sz[u] >= 2) ans += 4ll*choose2(sz[u])*(sub[u]-sz[u] + sz_comp[comp_tree[u]]-sub[u]);

	ans += 2ll*sz[u]*(sz_comp[comp_tree[u]]-sub[u])*(sub[u]-sz[u]);

	int soma = 0;

	for (auto v: tree[u])
	{
		if (v == p) continue;

		ans += 2ll*sz[u]*soma*sub[v];
		soma += sub[v];

		ans += 2ll*choose2(sz[v])*sz[u];

		solve(v, u);
	}
}

int main(void)
{
	int m;
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back(v);
		grafo[v].push_back(u);
	}

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

	build();

	int cc_tree = 0;

	for (int i = 1; i <= 2*n; i++)
		if (!comp_tree[i])
			calc(i, 0, ++cc_tree);

	for (int i = 1; i <= 2*n; i++)
		if (!solved[comp_tree[i]])
			solve(i, 0);

	printf("%lld\n", ans);
}

Compilation message

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:143:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  143 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  148 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 9 ms 14444 KB Output is correct
3 Correct 9 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 11 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Incorrect 9 ms 14572 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 9 ms 14444 KB Output is correct
3 Correct 9 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 11 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Incorrect 9 ms 14572 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 105 ms 33116 KB Output is correct
2 Correct 94 ms 33264 KB Output is correct
3 Correct 110 ms 33824 KB Output is correct
4 Correct 96 ms 32932 KB Output is correct
5 Correct 107 ms 29732 KB Output is correct
6 Correct 119 ms 33056 KB Output is correct
7 Correct 144 ms 31476 KB Output is correct
8 Correct 139 ms 31896 KB Output is correct
9 Correct 106 ms 29984 KB Output is correct
10 Correct 107 ms 29472 KB Output is correct
11 Correct 92 ms 27168 KB Output is correct
12 Correct 99 ms 26912 KB Output is correct
13 Correct 83 ms 26912 KB Output is correct
14 Correct 80 ms 26656 KB Output is correct
15 Correct 63 ms 25760 KB Output is correct
16 Correct 64 ms 25524 KB Output is correct
17 Correct 16 ms 18788 KB Output is correct
18 Correct 15 ms 18788 KB Output is correct
19 Correct 15 ms 18788 KB Output is correct
20 Correct 16 ms 18788 KB Output is correct
21 Correct 16 ms 18660 KB Output is correct
22 Correct 16 ms 18660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14572 KB Output is correct
2 Correct 10 ms 14572 KB Output is correct
3 Correct 10 ms 14700 KB Output is correct
4 Correct 10 ms 14700 KB Output is correct
5 Correct 9 ms 14700 KB Output is correct
6 Correct 12 ms 14700 KB Output is correct
7 Correct 10 ms 14700 KB Output is correct
8 Correct 10 ms 14700 KB Output is correct
9 Correct 10 ms 14700 KB Output is correct
10 Correct 9 ms 14572 KB Output is correct
11 Correct 9 ms 14572 KB Output is correct
12 Correct 10 ms 14572 KB Output is correct
13 Correct 10 ms 14572 KB Output is correct
14 Correct 12 ms 14572 KB Output is correct
15 Correct 9 ms 14572 KB Output is correct
16 Correct 9 ms 14572 KB Output is correct
17 Correct 9 ms 14572 KB Output is correct
18 Correct 10 ms 14572 KB Output is correct
19 Correct 9 ms 14572 KB Output is correct
20 Correct 9 ms 14572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 31644 KB Output is correct
2 Correct 122 ms 31772 KB Output is correct
3 Correct 134 ms 31812 KB Output is correct
4 Correct 126 ms 31636 KB Output is correct
5 Correct 139 ms 31672 KB Output is correct
6 Correct 156 ms 41376 KB Output is correct
7 Correct 141 ms 38044 KB Output is correct
8 Correct 139 ms 37404 KB Output is correct
9 Correct 146 ms 35612 KB Output is correct
10 Correct 123 ms 31644 KB Output is correct
11 Correct 121 ms 31644 KB Output is correct
12 Correct 120 ms 31644 KB Output is correct
13 Correct 127 ms 31668 KB Output is correct
14 Correct 114 ms 30748 KB Output is correct
15 Correct 102 ms 29856 KB Output is correct
16 Correct 63 ms 26400 KB Output is correct
17 Correct 78 ms 30552 KB Output is correct
18 Correct 90 ms 30612 KB Output is correct
19 Correct 81 ms 30736 KB Output is correct
20 Correct 82 ms 30620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14572 KB Output is correct
2 Correct 10 ms 14572 KB Output is correct
3 Incorrect 10 ms 14572 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 125 ms 31644 KB Output is correct
2 Correct 127 ms 31900 KB Output is correct
3 Incorrect 153 ms 30236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 9 ms 14444 KB Output is correct
3 Correct 9 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 11 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Incorrect 9 ms 14572 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 9 ms 14444 KB Output is correct
3 Correct 9 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 11 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Incorrect 9 ms 14572 KB Output isn't correct
8 Halted 0 ms 0 KB -