Submission #698704

# Submission time Handle Problem Language Result Execution time Memory
698704 2023-02-14T08:23:20 Z cig32 Newspapers (CEOI21_newspapers) C++17
75 / 100
67 ms 8232 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int n, m;
vector<int> adj[1029];
int dp[1029];
void dpf(int node, int prv) {
  dp[node] = 1;
  for(int x: adj[node]) {
    if(adj[x].size() == 1) continue;
    if(x == prv) continue;
    dpf(x, node);
    dp[node] += dp[x];
  }
}
vector<int> dfs(int node, int prv) {
  int ct=0;
  vector<pair<int, int> > vt;
  for(int x: adj[node]) {
    if(x != prv && adj[x].size() > 1) {
      ct += (dp[x] > 1);
      vt.push_back({dp[x], x});
    }
  }
  if(ct > 2) {
    return {-1};
  }
  if(prv != -1 && ct > 1) {
    return {-1};
  }
  sort(vt.begin(), vt.end());
  if(vt.empty()) {
    return {node};
  }
  int to = vt[vt.size() - 1].second;
  vector<int> res;
  res = dfs(to, node);
  if(res[0] == to) reverse(res.begin(), res.end());
  res.push_back(node);
  for(int i=0; i+1<vt.size(); i++) {
    to = vt[i].second;
    vector<int> get = dfs(to, node);
    if(get[0] != to) reverse(get.begin(), get.end());
    for(int x: get) res.push_back(x);
    if(!(prv == -1 && i+2 == vt.size()))res.push_back(node);
  }
  for(int x: res) if(x == -1) return {-1};
  return res;
}
void solve(int tc) {
  cin >> n >> m;
  for(int i=0; i<m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(m != n-1) {
    cout << "NO\n"; return;
  }
  if(n == 2) {
    cout << "YES\n2\n1 1\n"; return;
  }
  if(n == 1) {
    cout << "YES\n1\n1\n"; return;
  }
  for(int rt=1; rt<=n; rt++) {
    if(adj[rt].size() > 1) {
      for(int i=1; i<=n; i++) dp[i] = 0;
      dpf(rt, -1);
      vector<int> seq = dfs(rt, -1);
      if(seq[0] != -1) {
        cout << "YES\n";
        cout << seq.size() * 3 << "\n";
        for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
        for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
        for(int i=seq.size()-1; i>=0; i--) cout << seq[i] << ' ';
        cout << "\n"; return;
      }
    }
  }
  cout << "NO\n";
}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}

Compilation message

newspapers.cpp: In function 'std::vector<int> dfs(int, int)':
newspapers.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for(int i=0; i+1<vt.size(); i++) {
      |                ~~~^~~~~~~~~~
newspapers.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     if(!(prv == -1 && i+2 == vt.size()))res.push_back(node);
      |                       ~~~~^~~~~~~~~~~~
newspapers.cpp: In function 'void solve(int)':
newspapers.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
      |                      ~^~~~~~~~~~~
newspapers.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |         for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
      |                      ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
4 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
5 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
6 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
7 Correct 1 ms 340 KB Output is correct
8 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
9 Correct 1 ms 340 KB Output is correct
10 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
11 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
12 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
13 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
14 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
15 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
16 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
17 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
18 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
19 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
20 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
21 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
22 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
23 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
24 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
25 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
26 Partially correct 1 ms 292 KB Provide a successful but not optimal strategy.
27 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
28 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
29 Correct 1 ms 340 KB Output is correct
30 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
31 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
32 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
33 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
34 Correct 0 ms 340 KB Output is correct
35 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
36 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
37 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
38 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
39 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
40 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
41 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
42 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
43 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
44 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
52 Correct 0 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
4 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
5 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
6 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
7 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
8 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
9 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
10 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
11 Partially correct 2 ms 596 KB Provide a successful but not optimal strategy.
12 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
13 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
14 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
15 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
16 Partially correct 2 ms 596 KB Provide a successful but not optimal strategy.
17 Partially correct 2 ms 596 KB Provide a successful but not optimal strategy.
18 Partially correct 2 ms 596 KB Provide a successful but not optimal strategy.
19 Partially correct 1 ms 596 KB Provide a successful but not optimal strategy.
20 Partially correct 1 ms 596 KB Provide a successful but not optimal strategy.
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
4 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
5 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
6 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
7 Correct 1 ms 340 KB Output is correct
8 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
9 Correct 1 ms 340 KB Output is correct
10 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
11 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
12 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
13 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
14 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
15 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
16 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
17 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
18 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
19 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
20 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
21 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
22 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
23 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
24 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
25 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
26 Partially correct 1 ms 292 KB Provide a successful but not optimal strategy.
27 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
28 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
29 Correct 1 ms 340 KB Output is correct
30 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
31 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
32 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
33 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
34 Correct 0 ms 340 KB Output is correct
35 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
36 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
37 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
38 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
39 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
40 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
41 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
42 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
43 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
44 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
52 Correct 0 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Partially correct 0 ms 340 KB Provide a successful but not optimal strategy.
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
68 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
69 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
70 Partially correct 2 ms 340 KB Provide a successful but not optimal strategy.
71 Correct 1 ms 340 KB Output is correct
72 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
73 Correct 1 ms 340 KB Output is correct
74 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
75 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
76 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
77 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
78 Partially correct 1 ms 340 KB Provide a successful but not optimal strategy.
79 Correct 4 ms 340 KB Output is correct
80 Correct 4 ms 340 KB Output is correct
81 Correct 7 ms 340 KB Output is correct
82 Correct 5 ms 360 KB Output is correct
83 Correct 7 ms 356 KB Output is correct
84 Correct 5 ms 340 KB Output is correct
85 Correct 4 ms 340 KB Output is correct
86 Correct 7 ms 356 KB Output is correct
87 Correct 4 ms 340 KB Output is correct
88 Correct 5 ms 340 KB Output is correct
89 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
90 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
91 Partially correct 2 ms 472 KB Provide a successful but not optimal strategy.
92 Partially correct 1 ms 476 KB Provide a successful but not optimal strategy.
93 Partially correct 1 ms 472 KB Provide a successful but not optimal strategy.
94 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
95 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
96 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
97 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
98 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
99 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
100 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
101 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
102 Partially correct 1 ms 484 KB Provide a successful but not optimal strategy.
103 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
104 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
105 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
106 Partially correct 1 ms 520 KB Provide a successful but not optimal strategy.
107 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
108 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
109 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
110 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
111 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
112 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
113 Partially correct 1 ms 484 KB Provide a successful but not optimal strategy.
114 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
115 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
116 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
117 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
118 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
119 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
120 Partially correct 2 ms 468 KB Provide a successful but not optimal strategy.
121 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
122 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
123 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
124 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
125 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
126 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
127 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
128 Partially correct 1 ms 468 KB Provide a successful but not optimal strategy.
129 Correct 6 ms 340 KB Output is correct
130 Correct 5 ms 340 KB Output is correct
131 Correct 6 ms 340 KB Output is correct
132 Correct 7 ms 340 KB Output is correct
133 Correct 7 ms 340 KB Output is correct
134 Correct 14 ms 2012 KB Output is correct
135 Correct 21 ms 1864 KB Output is correct
136 Correct 43 ms 5468 KB Output is correct
137 Correct 23 ms 3272 KB Output is correct
138 Correct 38 ms 4716 KB Output is correct
139 Correct 67 ms 7880 KB Output is correct
140 Correct 9 ms 1492 KB Output is correct
141 Correct 4 ms 724 KB Output is correct
142 Correct 59 ms 7540 KB Output is correct
143 Correct 61 ms 8232 KB Output is correct
144 Partially correct 1 ms 596 KB Provide a successful but not optimal strategy.
145 Partially correct 2 ms 612 KB Provide a successful but not optimal strategy.