Submission #602934

# Submission time Handle Problem Language Result Execution time Memory
602934 2022-07-23T12:52:36 Z cadmiumsky Newspapers (CEOI21_newspapers) C++14
50 / 100
189 ms 4480 KB
#include <bits/stdc++.h>

using namespace std;
using tii = tuple<int,int,int>;
using pii = pair<int,int>;
const int inf = 1e6 + 5, nmax = 1e3 + 5;

vector<int> g[nmax], light[nmax];
int h[nmax], atrleaf[nmax], heavy[nmax];
int ondiam[nmax];

void chmax(tii &l, tii r) {
  l = max(l, r);
}

tii finddiam(int node, int f) {
  tii rez = {1, node, node};
  atrleaf[node] = node;
  h[node] = 1;
  for(auto x : g[node]) {
    if(x == f) continue;
    chmax(rez, finddiam(x, node));
    if(h[x] + 1 > h[node]) {
      h[node] = h[x] + 1;
      atrleaf[node] = atrleaf[x];
    }
  }
  int other = 0, atr = node;
  for(auto x : g[node]) {
    if(x == f || atrleaf[node] == atrleaf[x])
      continue;
    if(h[x] > other) {
      other = h[x];
      atr = atrleaf[x];
    }
  }
  chmax(rez, tii{other + h[node], atrleaf[node], atr});
  return rez;
  
}



static bool markdiam(int node, int f, int rez) {
  if(node == rez) {
    heavy[node] = -1;
    ondiam[node] = 1;
    return 1;
  }
  ondiam[node] = 1;
  for(auto x : g[node]) {
    if(x == f)
      continue;
    if(markdiam(x, node, rez)) {
      heavy[node] = x;
      return 1;
    }
  }
  ondiam[node] = 0;
  return 0;
}
vector<int> rez;
  int len, l, r;
void commence(int node, bool state = 0) {
  if(node == -1)
    return;
  if(heavy[node] == -1 && light[node].size() == 0)
    return;
  if(state == 0) {
    commence(heavy[node], 0);
    if(heavy[node] != -1)
      rez.push_back(node);
    for(auto x : light[node])
      //cerr << x << ' ' << node << ' ',
      rez.push_back(x),
      rez.push_back(node);
    if(node != l)
      return;
    rez.pop_back();
    commence(node, 1);
    return;
  }
  if(state == 1) {
    if(node != l)
      rez.push_back(node);
    for(auto x : light[node])
      //cerr << x << ' ' << node << ' ',
      rez.push_back(x),
      rez.push_back(node);
    if(heavy[node] == -1)
      rez.pop_back();
    commence(heavy[node], 1);
    
    return;
  }
  
  
}

pii findend(int node, int f) {
  int cnt = 0;
  for(auto x : g[node]) {
    if(x == f) continue;
    if(h[x] > 2)
      cnt++;
  }
  if(cnt >= 2)
    return {-1, inf};
  if(cnt == 0)
    return {node, 1};
  for(auto x : g[node]) {
    if(x == f)
      continue;
    if(h[x] > 2) {
      auto tmp = findend(x, node);
      tmp.second++;
      return tmp;
    }
  }
}

