답안 #940826

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940826 2024-03-07T17:26:59 Z berr Newspapers (CEOI21_newspapers) C++17
100 / 100
113 ms 47476 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
 
int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n, m; cin >> n >> m;
 
    vector<vector<int>> g(n);
    vector<int> d(n), val(n);
 
    for(int i=0; i<m; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
 
    int flag = 1;
 
    auto dfs=[&](int v, int p, auto&&dfs)->int{
        if(v==p) d[v]=0;
        else d[v]=d[p]+1;
 
        val[v] = d[v];
 
        vector<int> a;
        for(auto i: g[v]){  
            if(i!=p) a.push_back(dfs(i, v, dfs));
        }
 
        sort(a.begin(), a.end());
/*        sort(g[v].begin(), g[v].end(), [&](int x, int y){
            return val[x]<val[y];
        });*/
 
        if(a.size())
        val[v]=max(val[v], a.back());
 
        if(v==p){
            if(a.size()>2 && a[a.size()-3] >2) flag =0;
        }
        return val[v];
          
    };
 
    if(n==m+1)
    for(int i=0; i<n; i++) dfs(i, i, dfs);
    else flag =0;
 
    if(flag){ 
        cout<<"YES\n";
        if(n==1) cout<<1<<" "<<1<<"\n";
        else if(n==2) cout<<"2 2 2"<<"\n";
        else{
            int check = 1;
            vector a(n, vector(n, vector<int>()));
            vector vis(n, vector(n, 0));
 
            auto dfs2=[&](int v, int p, auto&&dfs2)->vector<int>{
                vector<int> s; 
                if(vis[v][p]) return a[v][p];
                vis[v][p]=1;
             //   cout<<v<<" "<<d[v]<<" "<<val[v]<<"\n";
                if(d[v]==val[v]) return a[v][p]=s;
                else if(d[v]>=val[v]-1){
                    s.push_back(v);
                    return a[v][p]=s;
                }            
                else{
                    vector<int> b;
                    vector<vector<int>> c;
 
                    for(auto i: g[v]){
                        if(i==p) continue;
 
                        c.push_back(dfs2(i, v, dfs2));
                    }
 
          
                    sort(c.begin(), c.end(), [&](auto x, auto y){
                        return x.size()<y.size();
                    });
 
                    if(!c.back().size()){
                        return b;
                    }
 
                    
 
                    if(c.size()>1){
                        int k = c.size()-2;
                        reverse(c[k].begin(), c[k].end());
                    }   
 
      
                    if(v!=p){
                         for(auto i: c.back()) b.push_back(i);
                        b.push_back(v);
                        for(int i=0; i<c.size()-1; i++){
     
                            if(!c[i].size()){
                                continue;
                            }
                            for(auto j: c[i]){
                                b.push_back(j);
                            }
                            b.push_back(v);
     
     
                        }
                    }
                    else{
                        for(auto i: c.back()) b.push_back(i);
                        b.push_back(v);
 
                        for(int i=0; i<c.size()-1; i++){
     
                            if(!c[i].size()){
                                continue;
                            }
                            for(auto j: c[i]){
                                b.push_back(j);
                            }
                            if(i!=c.size()-2)
                            b.push_back(v);
     
     
                        }
                    }
                    
                    if(v!=p){
                        if(c.size()>=2 && c[c.size()-2].size()>1) check=0;
                    }
 
                    return a[v][p]=b;
                }
            };
 
            vector<int> x;
 
            for(int i=0; i<n; i++){
                dfs(i, i, dfs);
                check = 1;
                auto s = dfs2(i, i, dfs2);
                if(!x.size()&&check) x = s;
                else if(s.size()<x.size()&&check) x = s; 
            }
 
           // reverse(x.begin(), x.end());
 
            for(int i=x.size()-1; i>=0; i--){
                x.push_back(x[i]);
            }
 
            cout<<x.size()<<"\n";
 
            for(auto i: x) cout<<i+1<<" ";
                cout<<"\n";
 
 
        } 
    }
    else cout<<"NO\n";
 
}

Compilation message

newspapers.cpp: In lambda function:
newspapers.cpp:101:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |                         for(int i=0; i<c.size()-1; i++){
      |                                      ~^~~~~~~~~~~
newspapers.cpp:118:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |                         for(int i=0; i<c.size()-1; i++){
      |                                      ~^~~~~~~~~~~
newspapers.cpp: In instantiation of 'main()::<lambda(long long int, long long int, auto:24&&)> [with auto:24 = main()::<lambda(long long int, long long int, auto:24&&)>&]':
newspapers.cpp:146:41:   required from here
newspapers.cpp:101:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |                         for(int i=0; i<c.size()-1; i++){
      |                                      ~^~~~~~~~~~~
newspapers.cpp:118:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |                         for(int i=0; i<c.size()-1; i++){
      |                                      ~^~~~~~~~~~~
newspapers.cpp:126:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |                             if(i!=c.size()-2)
      |                                ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Correct 0 ms 344 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 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 600 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 1 ms 452 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 600 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 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 344 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 0 ms 456 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 344 KB Output is correct
57 Correct 1 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 344 KB Output is correct
61 Correct 1 ms 444 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 76 ms 42412 KB Output is correct
12 Correct 22 ms 12372 KB Output is correct
13 Correct 30 ms 17492 KB Output is correct
14 Correct 34 ms 19292 KB Output is correct
15 Correct 38 ms 21840 KB Output is correct
16 Correct 97 ms 47184 KB Output is correct
17 Correct 82 ms 47188 KB Output is correct
18 Correct 83 ms 47188 KB Output is correct
19 Correct 80 ms 47412 KB Output is correct
20 Correct 88 ms 47476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Correct 0 ms 344 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 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 600 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 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 1 ms 452 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 600 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 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 344 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 0 ms 456 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 344 KB Output is correct
57 Correct 1 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 344 KB Output is correct
61 Correct 1 ms 444 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 452 KB Output is correct
67 Correct 0 ms 344 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 344 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 448 KB Output is correct
74 Correct 0 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 344 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 24 ms 532 KB Output is correct
80 Correct 27 ms 348 KB Output is correct
81 Correct 25 ms 528 KB Output is correct
82 Correct 36 ms 344 KB Output is correct
83 Correct 30 ms 508 KB Output is correct
84 Correct 25 ms 348 KB Output is correct
85 Correct 30 ms 532 KB Output is correct
86 Correct 26 ms 532 KB Output is correct
87 Correct 28 ms 348 KB Output is correct
88 Correct 23 ms 348 KB Output is correct
89 Correct 102 ms 40608 KB Output is correct
90 Correct 80 ms 31568 KB Output is correct
91 Correct 74 ms 29776 KB Output is correct
92 Correct 63 ms 25372 KB Output is correct
93 Correct 103 ms 39252 KB Output is correct
94 Correct 69 ms 27444 KB Output is correct
95 Correct 102 ms 42332 KB Output is correct
96 Correct 101 ms 40440 KB Output is correct
97 Correct 70 ms 24424 KB Output is correct
98 Correct 99 ms 39232 KB Output is correct
99 Correct 74 ms 30292 KB Output is correct
100 Correct 113 ms 32524 KB Output is correct
101 Correct 76 ms 29264 KB Output is correct
102 Correct 76 ms 30692 KB Output is correct
103 Correct 85 ms 32832 KB Output is correct
104 Correct 60 ms 23888 KB Output is correct
105 Correct 68 ms 26812 KB Output is correct
106 Correct 88 ms 35216 KB Output is correct
107 Correct 73 ms 27516 KB Output is correct
108 Correct 108 ms 40272 KB Output is correct
109 Correct 80 ms 32320 KB Output is correct
110 Correct 92 ms 31784 KB Output is correct
111 Correct 79 ms 26300 KB Output is correct
112 Correct 75 ms 28032 KB Output is correct
113 Correct 106 ms 41224 KB Output is correct
114 Correct 87 ms 34888 KB Output is correct
115 Correct 71 ms 28980 KB Output is correct
116 Correct 91 ms 33308 KB Output is correct
117 Correct 105 ms 42060 KB Output is correct
118 Correct 88 ms 34888 KB Output is correct
119 Correct 86 ms 30040 KB Output is correct
120 Correct 91 ms 36424 KB Output is correct
121 Correct 95 ms 37184 KB Output is correct
122 Correct 71 ms 29012 KB Output is correct
123 Correct 68 ms 26708 KB Output is correct
124 Correct 101 ms 40024 KB Output is correct
125 Correct 60 ms 24460 KB Output is correct
126 Correct 65 ms 26632 KB Output is correct
127 Correct 65 ms 25136 KB Output is correct
128 Correct 101 ms 39504 KB Output is correct
129 Correct 41 ms 504 KB Output is correct
130 Correct 40 ms 536 KB Output is correct
131 Correct 35 ms 600 KB Output is correct
132 Correct 35 ms 348 KB Output is correct
133 Correct 35 ms 344 KB Output is correct
134 Correct 11 ms 2904 KB Output is correct
135 Correct 12 ms 3164 KB Output is correct
136 Correct 27 ms 9052 KB Output is correct
137 Correct 17 ms 5212 KB Output is correct
138 Correct 34 ms 7340 KB Output is correct
139 Correct 52 ms 12112 KB Output is correct
140 Correct 8 ms 2140 KB Output is correct
141 Correct 4 ms 1112 KB Output is correct
142 Correct 47 ms 11600 KB Output is correct
143 Correct 62 ms 12408 KB Output is correct
144 Correct 82 ms 47440 KB Output is correct
145 Correct 81 ms 47448 KB Output is correct