답안 #602933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
602933 2022-07-23T12:51:30 Z cadmiumsky Newspapers (CEOI21_newspapers) C++14
50 / 100
187 ms 4612 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);
  if(len == 1 && light[l].size() == 0) {
    cout << "YES\n2\n" << l << ' ' << r << '\n';
    return 0;
  }
  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);
        }
      }
    }
  }
  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 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
4 Correct 1 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 0 ms 340 KB Output is correct
8 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
9 Correct 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
15 Correct 1 ms 352 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 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 1 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 344 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 0 ms 352 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 352 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 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 1 ms 340 KB Output is correct
39 Correct 1 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 1 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 1 ms 344 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 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 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 352 KB Output is correct
57 Correct 1 ms 352 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 356 KB Output is correct
3 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
4 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
5 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 356 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 23 ms 524 KB Output is correct
12 Correct 5 ms 448 KB Output is correct
13 Correct 7 ms 468 KB Output is correct
14 Correct 8 ms 476 KB Output is correct
15 Correct 9 ms 468 KB Output is correct
16 Correct 18 ms 480 KB Output is correct
17 Correct 22 ms 596 KB Output is correct
18 Correct 17 ms 536 KB Output is correct
19 Correct 17 ms 524 KB Output is correct
20 Correct 18 ms 528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
4 Correct 1 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 0 ms 340 KB Output is correct
8 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
9 Correct 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
15 Correct 1 ms 352 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 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 1 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 344 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 0 ms 352 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 352 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 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 1 ms 340 KB Output is correct
39 Correct 1 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 1 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 1 ms 344 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 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 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 352 KB Output is correct
57 Correct 1 ms 352 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 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 Failed to provide a successful strategy.
68 Correct 1 ms 352 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Partially correct 1 ms 348 KB Failed to provide a successful strategy.
71 Correct 1 ms 340 KB Output is correct
72 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
73 Correct 1 ms 352 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 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
79 Correct 1 ms 340 KB Output is correct
80 Correct 1 ms 356 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 1 ms 356 KB Output is correct
83 Correct 1 ms 356 KB Output is correct
84 Correct 2 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 1 ms 360 KB Output is correct
87 Correct 2 ms 352 KB Output is correct
88 Correct 2 ms 340 KB Output is correct
89 Correct 30 ms 452 KB Output is correct
90 Correct 20 ms 448 KB Output is correct
91 Correct 19 ms 452 KB Output is correct
92 Correct 18 ms 340 KB Output is correct
93 Correct 27 ms 456 KB Output is correct
94 Correct 23 ms 432 KB Output is correct
95 Correct 34 ms 468 KB Output is correct
96 Correct 27 ms 468 KB Output is correct
97 Correct 15 ms 440 KB Output is correct
98 Correct 28 ms 460 KB Output is correct
99 Correct 24 ms 352 KB Output is correct
100 Correct 21 ms 452 KB Output is correct
101 Correct 18 ms 468 KB Output is correct
102 Correct 20 ms 456 KB Output is correct
103 Correct 21 ms 436 KB Output is correct
104 Correct 15 ms 340 KB Output is correct
105 Correct 18 ms 352 KB Output is correct
106 Correct 24 ms 352 KB Output is correct
107 Correct 19 ms 444 KB Output is correct
108 Correct 26 ms 460 KB Output is correct
109 Correct 21 ms 452 KB Output is correct
110 Correct 23 ms 440 KB Output is correct
111 Correct 18 ms 440 KB Output is correct
112 Correct 20 ms 440 KB Output is correct
113 Correct 31 ms 460 KB Output is correct
114 Correct 23 ms 436 KB Output is correct
115 Correct 18 ms 468 KB Output is correct
116 Correct 22 ms 440 KB Output is correct
117 Correct 28 ms 460 KB Output is correct
118 Correct 24 ms 444 KB Output is correct
119 Correct 22 ms 452 KB Output is correct
120 Correct 25 ms 452 KB Output is correct
121 Correct 28 ms 460 KB Output is correct
122 Correct 18 ms 452 KB Output is correct
123 Correct 17 ms 340 KB Output is correct
124 Correct 30 ms 444 KB Output is correct
125 Correct 16 ms 444 KB Output is correct
126 Correct 19 ms 356 KB Output is correct
127 Correct 22 ms 436 KB Output is correct
128 Correct 26 ms 468 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 424 KB Output is correct
134 Correct 40 ms 1356 KB Output is correct
135 Correct 32 ms 1484 KB Output is correct
136 Correct 96 ms 3996 KB Output is correct
137 Correct 57 ms 2424 KB Output is correct
138 Correct 95 ms 2920 KB Output is correct
139 Correct 170 ms 4612 KB Output is correct
140 Correct 28 ms 1112 KB Output is correct
141 Correct 8 ms 724 KB Output is correct
142 Correct 168 ms 4536 KB Output is correct
143 Correct 187 ms 4504 KB Output is correct
144 Correct 22 ms 528 KB Output is correct
145 Correct 19 ms 532 KB Output is correct