Submission #592366

# Submission time Handle Problem Language Result Execution time Memory
592366 2022-07-09T05:57:34 Z SeDunion Newspapers (CEOI21_newspapers) C++17
100 / 100
77 ms 28172 KB
#include <iostream>
#include <cassert>
#include <iomanip>
#include <algorithm>
#include <string>
#include <bitset> 
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
	#include <bits/stdc++.h>
	#define cerr if(false)cerr
#endif
 
using namespace std;
using ll = long long;

const int N = 1e6 + 66;

int cycle;
vector<int>g[N];
 
int n;

int used[N];
 
void dfsdfs(int v, int p = -1) {
	used[v] = 1;
	for (int to : g[v]) if (to != p) {
		if (used[to] == 1) cycle = 1;
		if (used[to] == 0) dfsdfs(to, v);
	}
	used[v] = 2;
}

int d[N];

queue<int>q;

int p[N];

int bfs(int v) {
	for (int i = 1 ; i <= n ; ++ i) d[i] = ((int)g[i].size() == 1 ? -2 : -1);
	d[v] = 0;
	p[v] = 0;
	q.push(v);
	while (q.size()) {
		v = q.front(); q.pop();
		for (int to : g[v]) if (d[to] == -1) {
			d[to] = d[v] + 1;
			p[to] = v;
			q.push(to);
		}
	}
	for (int i = 1 ; i <= n ; ++ i) if (d[i] > d[v]) v = i;
	return v;
}

vector<int>temp;

void go(int v) {
	used[v] = 1;
	if ((int)g[v].size() == 1) return;
	temp.emplace_back(v);
	for (int to : g[v]) if (!used[to] && (int)g[to].size() > 1) {
		go(to);
		temp.emplace_back(v);
	}
}

