This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
//ifstream cin("C:\\CompetitiveProgramming\\Problems\\CEOI\\2021\\day1\\in.txt");
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;
vector<bitset<1000>> graphAlt(N);
for (ll i = 0; i < N; i++) for (ll x : graph[i]) graphAlt[i][x] = 1;
v valids;
for (ll i = 0; i < N; i++)
{
ll cnt = 0;
for (ll x : graph[i]) if (graph[x].size() > 1) cnt++;
if (graph[i].size() > 1 && cnt <= 1) valids.pb(i);
}
random_shuffle(all(valids));
for (ll j = 0; j < min((ll)10,(ll)valids.size()); j++)
{
ll i = valids[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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |