Submission #602945

# Submission time Handle Problem Language Result Execution time Memory
602945 2022-07-23T13:01:54 Z cadmiumsky Newspapers (CEOI21_newspapers) C++14
100 / 100
214 ms 4572 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(state == 0) {
    commence(heavy[node], 0);
    rez.push_back(node);
    for(auto x : light[node])
      //cerr << x << ' ' << node << ' ',
      rez.push_back(x),
      rez.push_back(node);
    if(node != l)
      return;
    if(light[node].size())
      rez.pop_back();
    commence(node, 1);
    return;
  }
  if(state == 1) {
    if(node != l || light[node].size() == 0)
      rez.push_back(node);
    for(auto x : light[node])
      //cerr << x << ' ' << node << ' ',
      rez.push_back(x),
      rez.push_back(node);
    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};
  cnt = 0;
  for(auto x : g[node]) {
    if(x == f) continue;
    if(h[x] > 1)
      cnt++;
  }
  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;
    }
  }
  for(auto x : g[node]) {
    if(x == f)
      continue;
    if(h[x] > 1) {
      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) {
    cerr << "amog\n";
    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:131:1: warning: control reaches end of non-void function [-Wreturn-type]
  131 | }
      | ^
# 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 Correct 1 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 1 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 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 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 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 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 1 ms 360 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 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 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 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 Correct 1 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 1 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 1 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 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 0 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 0 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 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 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 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 31 ms 532 KB Output is correct
12 Correct 10 ms 452 KB Output is correct
13 Correct 11 ms 468 KB Output is correct
14 Correct 13 ms 468 KB Output is correct
15 Correct 16 ms 468 KB Output is correct
16 Correct 25 ms 540 KB Output is correct
17 Correct 27 ms 468 KB Output is correct
18 Correct 25 ms 540 KB Output is correct
19 Correct 24 ms 468 KB Output is correct
20 Correct 24 ms 532 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 Correct 1 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 1 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 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 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 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 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 1 ms 360 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 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 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 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 Correct 1 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 1 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 1 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 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 0 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 0 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 Correct 1 ms 340 KB Output is correct
71 Correct 0 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 1 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 0 ms 340 KB Output is correct
77 Correct 1 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 1 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 1 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 37 ms 452 KB Output is correct
90 Correct 29 ms 468 KB Output is correct
91 Correct 33 ms 468 KB Output is correct
92 Correct 23 ms 440 KB Output is correct
93 Correct 39 ms 464 KB Output is correct
94 Correct 26 ms 444 KB Output is correct
95 Correct 37 ms 472 KB Output is correct
96 Correct 37 ms 468 KB Output is correct
97 Correct 24 ms 460 KB Output is correct
98 Correct 44 ms 468 KB Output is correct
99 Correct 33 ms 460 KB Output is correct
100 Correct 30 ms 456 KB Output is correct
101 Correct 26 ms 444 KB Output is correct
102 Correct 27 ms 460 KB Output is correct
103 Correct 29 ms 460 KB Output is correct
104 Correct 24 ms 432 KB Output is correct
105 Correct 24 ms 448 KB Output is correct
106 Correct 31 ms 448 KB Output is correct
107 Correct 24 ms 444 KB Output is correct
108 Correct 35 ms 460 KB Output is correct
109 Correct 29 ms 468 KB Output is correct
110 Correct 29 ms 440 KB Output is correct
111 Correct 26 ms 460 KB Output is correct
112 Correct 27 ms 448 KB Output is correct
113 Correct 36 ms 460 KB Output is correct
114 Correct 30 ms 452 KB Output is correct
115 Correct 26 ms 460 KB Output is correct
116 Correct 37 ms 448 KB Output is correct
117 Correct 45 ms 460 KB Output is correct
118 Correct 32 ms 468 KB Output is correct
119 Correct 28 ms 444 KB Output is correct
120 Correct 33 ms 456 KB Output is correct
121 Correct 36 ms 444 KB Output is correct
122 Correct 35 ms 448 KB Output is correct
123 Correct 25 ms 448 KB Output is correct
124 Correct 36 ms 460 KB Output is correct
125 Correct 22 ms 444 KB Output is correct
126 Correct 24 ms 448 KB Output is correct
127 Correct 23 ms 432 KB Output is correct
128 Correct 36 ms 464 KB Output is correct
129 Correct 1 ms 340 KB Output is correct
130 Correct 1 ms 340 KB Output is correct
131 Correct 2 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 40 ms 1212 KB Output is correct
135 Correct 32 ms 1364 KB Output is correct
136 Correct 86 ms 3800 KB Output is correct
137 Correct 57 ms 2300 KB Output is correct
138 Correct 104 ms 2796 KB Output is correct
139 Correct 176 ms 4572 KB Output is correct
140 Correct 24 ms 976 KB Output is correct
141 Correct 8 ms 588 KB Output is correct
142 Correct 165 ms 4320 KB Output is correct
143 Correct 214 ms 4392 KB Output is correct
144 Correct 24 ms 536 KB Output is correct
145 Correct 27 ms 532 KB Output is correct