Submission #516966

#TimeUsernameProblemLanguageResultExecution timeMemory
516966pragmatistDuathlon (APIO18_duathlon)C++17
10 / 100
1093 ms11984 KiB
#include <bits/stdc++.h>                             

#define sz(v) v.size()                                
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
 
using namespace std;

typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;

const int N = (int)3e5 + 7;
const int M = (int)15e6 + 7;
const ll MOD = (ll)1e9 + 7;                    
const int inf = (ll)1e9 + 7;
const ll INF = (ll)3e18 + 7;

pii dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

int n, m, s, c, f, cur, d[N];
bool used[N], is;
vector<int> g[N];

bool dfs(int v, bool was) {
	used[v] = 1;
	if(v == c) was = 1;
	if(was && v == f) return 1;
	bool ok = 0;
	for(auto to : g[v]) {
		if(!used[to]) {
			ok |= dfs(to, was);
			if(ok) break;			
		}	
	}
	used[v] = 0;
	return ok;
}

void DFS(int v, int pr) {
	used[v] = 1;
	cur++;
	for(auto to : g[v]) {
		if(to == pr) continue;
		if(used[to]) is = 1;
		else {
			DFS(to, v);
		}
	}
}

void solve() {                              
	cin >> n >> m;
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	int ans = 0;
	bool ok = 1;
		for(s = 1; s <= n; ++s) {
			queue<int> q;
			q.push(s);
		    fill(d + 1, d + 1 + n, inf);
			d[s] = 0;
			while(!q.empty()) {
				int v = q.front();
				q.pop();
				for(auto to : g[v]) {
					if(d[v] + 1 < d[to]) {
						d[to] = d[v] + 1;						
						q.push(to);
					}
				}
			}
			int cur = 0;
			for(int i = 1; i <= n; ++i) {
				if(d[i] == inf || i == s) continue;
				cur += d[i] - 1; 	
			}      
			ans += cur;
	}         
	cout << ans << nl;	
}

signed main() {                   
	ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
    int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; ++i) {
        //cout << "Case " << i << ": ";
        solve();
    }
    return 0;
}
/*
4 3
1 2
2 3
3 4
*/

Compilation message (stderr)

count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:66:7: warning: unused variable 'ok' [-Wunused-variable]
   66 |  bool ok = 1;
      |       ^~
#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...