#include <bits/stdc++.h>
using namespace std;
typedef short 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;
void 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)
{
eulerTour(x,visited,graph,res,uniqCount, nonLeafCount);
if (uniqCount < nonLeafCount)
{
res.pb(i);
visited[i]++;
}
}
}
}
ll nonLeafSubtreeCount(ll i, v &res, vv &graph)
{
if (res[i] != -1 || graph[i].size() == 1) return 0;
res[i] = 1;
for (ll x : graph[i]) res[i] += nonLeafSubtreeCount(x, res, graph);
return res[i];
}
bool simulate(v &order, vector<bitset<1000>> &graph)
{
ll N = graph.size();
bitset<1000> available;
for (ll i= 0; i < N; i++) available[i] = 1;
bool poss = true;
for (ll x : order)
{
available[x] = 0;
bitset<1000> newGeneration;
for (ll i = 0; i < N; i++)
{
if (available[i]) newGeneration |= graph[i];
}
available = newGeneration;
}
return available.none();
}
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;
v order(N);
for (ll i= 0; i < N; i++) order[i] = i;
random_shuffle(all(order));
vector<bitset<1000>> graphAlt(N);
for (ll i = 0; i < N; i++) for (ll x : graph[i]) graphAlt[i][x] = 1;
for (ll j= 0; j < N; j++)
{
ll i = order[j];
if (graph[i].size() == 1) continue;
ll valid = 0;
for (ll x : graph[i]) if (graph[x].size() > 1) valid++;
if (valid > 1) continue;
v nonLeafSubtreeCounts(N, -1); nonLeafSubtreeCount(i, nonLeafSubtreeCounts, graph);
for (ll i = 0; i < N; i++) sort(all(graph[i]),[&nonLeafSubtreeCounts](ll i, ll j){return nonLeafSubtreeCounts[i] < nonLeafSubtreeCounts[j];});
ll cnt = 0;
v visited(N, 0);
v tour;
ll nonLeafCount = 0;
for (auto x : graph) if (x.size() != 1) nonLeafCount++;
eulerTour(i, visited, graph, tour, cnt, nonLeafCount);
v res;
if (simulate(tour, graphAlt))
{
res = tour;
}
else
{
v newTour1;
for (ll x : tour) newTour1.pb(x);
for (ll x : tour) newTour1.pb(x);
if (simulate(newTour1, graphAlt)) res = newTour1;
else
{
v newTour2;
for (ll x : tour) newTour2.pb(x);
for (ll i = tour.size() - 1; i >= 0; i--) newTour2.pb(tour[i]);
if (simulate(newTour2, graphAlt)) res = newTour2;
}
}
if (res.size() > 0)
{
mini = res.size();
minTour = res;
break;
}
}
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;
}
Compilation message
newspapers.cpp:27:16: warning: overflow in conversion from 'double' to 'll' {aka 'short int'} changes value from '1.0e+18' to '32767' [-Woverflow]
27 | const ll INF = 1e18;
| ^~~~
newspapers.cpp:28:20: warning: overflow in conversion from 'double' to 'll' {aka 'short int'} changes value from '1.000000007e+9' to '32767' [-Woverflow]
28 | const ll mod = 1e9 + 7;
| ~~~~^~~
newspapers.cpp: In function 'bool simulate(v&, std::vector<std::bitset<1000> >&)':
newspapers.cpp:63:10: warning: unused variable 'poss' [-Wunused-variable]
63 | bool poss = true;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
504 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
344 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
25 ms |
700 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
9 ms |
604 KB |
Output is correct |
14 |
Correct |
12 ms |
600 KB |
Output is correct |
15 |
Correct |
7 ms |
604 KB |
Output is correct |
16 |
Correct |
24 ms |
724 KB |
Output is correct |
17 |
Correct |
14 ms |
604 KB |
Output is correct |
18 |
Correct |
24 ms |
856 KB |
Output is correct |
19 |
Correct |
14 ms |
600 KB |
Output is correct |
20 |
Correct |
31 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
504 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
344 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
500 KB |
Output is correct |
74 |
Correct |
1 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Execution timed out |
1096 ms |
620 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |