Submission #986152

# Submission time Handle Problem Language Result Execution time Memory
986152 2024-05-19T22:32:41 Z nextgenxing Newspapers (CEOI21_newspapers) C++14
6 / 100
3 ms 13404 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0R(i, x) FOR(i, 0, x)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define F0Rd(i, x) FORd(i, 0, x)
#define ckif(a, b, c) ((c) ? (a) : (b))
const int MAX_N = 2e5+69;
const ll MOD = 1000000007;
const ll INF = 1e18;

int n, m;
vector<int> adj[MAX_N];
int anc[MAX_N][20], dep[MAX_N], vis[MAX_N];
pii mx[MAX_N], mx2[MAX_N];
pair<int, pii> diam[MAX_N];

bool dfs(int curr, int par = -1){
    vis[curr] = 1, mx[curr] = {0, curr};
    for(auto x : adj[curr]){
        if(x == par) continue;
        if(vis[x]) return false;
        anc[x][0] = curr;
        dep[x] = dep[curr]+1;
        if(!dfs(x, curr)) return false;
        if(mx[x].ff+1 >= mx[curr].ff) mx2[curr] = mx[curr], mx[curr] = mx[x], mx[curr].ff++;
        else if(mx[x].ff+1 >= mx2[curr].ff) mx2[curr] = mx[x], mx2[curr].ff++;
        if(diam[x].ff >= diam[curr].ff) diam[curr] = diam[x];
    }
    if(mx[curr].ff+mx2[curr].ff >= diam[curr].ff) diam[curr] = {mx[curr].ff+mx2[curr].ff, {mx[curr].ss, mx2[curr].ss}};
    return true;
}

int lca(int a, int b){
    if(dep[a] > dep[b]) swap(a, b);
    F0Rd(i, 20) if(dep[a]+(1 << i) <= dep[b])
        b = anc[b][i];
    if(a == b) return a;
    F0Rd(i, 20) if(anc[a][i] != anc[b][i])
        a = anc[a][i], b = anc[b][i];
    return anc[a][0];
}

int dist(int a, int b){
    return dep[lca(a, b)]*2-dep[a]-dep[b];
}

int main(int argc, const char * argv[]){
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    cin >> n >> m;
    F0R(i, m){
        int a, b; cin >> a >> b; a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    if(!dfs(0)){
        cout << "NO\n";
        return 0;
    }
    FOR(d, 1, 20) F0R(i, n)
        anc[i][d] = anc[anc[i][d-1]][d-1];
    int a = diam[0].ss.ff, b = diam[0].ss.ss, c = lca(a, b);
    F0R(i, n){
        int x = c^lca(a, i)^lca(b, i);
        if(dist(i, x) > 2){
            cout << "NO\n";
            return 0;
        }
    }
    cout << "YES\n";
    if(dep[a] > dep[b]) swap(a, b);
    vector<int> seq, seq2;
    while(anc[a][0] != c){
        int tmp = a; a = anc[a][0];
        seq.push_back(a);
        for(auto x : adj[a]) if(x != tmp && x != anc[a][0])
            seq.push_back(x), seq.push_back(a);
    }
    while(anc[b][0] != c){
        int tmp = b; b = anc[b][0];
        seq2.push_back(b);
        for(auto x : adj[b]) if(x != tmp && x != anc[b][0])
            seq2.push_back(x), seq2.push_back(b);
    }
    if(b != c){
        seq.push_back(c);
        for(auto x : adj[c]) if(x != a && x != b)
            seq.push_back(x), seq.push_back(c);
        reverse(all(seq2));
        for(auto x : seq2) seq.push_back(x);
    }

    if(seq.size()%2) cout << 2*seq.size() << '\n';
    else cout << 2*seq.size()+1 << '\n';
    for(auto x : seq) cout << x+1 << ' ';
    if(seq.size()%2 == 0) cout << c+1 << ' ';
    for(auto x : seq) cout << x+1 << ' ';
    cout << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
4 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
5 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
6 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
7 Correct 2 ms 13148 KB Output is correct
8 Correct 2 ms 13148 KB Output is correct
9 Incorrect 2 ms 13144 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11508 KB Output is correct
2 Correct 2 ms 13144 KB Output is correct
3 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
4 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
5 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
6 Partially correct 2 ms 13240 KB Provide a successful but not optimal strategy.
7 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
8 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
9 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
10 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
11 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
12 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
13 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
14 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
15 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
16 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
17 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
18 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
19 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
20 Partially correct 3 ms 13404 KB Provide a successful but not optimal strategy.
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
4 Partially correct 2 ms 13148 KB Provide a successful but not optimal strategy.
5 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
6 Partially correct 2 ms 13144 KB Provide a successful but not optimal strategy.
7 Correct 2 ms 13148 KB Output is correct
8 Correct 2 ms 13148 KB Output is correct
9 Incorrect 2 ms 13144 KB Output isn't correct
10 Halted 0 ms 0 KB -