int go(int v, int p1, int d = 1) {
	if (d == 3) return 1;
	for (int to : g[v]) if (to != p1 && go(to, v, d+1)) return 1;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int m;
	cin >> n >> m;
	if (n == 1) {
		cout << "YES\n1\n1";
		return 0;
	}
	if (n == 2) {
		cout << "YES\n2\n1 1";
		return 0;
	}
	while (m--) {
		int a, b;
		cin >> a >> b;
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	for (int i = 1 ; i <= n ; ++ i) {
		int cnt = 0;
		for (int j : g[i]) cnt += go(j, i, 1);
		if (cnt >= 3) {
			cout << "NO";
			return 0;
		}
	}
	for (int i = 1 ; i <= n ; ++ i) if (!used[i]) {
		dfsdfs(i);
	}
	if (cycle) {
		cout << "NO";
		return 0;
	}
	for (int i = 1 ; i <= n ; ++ i) used[i] = 0;
	vector<int>ans;
	int v = -1;
	for (int u = 1 ; u <= n ; ++ u) if ((int)g[u].size() >= 2) v = u;
	assert(v != -1);
	temp.clear();
	int x = bfs(v);
	int y = bfs(x);
	int z = y;
	for (int i = 1 ; i <= n ; ++ i) used[i] = 0;
	while (z > 0) {
		used[z] = 1;
		z = p[z];
	}
	z = y;
	while (z > 0) {
		go(z);
		z = p[z];
	}
	for (int i : temp) ans.emplace_back(i);
	if ((int)temp.size() % 2 == 0) reverse(temp.begin(), temp.end());
	for (int i : temp) ans.emplace_back(i);
	cout << "YES\n";
	cout << ans.size() << "\n";
	for (int i : ans) cout << i << " ";	
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23828 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23824 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 12 ms 23820 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 15 ms 23728 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 15 ms 23764 KB Output is correct
12 Correct 12 ms 23744 KB Output is correct
13 Correct 13 ms 23760 KB Output is correct
14 Correct 14 ms 23832 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 13 ms 23820 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 13 ms 23800 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 13 ms 23824 KB Output is correct
25 Correct 13 ms 23820 KB Output is correct
26 Correct 12 ms 23832 KB Output is correct
27 Correct 12 ms 23764 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 13 ms 23736 KB Output is correct
30 Correct 13 ms 23724 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 12 ms 23816 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 14 ms 23828 KB Output is correct
35 Correct 13 ms 23764 KB Output is correct
36 Correct 12 ms 23808 KB Output is correct
37 Correct 12 ms 23800 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 12 ms 23820 KB Output is correct
40 Correct 12 ms 23764 KB Output is correct
41 Correct 14 ms 23756 KB Output is correct
42 Correct 13 ms 23764 KB Output is correct
43 Correct 12 ms 23764 KB Output is correct
44 Correct 16 ms 23760 KB Output is correct
45 Correct 12 ms 23764 KB Output is correct
46 Correct 15 ms 23764 KB Output is correct
47 Correct 14 ms 23764 KB Output is correct
48 Correct 15 ms 23764 KB Output is correct
49 Correct 13 ms 23820 KB Output is correct
50 Correct 12 ms 23764 KB Output is correct
51 Correct 13 ms 23824 KB Output is correct
52 Correct 12 ms 23824 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 12 ms 23808 KB Output is correct
55 Correct 13 ms 23820 KB Output is correct
56 Correct 12 ms 23720 KB Output is correct
57 Correct 13 ms 23764 KB Output is correct
58 Correct 13 ms 23824 KB Output is correct
59 Correct 12 ms 23764 KB Output is correct
60 Correct 15 ms 23824 KB Output is correct
61 Correct 14 ms 23764 KB Output is correct
62 Correct 15 ms 23820 KB Output is correct
63 Correct 15 ms 23764 KB Output is correct
64 Correct 12 ms 23820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 14 ms 23820 KB Output is correct
4 Correct 14 ms 23844 KB Output is correct
5 Correct 12 ms 23812 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23828 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 14 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 24 ms 23956 KB Output is correct
12 Correct 15 ms 23892 KB Output is correct
13 Correct 15 ms 23880 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 13 ms 23892 KB Output is correct
16 Correct 13 ms 23920 KB Output is correct
17 Correct 13 ms 23892 KB Output is correct
18 Correct 14 ms 23960 KB Output is correct
19 Correct 13 ms 23892 KB Output is correct
20 Correct 16 ms 23860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23828 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23824 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 12 ms 23820 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 15 ms 23728 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 15 ms 23764 KB Output is correct
12 Correct 12 ms 23744 KB Output is correct
13 Correct 13 ms 23760 KB Output is correct
14 Correct 14 ms 23832 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 13 ms 23820 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 13 ms 23800 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 13 ms 23824 KB Output is correct
25 Correct 13 ms 23820 KB Output is correct
26 Correct 12 ms 23832 KB Output is correct
27 Correct 12 ms 23764 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 13 ms 23736 KB Output is correct
30 Correct 13 ms 23724 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 12 ms 23816 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 14 ms 23828 KB Output is correct
35 Correct 13 ms 23764 KB Output is correct
36 Correct 12 ms 23808 KB Output is correct
37 Correct 12 ms 23800 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 12 ms 23820 KB Output is correct
40 Correct 12 ms 23764 KB Output is correct
41 Correct 14 ms 23756 KB Output is correct
42 Correct 13 ms 23764 KB Output is correct
43 Correct 12 ms 23764 KB Output is correct
44 Correct 16 ms 23760 KB Output is correct
45 Correct 12 ms 23764 KB Output is correct
46 Correct 15 ms 23764 KB Output is correct
47 Correct 14 ms 23764 KB Output is correct
48 Correct 15 ms 23764 KB Output is correct
49 Correct 13 ms 23820 KB Output is correct
50 Correct 12 ms 23764 KB Output is correct
51 Correct 13 ms 23824 KB Output is correct
52 Correct 12 ms 23824 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 12 ms 23808 KB Output is correct
55 Correct 13 ms 23820 KB Output is correct
56 Correct 12 ms 23720 KB Output is correct
57 Correct 13 ms 23764 KB Output is correct
58 Correct 13 ms 23824 KB Output is correct
59 Correct 12 ms 23764 KB Output is correct
60 Correct 15 ms 23824 KB Output is correct
61 Correct 14 ms 23764 KB Output is correct
62 Correct 15 ms 23820 KB Output is correct
63 Correct 15 ms 23764 KB Output is correct
64 Correct 12 ms 23820 KB Output is correct
65 Correct 14 ms 23764 KB Output is correct
66 Correct 13 ms 23764 KB Output is correct
67 Correct 13 ms 23764 KB Output is correct
68 Correct 12 ms 23728 KB Output is correct
69 Correct 12 ms 23708 KB Output is correct
70 Correct 12 ms 23824 KB Output is correct
71 Correct 13 ms 23784 KB Output is correct
72 Correct 12 ms 23824 KB Output is correct
73 Correct 13 ms 23764 KB Output is correct
74 Correct 12 ms 23824 KB Output is correct
75 Correct 12 ms 23848 KB Output is correct
76 Correct 13 ms 23832 KB Output is correct
77 Correct 13 ms 23824 KB Output is correct
78 Correct 13 ms 23764 KB Output is correct
79 Correct 12 ms 23764 KB Output is correct
80 Correct 12 ms 23800 KB Output is correct
81 Correct 12 ms 23764 KB Output is correct
82 Correct 12 ms 23764 KB Output is correct
83 Correct 13 ms 23828 KB Output is correct
84 Correct 14 ms 23764 KB Output is correct
85 Correct 12 ms 23808 KB Output is correct
86 Correct 12 ms 23736 KB Output is correct
87 Correct 13 ms 23764 KB Output is correct
88 Correct 12 ms 23764 KB Output is correct
89 Correct 16 ms 23828 KB Output is correct
90 Correct 12 ms 23892 KB Output is correct
91 Correct 17 ms 23892 KB Output is correct
92 Correct 12 ms 23892 KB Output is correct
93 Correct 13 ms 23836 KB Output is correct
94 Correct 12 ms 23828 KB Output is correct
95 Correct 12 ms 23892 KB Output is correct
96 Correct 13 ms 23828 KB Output is correct
97 Correct 13 ms 23816 KB Output is correct
98 Correct 13 ms 23864 KB Output is correct
99 Correct 13 ms 23912 KB Output is correct
100 Correct 13 ms 23892 KB Output is correct
101 Correct 12 ms 23892 KB Output is correct
102 Correct 12 ms 23824 KB Output is correct
103 Correct 12 ms 23892 KB Output is correct
104 Correct 12 ms 23828 KB Output is correct
105 Correct 12 ms 23880 KB Output is correct
106 Correct 14 ms 23892 KB Output is correct
107 Correct 13 ms 23820 KB Output is correct
108 Correct 17 ms 23884 KB Output is correct
109 Correct 14 ms 23828 KB Output is correct
110 Correct 13 ms 23892 KB Output is correct
111 Correct 13 ms 23892 KB Output is correct
112 Correct 12 ms 23892 KB Output is correct
113 Correct 13 ms 23896 KB Output is correct
114 Correct 13 ms 23892 KB Output is correct
115 Correct 13 ms 23892 KB Output is correct
116 Correct 12 ms 23892 KB Output is correct
117 Correct 14 ms 23892 KB Output is correct
118 Correct 15 ms 23908 KB Output is correct
119 Correct 14 ms 23916 KB Output is correct
120 Correct 14 ms 23812 KB Output is correct
121 Correct 12 ms 23892 KB Output is correct
122 Correct 12 ms 23852 KB Output is correct
123 Correct 13 ms 23840 KB Output is correct
124 Correct 14 ms 23824 KB Output is correct
125 Correct 13 ms 23892 KB Output is correct
126 Correct 13 ms 23892 KB Output is correct
127 Correct 15 ms 23804 KB Output is correct
128 Correct 12 ms 23892 KB Output is correct
129 Correct 11 ms 23792 KB Output is correct
130 Correct 12 ms 23764 KB Output is correct
131 Correct 12 ms 23764 KB Output is correct
132 Correct 13 ms 23820 KB Output is correct
133 Correct 12 ms 23828 KB Output is correct
134 Correct 25 ms 24736 KB Output is correct
135 Correct 27 ms 24792 KB Output is correct
136 Correct 44 ms 27332 KB Output is correct
137 Correct 33 ms 25804 KB Output is correct
138 Correct 44 ms 26356 KB Output is correct
139 Correct 72 ms 28172 KB Output is correct
140 Correct 20 ms 24532 KB Output is correct
141 Correct 16 ms 24096 KB Output is correct
142 Correct 77 ms 28012 KB Output is correct
143 Correct 74 ms 28064 KB Output is correct
144 Correct 13 ms 23892 KB Output is correct
145 Correct 15 ms 23952 KB Output is correct