#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> way(n);
for(int i = 0; i < m; ++i){
int x, y;
cin >> x >> y;
--x, --y;
way[x].push_back(y);
way[y].push_back(x);
}
auto getfar = [&](auto&&self, int x, int pr)->pair<int, int>{
pair<int, int> rv = {0, x};
for(auto&nxt:way[x]){
if(nxt != pr){
rv = max(rv, self(self, nxt, x));
}
}
return {rv.first + 1, rv.second};
};
auto rv = getfar(getfar, 0, -1);
auto rv2 = getfar(getfar, rv.second, -1);
int x = rv.second, y = rv2.second;
vector<int> disx(n), disy(n);
auto dodis = [&](auto&&self, int x, int pr, vector<int>&vc)->void{
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
vc[nxt] = vc[x] + 1;
self(self, nxt, x, vc);
}
};
dodis(dodis, x, -1, disx);
dodis(dodis, y, -1, disy);
vector<int> dist(n, (int)1e9);
queue<int> que;
for(int i = 0; i < n; ++i){
if(disx[i] + disy[i] == disx[y]){
que.push(i);
dist[i] = 0;
}
}
while(!que.empty()){
int now = que.front();
que.pop();
for(auto&nxt:way[now]){
if(dist[nxt] == (int)1e9){
dist[nxt] = dist[now] + 1;
que.push(nxt);
}
}
}
for(int i = 0; i < n; ++i){
if(dist[i] > 2){
cout << "NO\n";
return 0;
}
}
cout << "YES\n1\n1\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Partially correct |
1 ms |
320 KB |
Failed to provide a successful strategy. |
3 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
4 |
Partially correct |
0 ms |
212 KB |
Failed to provide a successful strategy. |
5 |
Partially correct |
1 ms |
316 KB |
Failed to provide a successful strategy. |
6 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
7 |
Runtime error |
343 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Partially correct |
1 ms |
316 KB |
Failed to provide a successful strategy. |
3 |
Partially correct |
1 ms |
328 KB |
Failed to provide a successful strategy. |
4 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
5 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
6 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
7 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
8 |
Partially correct |
1 ms |
324 KB |
Failed to provide a successful strategy. |
9 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
10 |
Partially correct |
0 ms |
212 KB |
Failed to provide a successful strategy. |
11 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
12 |
Partially correct |
1 ms |
312 KB |
Failed to provide a successful strategy. |
13 |
Partially correct |
1 ms |
324 KB |
Failed to provide a successful strategy. |
14 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
15 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
16 |
Partially correct |
1 ms |
452 KB |
Failed to provide a successful strategy. |
17 |
Partially correct |
1 ms |
448 KB |
Failed to provide a successful strategy. |
18 |
Partially correct |
1 ms |
444 KB |
Failed to provide a successful strategy. |
19 |
Partially correct |
1 ms |
448 KB |
Failed to provide a successful strategy. |
20 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Partially correct |
1 ms |
320 KB |
Failed to provide a successful strategy. |
3 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
4 |
Partially correct |
0 ms |
212 KB |
Failed to provide a successful strategy. |
5 |
Partially correct |
1 ms |
316 KB |
Failed to provide a successful strategy. |
6 |
Partially correct |
1 ms |
212 KB |
Failed to provide a successful strategy. |
7 |
Runtime error |
343 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |