답안 #522236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522236 2022-02-04T08:48:48 Z blue Newspapers (CEOI21_newspapers) C++17
100 / 100
35 ms 4384 KB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


void answer(vector<int> res)
{
    if((int)res.size() == 0)
    {
        cout << "NO\n";
    }
    else
    {
        cout << "YES\n";
        cout << (int)res.size() << '\n';
        for(int r:res) cout << r << ' ';
        cout << '\n';
    }
}

const int maxN = 1000;
const int INF = 1005;

int N, M;
vector<int> edge[1+maxN];
vector< vector<int> > dist(1+maxN, vector<int>(1+maxN, INF));

int ans = 0;
vector<int> visit;


    int S1 = 1, S2 = 1;


int dfs(int u)
{
    int F = 0;
    for(int v: edge[u])
    {
        if(visit[v]) continue;
        visit[v] = 1;
        F = max(F, dfs(v));
    }
    return 1+F;
}








void dfs1(int s, int u)
{
    for(int v: edge[u])
    {
        if(dist[s][v] <= 1 + dist[s][u]) continue;
        dist[s][v] = 1 + dist[s][u];
        dfs1(s, v);
    }
}

vector<int> leaf(1+maxN, 0);



int main()
{
    cin >> N >> M;

    if(M > N-1)
    {
        cout << "NO\n";
        return 0;
    }

    for(int j = 0; j < M; j++)
    {
        int u, v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }


    for(int src = 1; src <= N; src++) //CHECK VALIDITY
    {
        visit = vector<int>(1+N, 0);
        ans = 0;

        visit[src] = 1;
        for(int v: edge[src])
        {
            visit[v] = 1;
            if(dfs(v) >= 3)
                ans++;
        }

        if(ans >= 3)
        {
            cout << "NO\n";
            return 0;
        }
    }








    //VALID!!!

    for(int u = 1; u <= N; u++)
    {
        dist[u][u] = 0;
        dfs1(u, u);
    }

    if(N == 1)
    {
        answer({1});
        return 0;
    }
    else if(N == 2)
    {
        answer({1, 1});
        return 0;
    }

    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(dist[i][j] > dist[S1][S2])
            {
                S1 = i;
                S2 = j;
            }
        }
    }
    S1 = edge[S1][0];
    S2 = edge[S2][0];

    vector<int> mainline;
    for(int i = 1; i <= N; i++)
    {
        if(dist[S1][i] + dist[i][S2] == dist[S1][S2])
        {
            mainline.push_back(i);
        }
    }

    sort(mainline.begin(), mainline.end(), [] (int x, int y)
    {
        return dist[S1][x] < dist[S1][y];
    });

    vector<int> anslist;
    for(int u: mainline)
    {
        anslist.push_back(u);
        for(int v: edge[u])
        {
            if(edge[v].size() > 1 && dist[S1][v] + dist[v][S2] > dist[S1][S2])
            {
                anslist.push_back(v);
                anslist.push_back(u);
            }
        }
    }

    vector<int> J;
    for(int q: edge[S2])
    {
        if(edge[q].size() > 1 && dist[S1][q] + dist[q][S2] > dist[S1][S2])
        {
            J.push_back(q);
        }
    }

    vector<int> al2 = anslist;
    if(al2.size() % 2 == 0) reverse(al2.begin(), al2.end());

    if(J.size() > 1)
    swap(al2[0], al2[2]);
    for(int t: al2) anslist.push_back(t);

    answer(anslist);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4172 KB Output is correct
