Submission #405376

#TimeUsernameProblemLanguageResultExecution timeMemory
405376aryan12Duathlon (APIO18_duathlon)C++17
0 / 100
1108 ms1048580 KiB
#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];
int subtree[N], ans = 0;

void dfs(int node, int par) {
	subtree[node] = 1;
	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]];
		}
	}
	int total = subtree[node];
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i] != par) {
			ans += ((subtree[g[node][i]] * (total - subtree[g[node][i]]))) / 2;
		}
	}
	ans += subtree[node] - 1;
}

void Solve() {
	int n, m;
	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);
	}
	dfs(1, -1);
	cout << ans << "\n";
}

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

Compilation message (stderr)

count_triplets.cpp: In function 'void dfs(long long int, long long int)':
count_triplets.cpp:13: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]
   13 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
count_triplets.cpp:20: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]
   20 |  for(int i = 0; i < g[node].size(); 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...