답안 #971470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971470 2024-04-28T15:17:29 Z Szil Newspapers (CEOI21_newspapers) C++17
100 / 100
51 ms 8260 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// #define VERIFY

const int MAXN = 1001;

vector<int> g[MAXN], ans;
int n, m, md = 0, f = 0, cnt = 0, sz[MAXN];

void dfs(int u, int p = -1, int d = 0) {
    if (md < d) {
        md = d;
        f = u;
    }
    for (int v : g[u]) {
        if (v != p) dfs(v, u, d+1);
    }
}

void dfs2(int u, int p = -1) {
    if (cnt != n && g[u].size() != 1) ans.emplace_back(u);
    cnt++;
    for (int v : g[u]) {
        if (v != p) {
            dfs2(v, u);
            if (cnt != n && ans.back() != u) ans.emplace_back(u);
        }
    }
}

int dfs3(int u, int p = -1) {
    sz[u] = 1;
    for (int v : g[u]) {
        if (v != p) sz[u] = max(sz[u], dfs3(v, u)+1);
    }
    sort(g[u].begin(), g[u].end(), [&](int a, int b){
        return sz[a] < sz[b];
    });
    return sz[u];
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    if (n - 1 != m) {
        cout << "NO\n";
        return 0;
    }
    bool ok = true;
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        for (int u : g[i]) {
            md = 0;
            dfs(u, i);
            cnt += md >= 2;
        }
        if (cnt >= 3) {
            ok = false;
            break;
        }
    }
    cout << (ok ? "YES\n" : "NO\n");
    if (n == 1) {
        cout << "1\n1\n";
    } else if (n == 2) {
        cout << "2\n1 1\n";
    } else if (ok) {
        md = 0;
        dfs(1);
        dfs3(f);
        dfs2(f);
        vector<int> v;
        for (int i : ans) v.emplace_back(i);
        reverse(ans.begin(), ans.end());
        for (int i : ans) v.emplace_back(i);

#ifdef VERIFY
        assert(n <= 20);
        int bits = (1<<n)-1;
        for (int i : v) {
            int nw = 0;
            for (int j = 0; j < n; j++) {
                if (i != j + 1 && bits & (1<<j)) {
                    for (int u : g[j+1]) {
                        nw |= 1<<(u-1);
                    }
                }
            }
            bits = nw;
        }
        if (__popcount(bits) != 0) throw 1;
#endif

        cout << v.size() << "\n";
        for (int i : v) cout << i << " ";
        cout << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 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 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 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 600 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 484 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 484 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 480 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 344 KB Output is correct
48 Correct 1 ms 600 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 344 KB Output is correct
55 Correct 0 ms 344 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 1 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 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 480 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 7 ms 604 KB Output is correct
12 Correct 3 ms 600 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 4 ms 352 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 8 ms 604 KB Output is correct
17 Correct 8 ms 608 KB Output is correct
18 Correct 8 ms 604 KB Output is correct
19 Correct 9 ms 548 KB Output is correct
20 Correct 8 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 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 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 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 600 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 484 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 484 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 480 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 344 KB Output is correct
48 Correct 1 ms 600 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 344 KB Output is correct
55 Correct 0 ms 344 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 1 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 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 480 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 1 ms 352 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 356 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 1 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 1 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 Correct 0 ms 348 KB Output is correct
80 Correct 1 ms 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 1 ms 344 KB Output is correct
84 Correct 1 ms 344 KB Output is correct
85 Correct 1 ms 348 KB Output is correct
86 Correct 1 ms 348 KB Output is correct
87 Correct 1 ms 348 KB Output is correct
88 Correct 1 ms 348 KB Output is correct
89 Correct 12 ms 344 KB Output is correct
90 Correct 10 ms 344 KB Output is correct
91 Correct 9 ms 348 KB Output is correct
92 Correct 7 ms 348 KB Output is correct
93 Correct 13 ms 528 KB Output is correct
94 Correct 10 ms 344 KB Output is correct
95 Correct 13 ms 344 KB Output is correct
96 Correct 12 ms 344 KB Output is correct
97 Correct 7 ms 344 KB Output is correct
98 Correct 12 ms 348 KB Output is correct
99 Correct 9 ms 348 KB Output is correct
100 Correct 10 ms 348 KB Output is correct
101 Correct 9 ms 528 KB Output is correct
102 Correct 9 ms 348 KB Output is correct
103 Correct 12 ms 348 KB Output is correct
104 Correct 7 ms 348 KB Output is correct
105 Correct 8 ms 564 KB Output is correct
106 Correct 10 ms 348 KB Output is correct
107 Correct 8 ms 348 KB Output is correct
108 Correct 12 ms 600 KB Output is correct
109 Correct 10 ms 344 KB Output is correct
110 Correct 9 ms 348 KB Output is correct
111 Correct 8 ms 348 KB Output is correct
112 Correct 8 ms 348 KB Output is correct
113 Correct 13 ms 348 KB Output is correct
114 Correct 11 ms 344 KB Output is correct
115 Correct 8 ms 344 KB Output is correct
116 Correct 10 ms 348 KB Output is correct
117 Correct 12 ms 348 KB Output is correct
118 Correct 11 ms 528 KB Output is correct
119 Correct 9 ms 348 KB Output is correct
120 Correct 11 ms 344 KB Output is correct
121 Correct 11 ms 348 KB Output is correct
122 Correct 8 ms 528 KB Output is correct
123 Correct 8 ms 348 KB Output is correct
124 Correct 12 ms 344 KB Output is correct
125 Correct 7 ms 344 KB Output is correct
126 Correct 7 ms 492 KB Output is correct
127 Correct 7 ms 348 KB Output is correct
128 Correct 12 ms 348 KB Output is correct
129 Correct 1 ms 348 KB Output is correct
130 Correct 1 ms 348 KB Output is correct
131 Correct 1 ms 348 KB Output is correct
132 Correct 1 ms 484 KB Output is correct
133 Correct 1 ms 348 KB Output is correct
134 Correct 10 ms 2140 KB Output is correct
135 Correct 9 ms 1884 KB Output is correct
136 Correct 24 ms 5468 KB Output is correct
137 Correct 17 ms 3416 KB Output is correct
138 Correct 26 ms 4752 KB Output is correct
139 Correct 48 ms 7876 KB Output is correct
140 Correct 7 ms 1628 KB Output is correct
141 Correct 3 ms 860 KB Output is correct
142 Correct 43 ms 7536 KB Output is correct
143 Correct 51 ms 8260 KB Output is correct
144 Correct 8 ms 856 KB Output is correct
145 Correct 8 ms 600 KB Output is correct