Submission #604524

# Submission time Handle Problem Language Result Execution time Memory
604524 2022-07-25T07:16:15 Z Theo830 Newspapers (CEOI21_newspapers) C++17
100 / 100
83 ms 43764 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
bool ok = 1;
vector<vll>adj;
vll res;
ll c[1005] = {0};
void solve(ll idx,ll p){
    res.pb(idx+1);
    bool f = 1;
    ll posa = 0;
    for(auto x:adj[idx]){
        if(x != p){
            if(f){
                f = 0;
            }
            else{
                res.pb(idx+1);
            }
            c[x] = c[idx] ^ 1;
            solve(x,idx);
            posa += adj[x].size() != 1;
        }
    }
    if(posa > 1){
        ok = 0;
    }
}
bool cmp(ll a,ll b){
    return adj[a].size() < adj[b].size();
}
int main(void){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n,m;
    cin>>n>>m;
    adj.assign(n+5,vll());
    ll deg[n] = {0};
    vector<ii>ed;
    vll ans;
    f(i,0,5000){
        ans.pb(i);
    }
    f(i,0,m){
        ll a,b;
        cin>>a>>b;
        a--;
        b--;
        deg[a]++;
        deg[b]++;
        ed.pb(ii(a,b));
    }
    ok = m == n-1;
    vll w[n];
    if(ok){
        for(auto x:ed){
            if(deg[x.F] != 1 && deg[x.S] != 1){
                adj[x.F].pb(x.S);
                adj[x.S].pb(x.F);
            }
        }
        f(i,0,n){
            sort(all(adj[i]),cmp);
        }
        bool ivra = 0;
        f(i,0,n){
            f(j,0,5000){
                w[i].pb(j);
            }
        }
        f(i,0,n){
            if(deg[i] != 1){
                res.clear();
                ok = 1;
                solve(i,-1);
                if(ok){
                    w[i] = res;
                }
                if(ok && res.size() < ans.size()){
                    ans = res;
                }
                ivra |= ok;
            }
        }
        ok = ivra;
    }
    if(n == 1){
        cout<<"YES\n";
        cout<<"1\n 1\n";
    }
    else if(n == 2){
        cout<<"YES\n";
        cout<<"2\n2 2\n";
    }
    else if(ok){
        cout<<"YES\n";
        bool ek = (ans.size() % 2);
        f(i,0,n){
            if(c[i] != c[ans[0]-1] && w[i].size() == ans.size()){
                ek = 1;
                break;
            }
        }
        cout<<2 * ans.size() + (ek == 0)<<"\n";
        for(auto x:ans){
            cout<<x<<" ";
        }
        if(ans.size() % 2 == 0){
            ek = 0;
            f(i,0,n){
                if(c[i] != c[ans[0]-1] && w[i].size() == ans.size()){
                    ans = w[i];
                    ek = 1;
                    break;
                }
            }
            if(!ek){
                ans.insert(ans.begin(),1);
            }
        }
        for(auto x:ans){
            cout<<x<<" ";
        }
    }
    else{
        cout<<"NO\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 452 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 580 KB Output is correct
4 Correct 1 ms 708 KB Output is correct
5 Correct 1 ms 580 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 9 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 1 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 2 ms 836 KB Output is correct
22 Correct 1 ms 836 KB Output is correct
23 Correct 1 ms 960 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 2 ms 964 KB Output is correct
26 Correct 2 ms 964 KB Output is correct
27 Correct 1 ms 976 KB Output is correct
28 Correct 2 ms 964 KB Output is correct
29 Correct 2 ms 964 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 2 ms 980 KB Output is correct
33 Correct 1 ms 980 KB Output is correct
34 Correct 1 ms 980 KB Output is correct
35 Correct 2 ms 1108 KB Output is correct
36 Correct 2 ms 1108 KB Output is correct
37 Correct 1 ms 1108 KB Output is correct
38 Correct 2 ms 1108 KB Output is correct
39 Correct 1 ms 1092 KB Output is correct
40 Correct 2 ms 1088 KB Output is correct
41 Correct 1 ms 1108 KB Output is correct
42 Correct 1 ms 1108 KB Output is correct
43 Correct 1 ms 1108 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
45 Correct 2 ms 1108 KB Output is correct
46 Correct 2 ms 1116 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 2 ms 1240 KB Output is correct
49 Correct 2 ms 1228 KB Output is correct
50 Correct 2 ms 1248 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1248 KB Output is correct
53 Correct 2 ms 1232 KB Output is correct
54 Correct 10 ms 1248 KB Output is correct
55 Correct 2 ms 1248 KB Output is correct
56 Correct 2 ms 1248 KB Output is correct
57 Correct 12 ms 1260 KB Output is correct
58 Correct 1 ms 1248 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 352 KB Output is correct
61 Correct 1 ms 352 KB Output is correct
62 Correct 1 ms 336 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 5 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 7 ms 712 KB Output is correct
7 Correct 2 ms 712 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 836 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 52 ms 41436 KB Output is correct
12 Correct 25 ms 22684 KB Output is correct
13 Correct 38 ms 26700 KB Output is correct
14 Correct 30 ms 28372 KB Output is correct
15 Correct 32 ms 29908 KB Output is correct
16 Correct 51 ms 43512 KB Output is correct
17 Correct 51 ms 43592 KB Output is correct
18 Correct 65 ms 43696 KB Output is correct
19 Correct 53 ms 43712 KB Output is correct
20 Correct 53 ms 43676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 452 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 580 KB Output is correct
4 Correct 1 ms 708 KB Output is correct
5 Correct 1 ms 580 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 9 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 1 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 2 ms 836 KB Output is correct
22 Correct 1 ms 836 KB Output is correct
23 Correct 1 ms 960 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 2 ms 964 KB Output is correct
26 Correct 2 ms 964 KB Output is correct
27 Correct 1 ms 976 KB Output is correct
28 Correct 2 ms 964 KB Output is correct
29 Correct 2 ms 964 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 2 ms 980 KB Output is correct
33 Correct 1 ms 980 KB Output is correct
34 Correct 1 ms 980 KB Output is correct
35 Correct 2 ms 1108 KB Output is correct
36 Correct 2 ms 1108 KB Output is correct
37 Correct 1 ms 1108 KB Output is correct
38 Correct 2 ms 1108 KB Output is correct
39 Correct 1 ms 1092 KB Output is correct
40 Correct 2 ms 1088 KB Output is correct
41 Correct 1 ms 1108 KB Output is correct
42 Correct 1 ms 1108 KB Output is correct
43 Correct 1 ms 1108 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
45 Correct 2 ms 1108 KB Output is correct
46 Correct 2 ms 1116 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 2 ms 1240 KB Output is correct
49 Correct 2 ms 1228 KB Output is correct
50 Correct 2 ms 1248 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1248 KB Output is correct
53 Correct 2 ms 1232 KB Output is correct
54 Correct 10 ms 1248 KB Output is correct
55 Correct 2 ms 1248 KB Output is correct
56 Correct 2 ms 1248 KB Output is correct
57 Correct 12 ms 1260 KB Output is correct
58 Correct 1 ms 1248 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 352 KB Output is correct
61 Correct 1 ms 352 KB Output is correct
62 Correct 1 ms 336 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 1 ms 596 KB Output is correct
68 Correct 1 ms 708 KB Output is correct
69 Correct 1 ms 580 KB Output is correct
70 Correct 1 ms 724 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 724 KB Output is correct
73 Correct 1 ms 836 KB Output is correct
74 Correct 1 ms 852 KB Output is correct
75 Correct 1 ms 724 KB Output is correct
76 Correct 1 ms 724 KB Output is correct
77 Correct 1 ms 724 KB Output is correct
78 Correct 1 ms 712 KB Output is correct
79 Correct 34 ms 36172 KB Output is correct
80 Correct 32 ms 37196 KB Output is correct
81 Correct 34 ms 37568 KB Output is correct
82 Correct 37 ms 40884 KB Output is correct
83 Correct 37 ms 39116 KB Output is correct
84 Correct 36 ms 37464 KB Output is correct
85 Correct 35 ms 37460 KB Output is correct
86 Correct 34 ms 37976 KB Output is correct
87 Correct 36 ms 38988 KB Output is correct
88 Correct 31 ms 35204 KB Output is correct
89 Correct 43 ms 43080 KB Output is correct
90 Correct 38 ms 38092 KB Output is correct
91 Correct 37 ms 36684 KB Output is correct
92 Correct 32 ms 34028 KB Output is correct
93 Correct 42 ms 42472 KB Output is correct
94 Correct 35 ms 35696 KB Output is correct
95 Correct 43 ms 43736 KB Output is correct
96 Correct 42 ms 42912 KB Output is correct
97 Correct 34 ms 33304 KB Output is correct
98 Correct 44 ms 42712 KB Output is correct
99 Correct 41 ms 37200 KB Output is correct
100 Correct 42 ms 38244 KB Output is correct
101 Correct 38 ms 36688 KB Output is correct
102 Correct 40 ms 37068 KB Output is correct
103 Correct 40 ms 38644 KB Output is correct
104 Correct 32 ms 33008 KB Output is correct
105 Correct 37 ms 34792 KB Output is correct
106 Correct 42 ms 39676 KB Output is correct
107 Correct 37 ms 35356 KB Output is correct
108 Correct 44 ms 42836 KB Output is correct
109 Correct 40 ms 38252 KB Output is correct
110 Correct 40 ms 38208 KB Output is correct
111 Correct 35 ms 34716 KB Output is correct
112 Correct 35 ms 35668 KB Output is correct
113 Correct 46 ms 43136 KB Output is correct
114 Correct 73 ms 39688 KB Output is correct
115 Correct 36 ms 36000 KB Output is correct
116 Correct 40 ms 38996 KB Output is correct
117 Correct 44 ms 43468 KB Output is correct
118 Correct 40 ms 39892 KB Output is correct
119 Correct 65 ms 37340 KB Output is correct
120 Correct 42 ms 40972 KB Output is correct
121 Correct 42 ms 41044 KB Output is correct
122 Correct 35 ms 36412 KB Output is correct
123 Correct 61 ms 34848 KB Output is correct
124 Correct 43 ms 42864 KB Output is correct
125 Correct 33 ms 33216 KB Output is correct
126 Correct 62 ms 34612 KB Output is correct
127 Correct 34 ms 33804 KB Output is correct
128 Correct 43 ms 42700 KB Output is correct
129 Correct 42 ms 43556 KB Output is correct
130 Correct 42 ms 43620 KB Output is correct
131 Correct 45 ms 43656 KB Output is correct
132 Correct 41 ms 43568 KB Output is correct
133 Correct 46 ms 43596 KB Output is correct
134 Correct 13 ms 3272 KB Output is correct
135 Correct 12 ms 3172 KB Output is correct
136 Correct 83 ms 5692 KB Output is correct
137 Correct 20 ms 5596 KB Output is correct
138 Correct 33 ms 6196 KB Output is correct
139 Correct 59 ms 10792 KB Output is correct
140 Correct 8 ms 1996 KB Output is correct
141 Correct 9 ms 1112 KB Output is correct
142 Correct 58 ms 10800 KB Output is correct
143 Correct 74 ms 12068 KB Output is correct
144 Correct 60 ms 43688 KB Output is correct
145 Correct 51 ms 43764 KB Output is correct