/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1010;
int n, m;
vector < int > adj[maxn];
int used[maxn], rev[maxn];
void bfs(int v)
{
for (int i = 1; i <= n; i ++)
used[i] = -1;
used[v] = 0;
rev[v] = 0;
queue < int > q;
q.push(v);
while(!q.empty())
{
int cur = q.front();
q.pop();
for (int u : adj[cur])
{
if (used[u] == -1)
{
used[u] = used[cur] + 1;
rev[u] = cur;
q.push(u);
}
}
}
}
vector < pair < int, int > > acc[maxn];
vector < int > seq;
vector < int > act;
int mark[maxn];
void add_to_seq(int ver)
{
seq.push_back(ver);
int i = 0;
while(i < act.size() && act[i] != ver)
i ++;
if (i != act.size())
act.erase(act.begin() + i);
for (int i = 1; i <= n; i ++)
{
mark[i] = 0;
}
vector < int > new_act;
for (int v : act)
{
for (int u : adj[v])
mark[u] = 1;
}
for (int i = 1; i <= n; i ++)
if (mark[i])
new_act.push_back(i);
act = new_act;
}
void solve()
{
cin >> n >> m;
if (m != n - 1)
{
cout << "NO" << endl;
return;
}
for (int i = 1; i <= m; i ++)
{
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
if (n == 1)
{
cout << "YES" << endl << 1 << endl << 1 << endl;
return;
}
if (n == 2)
{
cout << "YES" << endl << 2 << endl << "1 1" << endl;
return;
}
bfs(1);
int s = 1;
for (int i = 1; i <= n; i ++)
if (used[i] > used[s])
s = i;
bfs(s);
int t = 1;
for (int i = 1; i <= n; i ++)
if (used[i] > used[t])
t = i;
/**for (int i = 1; i <= n; i ++)
cout << used[i] << " ";
cout << endl;*/
vector < int > chain;
int ver = t;
while(ver != s)
{
chain.push_back(ver);
ver = rev[ver];
}
chain.push_back(ver);
reverse(chain.begin(), chain.end());
/**cout << "-----------" << endl;
for (int cur : chain)
cout << cur << " ";
cout << endl;*/
for (int i = 1; i <= n; i ++)
{
used[i] = 0;
}
chain.erase(chain.begin());
chain.pop_back();
s = chain[0];
t = chain.back();
for (int cur : chain)
{
used[cur] = 1;
}
for (int cur : chain)
{
for (int nb : adj[cur])
{
if (used[nb])
continue;
for (int u : adj[nb])
{
if (u != cur)
{
acc[cur].push_back({nb, u});
}
}
}
}
for (int i = 1; i <= n; i ++)
act.push_back(i);
for (int cur : chain)
{
add_to_seq(cur);
for (pair < int, int > d : acc[cur])
{
add_to_seq(d.first);
add_to_seq(cur);
}
}
int cnt = 0;
while(act.size() > 0)
{
cnt ++;
assert(cnt < 10 * n);
int ver = act.back();
add_to_seq(ver);
}
cout << "YES" << endl;
cout << seq.size() << endl;
for (int cur : seq)
cout << cur << " ";
cout << endl;
}
int main()
{
solve();
return 0;
}
Compilation message
newspapers.cpp: In function 'void add_to_seq(int)':
newspapers.cpp:61:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | while(i < act.size() && act[i] != ver)
| ~~^~~~~~~~~~~~
newspapers.cpp:63:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (i != act.size())
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
488 KB |
Output is correct |
8 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
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 |
344 KB |
Output is correct |
8 |
Partially correct |
0 ms |
344 KB |
Provide a successful but not optimal strategy. |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Partially correct |
0 ms |
348 KB |
Provide a successful but not optimal strategy. |
11 |
Partially correct |
8 ms |
348 KB |
Provide a successful but not optimal strategy. |
12 |
Correct |
3 ms |
348 KB |
Output is correct |
13 |
Partially correct |
5 ms |
536 KB |
Provide a successful but not optimal strategy. |
14 |
Partially correct |
4 ms |
348 KB |
Provide a successful but not optimal strategy. |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Partially correct |
9 ms |
344 KB |
Provide a successful but not optimal strategy. |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Partially correct |
9 ms |
344 KB |
Provide a successful but not optimal strategy. |
19 |
Correct |
9 ms |
344 KB |
Output is correct |
20 |
Partially correct |
9 ms |
348 KB |
Provide a successful but not optimal strategy. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
488 KB |
Output is correct |
8 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
9 |
Halted |
0 ms |
0 KB |
- |