2 Correct 2 ms 4172 KB Output is correct
3 Correct 2 ms 4172 KB Output is correct
4 Correct 2 ms 4240 KB Output is correct
5 Correct 2 ms 4172 KB Output is correct
6 Correct 2 ms 4172 KB Output is correct
7 Correct 3 ms 4172 KB Output is correct
8 Correct 2 ms 4172 KB Output is correct
9 Correct 2 ms 4172 KB Output is correct
10 Correct 2 ms 4172 KB Output is correct
11 Correct 2 ms 4172 KB Output is correct
12 Correct 3 ms 4172 KB Output is correct
13 Correct 2 ms 4172 KB Output is correct
14 Correct 3 ms 4172 KB Output is correct
15 Correct 3 ms 4172 KB Output is correct
16 Correct 3 ms 4276 KB Output is correct
17 Correct 3 ms 4172 KB Output is correct
18 Correct 4 ms 4172 KB Output is correct
19 Correct 2 ms 4244 KB Output is correct
20 Correct 2 ms 4280 KB Output is correct
21 Correct 2 ms 4172 KB Output is correct
22 Correct 2 ms 4172 KB Output is correct
23 Correct 2 ms 4280 KB Output is correct
24 Correct 2 ms 4172 KB Output is correct
25 Correct 2 ms 4172 KB Output is correct
26 Correct 2 ms 4276 KB Output is correct
27 Correct 2 ms 4172 KB Output is correct
28 Correct 2 ms 4172 KB Output is correct
29 Correct 3 ms 4280 KB Output is correct
30 Correct 2 ms 4172 KB Output is correct
31 Correct 3 ms 4172 KB Output is correct
32 Correct 3 ms 4172 KB Output is correct
33 Correct 3 ms 4280 KB Output is correct
34 Correct 3 ms 4280 KB Output is correct
35 Correct 2 ms 4172 KB Output is correct
36 Correct 2 ms 4284 KB Output is correct
37 Correct 3 ms 4172 KB Output is correct
38 Correct 2 ms 4172 KB Output is correct
39 Correct 2 ms 4172 KB Output is correct
40 Correct 2 ms 4172 KB Output is correct
41 Correct 2 ms 4276 KB Output is correct
42 Correct 2 ms 4276 KB Output is correct
43 Correct 2 ms 4172 KB Output is correct
44 Correct 2 ms 4172 KB Output is correct
45 Correct 2 ms 4172 KB Output is correct
46 Correct 2 ms 4172 KB Output is correct
47 Correct 2 ms 4172 KB Output is correct
48 Correct 3 ms 4172 KB Output is correct
49 Correct 2 ms 4172 KB Output is correct
50 Correct 3 ms 4172 KB Output is correct
51 Correct 4 ms 4172 KB Output is correct
52 Correct 2 ms 4172 KB Output is correct
53 Correct 2 ms 4172 KB Output is correct
54 Correct 2 ms 4172 KB Output is correct
55 Correct 2 ms 4276 KB Output is correct
56 Correct 2 ms 4172 KB Output is correct
57 Correct 2 ms 4172 KB Output is correct
58 Correct 2 ms 4172 KB Output is correct
59 Correct 2 ms 4172 KB Output is correct
60 Correct 2 ms 4196 KB Output is correct
61 Correct 3 ms 4172 KB Output is correct
62 Correct 2 ms 4276 KB Output is correct
63 Correct 2 ms 4172 KB Output is correct
64 Correct 2 ms 4172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
3 Correct 2 ms 4172 KB Output is correct
4 Correct 3 ms 4172 KB Output is correct
5 Correct 3 ms 4172 KB Output is correct
6 Correct 3 ms 4172 KB Output is correct
7 Correct 3 ms 4172 KB Output is correct
8 Correct 3 ms 4280 KB Output is correct
9 Correct 2 ms 4284 KB Output is correct
10 Correct 2 ms 4172 KB Output is correct
11 Correct 17 ms 4300 KB Output is correct
12 Correct 7 ms 4300 KB Output is correct
13 Correct 8 ms 4300 KB Output is correct
14 Correct 10 ms 4336 KB Output is correct
15 Correct 10 ms 4284 KB Output is correct
16 Correct 20 ms 4300 KB Output is correct
17 Correct 19 ms 4384 KB Output is correct
18 Correct 19 ms 4300 KB Output is correct
19 Correct 19 ms 4300 KB Output is correct
20 Correct 19 ms 4380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4172 KB Output is correct
2 Correct 2 ms 4172 KB Output is correct
3 Correct 2 ms 4172 KB Output is correct
4 Correct 2 ms 4240 KB Output is correct
5 Correct 2 ms 4172 KB Output is correct
6 Correct 2 ms 4172 KB Output is correct
7 Correct 3 ms 4172 KB Output is correct
8 Correct 2 ms 4172 KB Output is correct
9 Correct 2 ms 4172 KB Output is correct
10 Correct 2 ms 4172 KB Output is correct
11 Correct 2 ms 4172 KB Output is correct
12 Correct 3 ms 4172 KB Output is correct
13 Correct 2 ms 4172 KB Output is correct
14 Correct 3 ms 4172 KB Output is correct
15 Correct 3 ms 4172 KB Output is correct
16 Correct 3 ms 4276 KB Output is correct
17 Correct 3 ms 4172 KB Output is correct
18 Correct 4 ms 4172 KB Output is correct
19 Correct 2 ms 4244 KB Output is correct
20 Correct 2 ms 4280 KB Output is correct
21 Correct 2 ms 4172 KB Output is correct
22 Correct 2 ms 4172 KB Output is correct
23 Correct 2 ms 4280 KB Output is correct
24 Correct 2 ms 4172 KB Output is correct
25 Correct 2 ms 4172 KB Output is correct
26 Correct 2 ms 4276 KB Output is correct
27 Correct 2 ms 4172 KB Output is correct
28 Correct 2 ms 4172 KB Output is correct
29 Correct 3 ms 4280 KB Output is correct
30 Correct 2 ms 4172 KB Output is correct
31 Correct 3 ms 4172 KB Output is correct
32 Correct 3 ms 4172 KB Output is correct
33 Correct 3 ms 4280 KB Output is correct
34 Correct 3 ms 4280 KB Output is correct
35 Correct 2 ms 4172 KB Output is correct
36 Correct 2 ms 4284 KB Output is correct
37 Correct 3 ms 4172 KB Output is correct
38 Correct 2 ms 4172 KB Output is correct
39 Correct 2 ms 4172 KB Output is correct
40 Correct 2 ms 4172 KB Output is correct
41 Correct 2 ms 4276 KB Output is correct
42 Correct 2 ms 4276 KB Output is correct
43 Correct 2 ms 4172 KB Output is correct
44 Correct 2 ms 4172 KB Output is correct
45 Correct 2 ms 4172 KB Output is correct
46 Correct 2 ms 4172 KB Output is correct
47 Correct 2 ms 4172 KB Output is correct
48 Correct 3 ms 4172 KB Output is correct
49 Correct 2 ms 4172 KB Output is correct
50 Correct 3 ms 4172 KB Output is correct
51 Correct 4 ms 4172 KB Output is correct
52 Correct 2 ms 4172 KB Output is correct
53 Correct 2 ms 4172 KB Output is correct
54 Correct 2 ms 4172 KB Output is correct
55 Correct 2 ms 4276 KB Output is correct
56 Correct 2 ms 4172 KB Output is correct
57 Correct 2 ms 4172 KB Output is correct
58 Correct 2 ms 4172 KB Output is correct
59 Correct 2 ms 4172 KB Output is correct
60 Correct 2 ms 4196 KB Output is correct
61 Correct 3 ms 4172 KB Output is correct
62 Correct 2 ms 4276 KB Output is correct
63 Correct 2 ms 4172 KB Output is correct
64 Correct 2 ms 4172 KB Output is correct
65 Correct 3 ms 4172 KB Output is correct
66 Correct 2 ms 4172 KB Output is correct
67 Correct 3 ms 4172 KB Output is correct
68 Correct 2 ms 4284 KB Output is correct
69 Correct 2 ms 4172 KB Output is correct
70 Correct 2 ms 4172 KB Output is correct
71 Correct 3 ms 4172 KB Output is correct
72 Correct 3 ms 4172 KB Output is correct
73 Correct 2 ms 4172 KB Output is correct
74 Correct 2 ms 4284 KB Output is correct
75 Correct 2 ms 4172 KB Output is correct
76 Correct 2 ms 4172 KB Output is correct
77 Correct 2 ms 4276 KB Output is correct
78 Correct 2 ms 4172 KB Output is correct
79 Correct 3 ms 4244 KB Output is correct
80 Correct 3 ms 4300 KB Output is correct
81 Correct 3 ms 4300 KB Output is correct
82 Correct 3 ms 4284 KB Output is correct
83 Correct 3 ms 4300 KB Output is correct
84 Correct 4 ms 4300 KB Output is correct
85 Correct 3 ms 4300 KB Output is correct
86 Correct 3 ms 4300 KB Output is correct
87 Correct 3 ms 4300 KB Output is correct
88 Correct 3 ms 4300 KB Output is correct
89 Correct 30 ms 4332 KB Output is correct
90 Correct 21 ms 4348 KB Output is correct
91 Correct 19 ms 4336 KB Output is correct
92 Correct 17 ms 4284 KB Output is correct
93 Correct 28 ms 4352 KB Output is correct
94 Correct 18 ms 4312 KB Output is correct
95 Correct 28 ms 4300 KB Output is correct
96 Correct 29 ms 4300 KB Output is correct
97 Correct 16 ms 4328 KB Output is correct
98 Correct 27 ms 4340 KB Output is correct
99 Correct 22 ms 4336 KB Output is correct
100 Correct 22 ms 4340 KB Output is correct
101 Correct 19 ms 4300 KB Output is correct
102 Correct 22 ms 4336 KB Output is correct
103 Correct 23 ms 4284 KB Output is correct
104 Correct 16 ms 4332 KB Output is correct
105 Correct 18 ms 4332 KB Output is correct
106 Correct 23 ms 4308 KB Output is correct
107 Correct 21 ms 4296 KB Output is correct
108 Correct 27 ms 4284 KB Output is correct
109 Correct 20 ms 4344 KB Output is correct
110 Correct 21 ms 4300 KB Output is correct
111 Correct 18 ms 4292 KB Output is correct
112 Correct 19 ms 4336 KB Output is correct
113 Correct 27 ms 4288 KB Output is correct
114 Correct 23 ms 4344 KB Output is correct
115 Correct 25 ms 4300 KB Output is correct
116 Correct 24 ms 4300 KB Output is correct
117 Correct 28 ms 4340 KB Output is correct
118 Correct 23 ms 4300 KB Output is correct
119 Correct 20 ms 4280 KB Output is correct
120 Correct 24 ms 4320 KB Output is correct
121 Correct 25 ms 4300 KB Output is correct
122 Correct 19 ms 4336 KB Output is correct
123 Correct 21 ms 4336 KB Output is correct
124 Correct 35 ms 4344 KB Output is correct
125 Correct 16 ms 4300 KB Output is correct
126 Correct 18 ms 4336 KB Output is correct
127 Correct 16 ms 4332 KB Output is correct
128 Correct 26 ms 4352 KB Output is correct
129 Correct 3 ms 4300 KB Output is correct
130 Correct 3 ms 4288 KB Output is correct
131 Correct 3 ms 4300 KB Output is correct
132 Correct 3 ms 4300 KB Output is correct
133 Correct 3 ms 4300 KB Output is correct
134 Correct 3 ms 4292 KB Output is correct
135 Correct 2 ms 4300 KB Output is correct
136 Correct 2 ms 4300 KB Output is correct
137 Correct 3 ms 4292 KB Output is correct
138 Correct 3 ms 4300 KB Output is correct
139 Correct 2 ms 4300 KB Output is correct
140 Correct 3 ms 4300 KB Output is correct
141 Correct 4 ms 4288 KB Output is correct
142 Correct 2 ms 4300 KB Output is correct
143 Correct 2 ms 4296 KB Output is correct
144 Correct 18 ms 4300 KB Output is correct
145 Correct 19 ms 4284 KB Output is correct