int main() {  
  int n, m;
  cin >> n >> m;
  if(n == 1) {
    cout << "YES\n1\n1\n";
    return 0;
  }
  for(int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  if(m >= n) {
    cout << "NO\n";
    return 0;
  }
  tie(len, l, r) = finddiam(1, 1); 
  markdiam(l, l, r);
  finddiam(l, l);
  for(int i = 1; i <= n; i++) {
    if(ondiam[i] == 1) {
      for(auto x : g[i]) {
        if(ondiam[x])
          continue;
        if(h[x] > 2) {
          cout << "NO\n";
          return 0;
        }
      }
    }
  }
  for(int i = 1; i <= n; i++)
    ondiam[i] = 0;
  
  int mn = len, mnl = l, mnr = r;
  for(int i = 1; i <= n; i++) {
    finddiam(i, i);
    tie(r, len) = findend(i, i);
    //cerr << i << ": " << r << ' ' << len << '\n';
    if(len < mn) {
      mn = len;
      mnl = i;
      mnr = r;
    }
    //cerr << mnl << ": " << mnr << ' ' << mn << '\n';
  }
  
  tie(l, r, len ) = tii{mnl, mnr, mn};
  markdiam(l, l, r);
  finddiam(l, l);
  for(int i = 1; i <= n; i++) {
    if(ondiam[i] == 1) {
      //cerr << "ceva\n";
      for(auto x : g[i]) {
        if(ondiam[x])
          continue;
        if(h[x] > 2) {
          cout << "NO\n";
          return 0;
        }
        //cerr << i << ' ' << x << ' ' << h[x] << '\n';
        
        if(h[x] == 2) {
          light[i].push_back(x);
        }
      }
    }
  }
  if(len == 1 && light[l].size() == 0) {
    cout << "YES\n2\n" << l << ' ' << r << '\n';
    return 0;
  }
  commence(l);
  //cerr << l << ' ' << r<< '\n';
  cout << "YES\n" << rez.size() << '\n';
  for(auto x : rez)
    cout << x<< ' ';
  cout << '\n';
}
//4 1 6 1 2 3 3 2 1 6 1 4 

Compilation message

newspapers.cpp: In function 'pii findend(int, int)':
newspapers.cpp:120:1: warning: control reaches end of non-void function [-Wreturn-type]
  120 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 1 ms 240 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 0 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 0 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 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 17 ms 468 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
13 Correct 8 ms 468 KB Output is correct
14 Correct 8 ms 468 KB Output is correct
15 Correct 9 ms 468 KB Output is correct
16 Correct 19 ms 524 KB Output is correct
17 Correct 22 ms 520 KB Output is correct
18 Correct 19 ms 468 KB Output is correct
19 Correct 18 ms 528 KB Output is correct
20 Correct 19 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 1 ms 240 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 0 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 0 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 0 ms 340 KB Output is correct
66 Correct 0 ms 340 KB Output is correct
67 Correct 0 ms 340 KB Output is correct
68 Correct 0 ms 340 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
71 Correct 1 ms 340 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 0 ms 340 KB Output is correct
75 Correct 0 ms 340 KB Output is correct
76 Correct 0 ms 340 KB Output is correct
77 Correct 0 ms 340 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 1 ms 340 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 2 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 2 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 1 ms 340 KB Output is correct
89 Correct 31 ms 436 KB Output is correct
90 Correct 20 ms 444 KB Output is correct
91 Correct 20 ms 444 KB Output is correct
92 Correct 16 ms 340 KB Output is correct
93 Correct 30 ms 444 KB Output is correct
94 Correct 23 ms 340 KB Output is correct
95 Correct 27 ms 452 KB Output is correct
96 Correct 27 ms 448 KB Output is correct
97 Correct 18 ms 428 KB Output is correct
98 Correct 31 ms 436 KB Output is correct
99 Correct 21 ms 340 KB Output is correct
100 Correct 25 ms 468 KB Output is correct
101 Correct 19 ms 444 KB Output is correct
102 Correct 20 ms 340 KB Output is correct
103 Correct 21 ms 468 KB Output is correct
104 Correct 16 ms 424 KB Output is correct
105 Correct 17 ms 340 KB Output is correct
106 Correct 25 ms 444 KB Output is correct
107 Correct 22 ms 428 KB Output is correct
108 Correct 27 ms 456 KB Output is correct
109 Correct 21 ms 444 KB Output is correct
110 Correct 21 ms 444 KB Output is correct
111 Correct 17 ms 436 KB Output is correct
112 Correct 18 ms 440 KB Output is correct
113 Correct 29 ms 452 KB Output is correct
114 Correct 23 ms 436 KB Output is correct
115 Correct 22 ms 464 KB Output is correct
116 Correct 22 ms 440 KB Output is correct
117 Correct 28 ms 460 KB Output is correct
118 Correct 23 ms 468 KB Output is correct
119 Correct 20 ms 428 KB Output is correct
120 Correct 30 ms 440 KB Output is correct
121 Correct 25 ms 444 KB Output is correct
122 Correct 23 ms 340 KB Output is correct
123 Correct 18 ms 432 KB Output is correct
124 Correct 30 ms 460 KB Output is correct
125 Correct 15 ms 436 KB Output is correct
126 Correct 19 ms 440 KB Output is correct
127 Correct 16 ms 432 KB Output is correct
128 Correct 29 ms 452 KB Output is correct
129 Correct 1 ms 340 KB Output is correct
130 Correct 1 ms 340 KB Output is correct
131 Correct 1 ms 340 KB Output is correct
132 Correct 1 ms 340 KB Output is correct
133 Correct 1 ms 340 KB Output is correct
134 Correct 37 ms 1208 KB Output is correct
135 Correct 30 ms 1280 KB Output is correct
136 Correct 84 ms 3804 KB Output is correct
137 Correct 58 ms 2220 KB Output is correct
138 Correct 94 ms 2908 KB Output is correct
139 Correct 175 ms 4480 KB Output is correct
140 Correct 25 ms 1016 KB Output is correct
141 Correct 12 ms 588 KB Output is correct
142 Correct 174 ms 4420 KB Output is correct
143 Correct 189 ms 4344 KB Output is correct
144 Correct 18 ms 532 KB Output is correct
145 Correct 18 ms 528 KB Output is correct