#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
typedef long long ll;
const ll OO = 1e18;
const int oo = 2e9;
const int N = 200100;
const int M = 1010;
vector<int> g[N], vc, cyc;
set<pii> st;
bool in_cyc[N];
int n, ans = 0, dst[N], mx = 0, mrk[N];
void CYCLE(int lst){
cyc.clear();
int it = sz(vc) - 1;
while (1){
cyc.PB(vc[it]);
in_cyc[vc[it]] = 1;
if (vc[it] == lst)
break;
it--;
}
}
void dfs(int v, int p){
if (sz(cyc) > 0) return;
vc.PB(v);
mrk[v] = 1;
for (int u : g[v]){
if (u == p) continue;
if (mrk[u] == 0)
dfs(u, v); else
if (mrk[u] == 1)
CYCLE(u);
}
mrk[v] = 2;
vc.pop_back();
}
int main(){
cin >> n;
for (int i = 0; i < n; i++){
int x, y; cin >> x >> y;
x--; y--;
g[x].PB(y); g[y].PB(x);
}
dfs(0, -1);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) {
dst[j] = oo;
mrk[j] = 0;
}
dst[i] = 0;
st.insert(MP(0, i));
while (sz(st)){
int v = (*st.begin()).sd;
mrk[v] |= in_cyc[v];
st.erase(st.begin());
for (int u : g[v])
if (dst[u] > dst[v] + 1){
st.erase(MP(dst[u], u));
mrk[u] = mrk[v];
dst[u] = dst[v] + 1;
st.insert(MP(dst[u], u));
}
}
for (int v = 0; v < n; v++) {
if (dst[v] > mx) {
mx = dst[v];
ans = 0;
}
if (dst[v] == mx)
ans += ((sz(cyc) % 2 == 0 && mrk[v]) ? 2 : 1);
}
}
cout << ans / 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
5112 KB |
Output is correct |
2 |
Correct |
124 ms |
5140 KB |
Output is correct |
3 |
Correct |
93 ms |
5240 KB |
Output is correct |
4 |
Incorrect |
176 ms |
5240 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1558 ms |
10104 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |