/// In The Name Of God
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)5000 + 7, inf = (int)1e9 + 7, mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
int get_int() {
char x = getchar();
int res = 0;
while (!isdigit(x)) res = res * 10 + x - '0', x = getchar();
while (isdigit(x)) res = res * 10 + x - '0', x = getchar();
return res;
}
int n;
vector <int> g[N];
namespace diam {
int d[N];
pair <int, int> calc(int x) {
memset(d, -1, sizeof(d));
d[x] = 0;
queue <int> q;
q.push(x);
while (sz(q)) {
int v = q.front();
q.pop();
for (auto to : g[v]) {
if (d[to] == -1) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
pair <int, int> res = {-1, -1};
for (int i = 1; i <= n; i++) {
if (d[i] > res.f) res = {d[i], i};
}
return res;
}
int get() {
pair <int, int> cur = calc(1);
cur = calc(cur.s);
return cur.f;
}
}
int mx;
int d[N][N];
void maxdist() {
memset(d, -1, sizeof(d));
for (int i = 1; i <= n; i++) {
d[i][i] = 0;
queue <int> q;
q.push(i);
while (sz(q)) {
int v = q.front();
q.pop();
mx = max(mx, d[i][v]);
for (auto to : g[v]) {
if (d[i][to] == -1) {
d[i][to] = d[i][v] + 1;
q.push(to);
}
}
}
}
}
namespace slow {
int cnt;
ll ans;
ll dp[N];
ll solve(int x) {
queue <int> q;
q.push(x);
memset(dp, 0, sizeof(dp));
dp[x] = 1;
while (sz(q)) {
int v = q.front();
q.pop();
for (auto to : g[v]) {
if (d[x][to] > d[x][v]) {
q.push(to);
dp[to] += dp[x];
}
}
}
ll res = 0;
for (int i = 1; i <= n; i++) {
if (d[x][i] == mx) res += dp[i], cnt++;
}
return res;
}
void solve() {
for (int i = 1; i <= n; i++) ans += solve(i);
if (cnt * 2 < (ans / 2)) assert(0);
cout << ans / 2;
ioi
}
}
namespace check {
void solve() {
int st = -1;
for (int i = 1; i <= n && st == -1; i++) {
for (int j = 1; j <= n; j++) {
if (d[i][j] == mx) {
st = i;
break;
}
}
}
cout << slow :: solve(st);
ioi
}
}
int main() {
#ifdef IOI2018
freopen ("in.txt", "r", stdin);
freopen ("slow.out", "w", stdout);
#endif
n = get_int();
for (int i = 1; i <= n; i++) {
int v = get_int(), u = get_int();
g[v].pb(u);
g[u].pb(v);
}
maxdist();
slow :: solve();
ioi
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
100128 KB |
Output is correct |
2 |
Correct |
9 ms |
100128 KB |
Output is correct |
3 |
Correct |
0 ms |
100128 KB |
Output is correct |
4 |
Correct |
3 ms |
100128 KB |
Output is correct |
5 |
Correct |
9 ms |
100128 KB |
Output is correct |
6 |
Correct |
9 ms |
100128 KB |
Output is correct |
7 |
Correct |
13 ms |
100128 KB |
Output is correct |
8 |
Correct |
3 ms |
100128 KB |
Output is correct |
9 |
Correct |
6 ms |
100128 KB |
Output is correct |
10 |
Correct |
0 ms |
100128 KB |
Output is correct |
11 |
Correct |
9 ms |
100128 KB |
Output is correct |
12 |
Correct |
6 ms |
100128 KB |
Output is correct |
13 |
Correct |
3 ms |
100128 KB |
Output is correct |
14 |
Correct |
13 ms |
100128 KB |
Output is correct |
15 |
Correct |
9 ms |
100128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
100128 KB |
Output is correct |
2 |
Correct |
29 ms |
100128 KB |
Output is correct |
3 |
Correct |
46 ms |
100128 KB |
Output is correct |
4 |
Correct |
39 ms |
100128 KB |
Output is correct |
5 |
Correct |
1169 ms |
100260 KB |
Output is correct |
6 |
Correct |
699 ms |
100392 KB |
Output is correct |
7 |
Execution timed out |
1500 ms |
100260 KB |
Execution timed out |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
100128 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |