Submission #17958

#TimeUsernameProblemLanguageResultExecution timeMemory
17958AdilkhanShymbulak (IZhO14_shymbulak)C++98
26 / 100
1500 ms35144 KiB
#include <bits/stdc++.h>

#define pb push_back
#define endl "\n"
#define mp make_pair 
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define fname ""
#define sz(x) (int)(x.size())

typedef long long ll;

using namespace std;

const ll N = (ll)(5e5) + 322;
const ll INF = (ll)(1e9);
const ll mod = (ll)(1e9) + 7;
const double eps = 1e-9;

ll d[N][3], n, m, mx, sum;
        
vector <ll> v[N];

queue <ll> q;

void fmbfs(ll s) {
	memset(d, -1, sizeof d);
	for (int i = 1; i <= n; ++i) d[i][1] = 0;
	q.push(s);
	d[s][0] = 0; d[s][1] = 1;
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (int j = 0; j < sz(v[x]); ++j) {
			int to = v[x][j];
			if (d[to][0] == -1) {
				d[to][0] = d[x][0] + 1;
				d[to][1] += d[x][1];
				q.push(to);			
				continue;
			}		
			if (d[to][0] == d[x][0] + 1) {
				d[to][1] += d[x][1];
			}
		}	
	}	
	for (int i = 1; i <= n; ++i) {
		mx = max(mx, d[i][0]);
	}         
}

bool bfs(ll s) {
	ll z = sum;
	memset(d, -1, sizeof d);
	for (int i = 1; i <= n; ++i) d[i][1] = 0;
	q.push(s);
	d[s][0] = 0; d[s][1] = 1;
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (int j = 0; j < sz(v[x]); ++j) {
		  int to = v[x][j];
			if (d[to][0] == -1) {
				d[to][0] = d[x][0] + 1;
				d[to][1] += d[x][1];
				q.push(to);			
				continue;
			}		
			if (d[to][0] == d[x][0] + 1) {
				d[to][1] += d[x][1];
			}
		}	
	}	
	for (int i = 1; i <= n; ++i) {
		if (mx == d[i][0]) {
			sum += d[i][1];
		}
	}
	return (z == sum);
}

int main () {
//	freopen(fname".in", "r", stdin);
	//freopen(fname".out", "w", stdout);
	scanf("%lld", &n);
	for (int i = 1; i <= n; ++i) {
		ll x, y; scanf("%lld%lld", &x, &y);
		v[x].pb(y); v[y].pb(x);
	}
	for (int i = 1; i <= n; ++i) {
		fmbfs(i);
	}
	for (int i = 1; i <= n; ++i) {
		if (!bfs(i)) {
			v[i].clear();			
		}      
	}
	printf("%lld", sum / 2);
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...