Submission #405402

# Submission time Handle Problem Language Result Execution time Memory
405402 2021-05-16T10:52:01 Z aryan12 Duathlon (APIO18_duathlon) C++17
23 / 100
1000 ms 1048580 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 N = 1e5 + 5;
vector<int> g[N];
bool vis[N];
int subtree[N], ans = 0;
int n, m, cnt;

void dfs(int node, int par) {
	subtree[node] = 1;
	vis[node] = true;
	cnt++;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] != par) {
			dfs(g[node][i], node);
			subtree[node] += subtree[g[node][i]];
		}
	}
}

void dfs2(int node, int par) {
	//cout << "node = " << node << ", cnt = " << cnt << "\n";
	int total = subtree[node] - 1, left = cnt - 1;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] != par) {
			left -= subtree[g[node][i]];
			ans += (subtree[g[node][i]] * left);
		}
	}
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] != par) {
			dfs2(g[node][i], node);
		}
	}
}

void Solve() {
	cin >> n >> m;
	for(int i = 1; i <= m; i++) {
		int v, u;
		cin >> v >> u;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	for(int i = 1; i <= n; i++) {
		if(!vis[i]) {
			cnt = 0;
			dfs(i, -1);
			dfs2(i, -1);
		}
	}
	cout << ans * 2 << "\n";
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
}

/*
8 7
1 2
2 3
3 4
2 5
5 8
1 6
6 7

*/

Compilation message

count_triplets.cpp: In function 'void dfs(long long int, long long int)':
count_triplets.cpp:17:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(long long int, long long int)':
count_triplets.cpp:28:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
count_triplets.cpp:34:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
count_triplets.cpp:27:6: warning: unused variable 'total' [-Wunused-variable]
   27 |  int total = subtree[node] - 1, left = cnt - 1;
      |      ^~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 562 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 562 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1096 ms 355512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 3 ms 2636 KB Output is correct
8 Correct 4 ms 2636 KB Output is correct
9 Correct 3 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 3 ms 2680 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2680 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 3 ms 2636 KB Output is correct
16 Correct 3 ms 2636 KB Output is correct
17 Correct 3 ms 2636 KB Output is correct
18 Correct 2 ms 2684 KB Output is correct
19 Correct 3 ms 2676 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 7236 KB Output is correct
2 Correct 90 ms 7252 KB Output is correct
3 Correct 55 ms 7148 KB Output is correct
4 Correct 54 ms 7236 KB Output is correct
5 Correct 61 ms 7272 KB Output is correct
6 Correct 67 ms 9156 KB Output is correct
7 Correct 82 ms 8888 KB Output is correct
8 Correct 63 ms 8388 KB Output is correct
9 Correct 60 ms 7952 KB Output is correct
10 Correct 60 ms 7272 KB Output is correct
11 Correct 61 ms 8404 KB Output is correct
12 Correct 60 ms 8480 KB Output is correct
13 Correct 64 ms 8520 KB Output is correct
14 Correct 62 ms 8064 KB Output is correct
15 Correct 61 ms 7724 KB Output is correct
16 Correct 27 ms 6540 KB Output is correct
17 Correct 39 ms 8676 KB Output is correct
18 Correct 39 ms 8672 KB Output is correct
19 Correct 39 ms 8680 KB Output is correct
20 Correct 43 ms 8676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Runtime error 753 ms 1048580 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 66 ms 7316 KB Output is correct
2 Correct 55 ms 7116 KB Output is correct
3 Runtime error 946 ms 1048580 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 562 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 562 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -