#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define fi first
#define se second
#define new new228
#define pb push_back
#define rank rank228
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // st.oreder_of_key();
const int N = 100100;
const int INF = 1e9 + 7;
const int MAXN = 4 * N;
const int MOD = 998244353;
int TN = 1;
int n;
int ans;
int d[N], us[N];
vector<int> verr[N];
set<pair<int, int>> st;
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d %d", &x, &y);
verr[x].pb(y); verr[y].pb(x);
}
queue<int> q; q.push(1); us[1] = 1;
int root = 1;
while (!q.empty()) {
int x = q.front(); q.pop();
for (auto to: verr[x]) {
if (!us[to]) {
us[to] = true;
d[to] = d[x] + 1;
if (ans < d[to]) {
ans = d[to]; root = to;
}
q.push(to);
}
}
}
fill(d + 1, d + 1 + n, 0); fill(us + 1, us + 1 + n, 0);
q.push(root); us[root] = 1;
int cur = 0;
while (!q.empty()) {
int x = q.front(); q.pop();
for (auto to: verr[x]) {
if (!us[to]) {
us[to] = true;
d[to] = d[x] + 1;
if (ans < d[to]) {
ans = d[to]; cur = 1;
}
else if (ans == d[to]) {
cur++;
}
q.push(to);
}
}
}
// printf("max shortest's len is %d\n", ans);
int mx = 0;
int cnt = 0;
for (int i = 1; i <= n; i++) {
fill(d + 1, d + 1 + n, 0);
fill(us + 1, us + 1 + n, 0);
q.push(i); us[i] = true; cnt = 0;
while (!q.empty()) {
int x = q.front(); q.pop();
for (auto to: verr[x]) {
if (!us[to]) {
d[to] = d[x] + 1;
if (d[to] == ans) {
cnt++;
} else {
q.push(to);
}
us[to] = 1;
} else {
if (d[to] == ans) {
cnt++;
}
else if (d[x] > d[to]) {
continue;
}
else if (d[x] < d[to]) {
q.push(to);
}
}
}
}
// printf("%d %d\n", i, cnt);
mx += cnt;
}
cout << mx / 2 << "\n";
return;
}
signed main() {
// ios_base::sync_with_stdio(0);
// in; out; // cin >> TN;
while (TN--) { solve(); }
return 0;
}
Compilation message
shymbulak.cpp:22:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(disable : 4996)
shymbulak.cpp: In function 'void solve()':
shymbulak.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
shymbulak.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
3 ms |
2880 KB |
Output is correct |
4 |
Correct |
3 ms |
2880 KB |
Output is correct |
5 |
Correct |
4 ms |
2936 KB |
Output is correct |
6 |
Correct |
4 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
2936 KB |
Output is correct |
8 |
Correct |
4 ms |
2936 KB |
Output is correct |
9 |
Correct |
4 ms |
2940 KB |
Output is correct |
10 |
Correct |
4 ms |
2940 KB |
Output is correct |
11 |
Correct |
4 ms |
2940 KB |
Output is correct |
12 |
Correct |
4 ms |
3024 KB |
Output is correct |
13 |
Correct |
4 ms |
3024 KB |
Output is correct |
14 |
Correct |
7 ms |
3024 KB |
Output is correct |
15 |
Incorrect |
7 ms |
3024 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3024 KB |
Output is correct |
2 |
Correct |
5 ms |
3024 KB |
Output is correct |
3 |
Incorrect |
17 ms |
3024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1573 ms |
6992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |