#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll,ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
bool eulerTour(ll i, v &visited, vv &graph, v &res, ll &uniqCount, ll nonLeafCount)
{
res.pb(i);
visited[i]++;
uniqCount++;
for (ll x : graph[i])
{
if (visited[x] == 0 && graph[x].size() > 1)
{
bool poss = eulerTour(x,visited,graph,res,uniqCount, nonLeafCount);
if (!poss) return false;
res.pb(i);
visited[i]++;
if (visited[i] >= 3) return false;
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll N, M;
cin >> N >> M;
vv graph(N);
for (ll i= 0; i < M; i++)
{
ll a, b;
cin >> a >> b;
graph[a-1].pb(b-1);
graph[b-1].pb(a-1);
}
if (M != N - 1)
{
cout << "NO\n";
return 0;
}
//theory - find euler tour, check if visiting a vertex thrice (at least). if yes, then NO. otherwise check if works. if not, repeat twice
if (N == 1)
{
cout << "YES\n1\n1\n";
return 0;
}
if (N == 2)
{
cout << "YES\n2\n1 1\n";
return 0;
}
ll mini = INF;
v minTour;
for (ll i= 0; i < N; i++)
{
if (graph[i].size() == 1) continue;
ll cnt = 0;
v visited(N, 0);
v tour;
ll nonLeafCount = 0;
for (auto x : graph) if (x.size() != 1) nonLeafCount++;
if (eulerTour(i, visited, graph, tour, cnt, nonLeafCount))
{
vb available(N, true);
bool needOnce = true;
for (ll x : tour)
{
available[x] = false;
vb newGeneration(N, false);
for (ll i = 0 ;i < N; i++) if (available[i]) for (ll x : graph[i]) newGeneration[x] = true;
available = newGeneration;
}
for (ll i = 0; i < N; i++) if (available[i]) needOnce = false;
if (!needOnce)
{
v newTour;
for (ll x : tour) newTour.pb(x);
for (ll x : tour) newTour.pb(x);
tour = newTour;
}
mini = tour.size();
minTour = tour;
}
}
if (mini == INF)
{
cout << "NO\n"; return 0;
}
cout << "YES\n";
cout << mini << "\n";
for (ll x : minTour) cout << x + 1 << " ";
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
4 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Partially correct |
1 ms |
348 KB |
Provide a successful but not optimal strategy. |
5 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
6 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
7 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
8 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
9 |
Partially correct |
1 ms |
344 KB |
Provide a successful but not optimal strategy. |
10 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
11 |
Partially correct |
32 ms |
708 KB |
Provide a successful but not optimal strategy. |
12 |
Partially correct |
10 ms |
604 KB |
Provide a successful but not optimal strategy. |
13 |
Partially correct |
14 ms |
604 KB |
Provide a successful but not optimal strategy. |
14 |
Partially correct |
16 ms |
600 KB |
Provide a successful but not optimal strategy. |
15 |
Partially correct |
18 ms |
604 KB |
Provide a successful but not optimal strategy. |
16 |
Partially correct |
36 ms |
712 KB |
Provide a successful but not optimal strategy. |
17 |
Partially correct |
35 ms |
604 KB |
Provide a successful but not optimal strategy. |
18 |
Partially correct |
39 ms |
600 KB |
Provide a successful but not optimal strategy. |
19 |
Partially correct |
36 ms |
720 KB |
Provide a successful but not optimal strategy. |
20 |
Partially correct |
37 ms |
604 KB |
Provide a successful but not optimal strategy. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
4 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |