답안 #786020

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786020 2023-07-17T22:18:19 Z gagik_2007 Newspapers (CEOI21_newspapers) C++17
100 / 100
63 ms 15692 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ff first
#define ss second

ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=300007;
ll n,m,k;
vector<ll>g[N];
ll sz[N];
bool used[N];
ll head[N];

void dfs(int v, int par=-1){
    // cout<<"YOO"<<endl;
    sz[v]=0;
    for(int to:g[v]){
        if(to!=par){
            dfs(to,v);
            sz[v]=max(sz[v],sz[to]+1);
        }
    }
}

bool is_tree(int c, int v, int par=-1){
    used[v]=true;
    head[v]=c;
    bool retval=true;
    for(int to:g[v]){
        if(to!=par){
            if(used[to])retval= false;
            else if(!is_tree(c,to,v))retval= false;
        }
    }
    return retval;
}

void construct(vector<int>& p, int v, int par=-1){
    // cout<<"CONSTRUCT: "<<v<<" "<<par<<endl;
    dfs(v);
    // cout<<"SZ: ";
    // for(int vv=1;vv<=n;vv++)cout<<sz[vv]<<" ";
    // cout<<endl;
    p.push_back(v);
    int u=-1;
    for(int to:g[v]){
        if(to!=par){
            if(sz[to]==1){
                p.push_back(to);
                p.push_back(v);
            }
            else if(sz[to]!=0) u=to;
        }
    }
    if(u!=-1)construct(p,u,v);
}

int find_leaf(int v, int par=-1){
    dfs(v);
    for(int to:g[v]){
        if(to!=par){
            if(sz[to]>=2){
                return find_leaf(to,v);
            }
        }
    }
    for(int to:g[v]){
        if(sz[to]==1)return to;
    }
    return v;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("output.txt","w",stdout);
    // freopen("input.txt","r",stdin);
    cin>>n>>m;
    // if(m!=n-1)cout<<"NO\n";
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    if(n==1&&m==0){
        cout<<"YES 1 1\n";
        return 0;
    }
    for(int v=1;v<=n;v++){
        if(!used[v]&&!is_tree(v,v)){
            cout<<"NO\n";
            return 0;
        }
    }
    for(int v=1;v<=n;v++){
        dfs(v);
        int cnt=0;
        for(int to:g[v]){
            cnt+=(sz[to]>=2);
        }
        if(cnt>2){
            cout<<"NO\n";
            return 0;
        }
    }
    cout<<"YES\n";
    vector<int>p;
    int leaf=find_leaf(1);
    // cout<<"leaf: "<<leaf<<endl;
    construct(p,leaf);
    if(p.size()>=3&&p[p.size()-1]==p[p.size()-3])p.pop_back();
    int len=p.size();
    for(int i=len-1;i>=0;i--){
        p.push_back(p[i]);
    }
    cout<<p.size()<<endl;
    for(int x:p)cout<<x<<" ";
    cout<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 5 ms 7252 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7324 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7300 KB Output is correct
12 Correct 4 ms 7280 KB Output is correct
13 Correct 3 ms 7380 KB Output is correct
14 Correct 3 ms 7252 KB Output is correct
15 Correct 3 ms 7252 KB Output is correct
16 Correct 3 ms 7380 KB Output is correct
17 Correct 3 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7376 KB Output is correct
22 Correct 4 ms 7260 KB Output is correct
23 Correct 3 ms 7380 KB Output is correct
24 Correct 3 ms 7252 KB Output is correct
25 Correct 3 ms 7380 KB Output is correct
26 Correct 3 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 3 ms 7260 KB Output is correct
30 Correct 3 ms 7380 KB Output is correct
31 Correct 3 ms 7380 KB Output is correct
32 Correct 3 ms 7380 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 4 ms 7284 KB Output is correct
35 Correct 4 ms 7380 KB Output is correct
36 Correct 3 ms 7332 KB Output is correct
37 Correct 3 ms 7252 KB Output is correct
38 Correct 3 ms 7380 KB Output is correct
39 Correct 3 ms 7368 KB Output is correct
40 Correct 3 ms 7252 KB Output is correct
41 Correct 3 ms 7380 KB Output is correct
42 Correct 4 ms 7252 KB Output is correct
43 Correct 3 ms 7252 KB Output is correct
44 Correct 4 ms 7252 KB Output is correct
45 Correct 3 ms 7380 KB Output is correct
46 Correct 3 ms 7292 KB Output is correct
47 Correct 3 ms 7380 KB Output is correct
48 Correct 3 ms 7380 KB Output is correct
49 Correct 4 ms 7380 KB Output is correct
50 Correct 3 ms 7380 KB Output is correct
51 Correct 3 ms 7380 KB Output is correct
52 Correct 4 ms 7252 KB Output is correct
53 Correct 3 ms 7252 KB Output is correct
54 Correct 3 ms 7380 KB Output is correct
55 Correct 5 ms 7252 KB Output is correct
56 Correct 3 ms 7252 KB Output is correct
57 Correct 3 ms 7252 KB Output is correct
58 Correct 3 ms 7380 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 3 ms 7380 KB Output is correct
61 Correct 4 ms 7348 KB Output is correct
62 Correct 3 ms 7252 KB Output is correct
63 Correct 3 ms 7380 KB Output is correct
64 Correct 3 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7252 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7252 KB Output is correct
4 Correct 3 ms 7252 KB Output is correct
5 Correct 4 ms 7300 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 5 ms 7324 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 25 ms 7496 KB Output is correct
12 Correct 9 ms 7452 KB Output is correct
13 Correct 12 ms 7452 KB Output is correct
14 Correct 15 ms 7456 KB Output is correct
15 Correct 14 ms 7504 KB Output is correct
16 Correct 27 ms 7508 KB Output is correct
17 Correct 27 ms 7484 KB Output is correct
18 Correct 28 ms 7500 KB Output is correct
19 Correct 27 ms 7500 KB Output is correct
20 Correct 27 ms 7496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 5 ms 7252 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7324 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7300 KB Output is correct
12 Correct 4 ms 7280 KB Output is correct
13 Correct 3 ms 7380 KB Output is correct
14 Correct 3 ms 7252 KB Output is correct
15 Correct 3 ms 7252 KB Output is correct
16 Correct 3 ms 7380 KB Output is correct
17 Correct 3 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7252 KB Output is correct
21 Correct 3 ms 7376 KB Output is correct
22 Correct 4 ms 7260 KB Output is correct
23 Correct 3 ms 7380 KB Output is correct
24 Correct 3 ms 7252 KB Output is correct
25 Correct 3 ms 7380 KB Output is correct
26 Correct 3 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 3 ms 7260 KB Output is correct
30 Correct 3 ms 7380 KB Output is correct
31 Correct 3 ms 7380 KB Output is correct
32 Correct 3 ms 7380 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 4 ms 7284 KB Output is correct
35 Correct 4 ms 7380 KB Output is correct
36 Correct 3 ms 7332 KB Output is correct
37 Correct 3 ms 7252 KB Output is correct
38 Correct 3 ms 7380 KB Output is correct
39 Correct 3 ms 7368 KB Output is correct
40 Correct 3 ms 7252 KB Output is correct
41 Correct 3 ms 7380 KB Output is correct
42 Correct 4 ms 7252 KB Output is correct
43 Correct 3 ms 7252 KB Output is correct
44 Correct 4 ms 7252 KB Output is correct
45 Correct 3 ms 7380 KB Output is correct
46 Correct 3 ms 7292 KB Output is correct
47 Correct 3 ms 7380 KB Output is correct
48 Correct 3 ms 7380 KB Output is correct
49 Correct 4 ms 7380 KB Output is correct
50 Correct 3 ms 7380 KB Output is correct
51 Correct 3 ms 7380 KB Output is correct
52 Correct 4 ms 7252 KB Output is correct
53 Correct 3 ms 7252 KB Output is correct
54 Correct 3 ms 7380 KB Output is correct
55 Correct 5 ms 7252 KB Output is correct
56 Correct 3 ms 7252 KB Output is correct
57 Correct 3 ms 7252 KB Output is correct
58 Correct 3 ms 7380 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 3 ms 7380 KB Output is correct
61 Correct 4 ms 7348 KB Output is correct
62 Correct 3 ms 7252 KB Output is correct
63 Correct 3 ms 7380 KB Output is correct
64 Correct 3 ms 7380 KB Output is correct
65 Correct 5 ms 7252 KB Output is correct
66 Correct 3 ms 7252 KB Output is correct
67 Correct 3 ms 7252 KB Output is correct
68 Correct 3 ms 7252 KB Output is correct
69 Correct 3 ms 7380 KB Output is correct
70 Correct 3 ms 7380 KB Output is correct
71 Correct 3 ms 7252 KB Output is correct
72 Correct 4 ms 7252 KB Output is correct
73 Correct 3 ms 7380 KB Output is correct
74 Correct 4 ms 7380 KB Output is correct
75 Correct 3 ms 7380 KB Output is correct
76 Correct 5 ms 7252 KB Output is correct
77 Correct 3 ms 7252 KB Output is correct
78 Correct 4 ms 7380 KB Output is correct
79 Correct 3 ms 7380 KB Output is correct
80 Correct 4 ms 7380 KB Output is correct
81 Correct 3 ms 7380 KB Output is correct
82 Correct 4 ms 7380 KB Output is correct
83 Correct 4 ms 7388 KB Output is correct
84 Correct 5 ms 7380 KB Output is correct
85 Correct 3 ms 7380 KB Output is correct
86 Correct 3 ms 7380 KB Output is correct
87 Correct 3 ms 7380 KB Output is correct
88 Correct 3 ms 7380 KB Output is correct
89 Correct 18 ms 7508 KB Output is correct
90 Correct 14 ms 7508 KB Output is correct
91 Correct 15 ms 7464 KB Output is correct
92 Correct 12 ms 7380 KB Output is correct
93 Correct 15 ms 7508 KB Output is correct
94 Correct 12 ms 7508 KB Output is correct
95 Correct 19 ms 7500 KB Output is correct
96 Correct 22 ms 7468 KB Output is correct
97 Correct 16 ms 7380 KB Output is correct
98 Correct 15 ms 7468 KB Output is correct
99 Correct 14 ms 7460 KB Output is correct
100 Correct 13 ms 7508 KB Output is correct
101 Correct 16 ms 7460 KB Output is correct
102 Correct 15 ms 7436 KB Output is correct
103 Correct 20 ms 7460 KB Output is correct
104 Correct 12 ms 7452 KB Output is correct
105 Correct 13 ms 7508 KB Output is correct
106 Correct 17 ms 7452 KB Output is correct
107 Correct 14 ms 7448 KB Output is correct
108 Correct 18 ms 7468 KB Output is correct
109 Correct 20 ms 7460 KB Output is correct
110 Correct 16 ms 7508 KB Output is correct
111 Correct 11 ms 7508 KB Output is correct
112 Correct 13 ms 7508 KB Output is correct
113 Correct 24 ms 7472 KB Output is correct
114 Correct 19 ms 7500 KB Output is correct
115 Correct 13 ms 7508 KB Output is correct
116 Correct 18 ms 7472 KB Output is correct
117 Correct 19 ms 7472 KB Output is correct
118 Correct 18 ms 7504 KB Output is correct
119 Correct 12 ms 7508 KB Output is correct
120 Correct 16 ms 7464 KB Output is correct
121 Correct 14 ms 7508 KB Output is correct
122 Correct 14 ms 7460 KB Output is correct
123 Correct 12 ms 7508 KB Output is correct
124 Correct 24 ms 7448 KB Output is correct
125 Correct 12 ms 7508 KB Output is correct
126 Correct 13 ms 7444 KB Output is correct
127 Correct 11 ms 7448 KB Output is correct
128 Correct 16 ms 7472 KB Output is correct
129 Correct 3 ms 7380 KB Output is correct
130 Correct 4 ms 7380 KB Output is correct
131 Correct 3 ms 7380 KB Output is correct
132 Correct 3 ms 7380 KB Output is correct
133 Correct 4 ms 7380 KB Output is correct
134 Correct 15 ms 9300 KB Output is correct
135 Correct 13 ms 9476 KB Output is correct
136 Correct 28 ms 14284 KB Output is correct
137 Correct 29 ms 11248 KB Output is correct
138 Correct 32 ms 12460 KB Output is correct
139 Correct 50 ms 15692 KB Output is correct
140 Correct 10 ms 8800 KB Output is correct
141 Correct 10 ms 7912 KB Output is correct
142 Correct 63 ms 15568 KB Output is correct
143 Correct 54 ms 15520 KB Output is correct
144 Correct 27 ms 7500 KB Output is correct
145 Correct 27 ms 7508 KB Output is correct