답안 #602928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
602928 2022-07-23T12:45:19 Z cadmiumsky Newspapers (CEOI21_newspapers) C++14
50 / 100
188 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(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])
      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])
      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};
  if(r == l) {
    cout << "YES\n2\n" << l << ' ' << r << '\n';
    return 0;
  }
  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) {
          cerr << "futui " << x << ' ' << i << '\n';
          cout << "NO\n";
          return 0;
        }
        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:118:1: warning: control reaches end of non-void function [-Wreturn-type]
  118 | }
      | ^
# 결과 실행 시간 메모리 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 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 0 ms 340 KB Output is correct
10 Correct 1 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 340 KB Output is correct
16 Correct 1 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 360 KB Output is correct
20 Correct 0 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 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 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 368 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 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 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 0 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 1 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 0 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 0 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 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 0 ms 340 KB Failed to provide a successful strategy.
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 0 ms 340 KB Output is correct
11 Correct 31 ms 468 KB Output is correct
12 Correct 17 ms 456 KB Output is correct
13 Correct 15 ms 468 KB Output is correct
14 Correct 16 ms 468 KB Output is correct
15 Correct 18 ms 484 KB Output is correct
16 Correct 32 ms 468 KB Output is correct
17 Correct 30 ms 536 KB Output is correct
18 Correct 33 ms 548 KB Output is correct
19 Correct 31 ms 540 KB Output is correct
20 Correct 40 ms 532 KB Output is correct
# 결과 실행 시간 메모리 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 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 0 ms 340 KB Output is correct
10 Correct 1 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 340 KB Output is correct
16 Correct 1 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 360 KB Output is correct
20 Correct 0 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 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 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 368 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 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 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 0 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 1 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 0 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 0 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 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
68 Correct 1 ms 340 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Partially correct 1 ms 340 KB Failed to provide a successful strategy.
71 Correct 1 ms 340 KB Output is correct
72 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
73 Correct 1 ms 340 KB Output is correct
74 Correct 0 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 1 ms 340 KB Output is correct
77 Correct 1 ms 276 KB Output is correct
78 Partially correct 0 ms 340 KB Failed to provide a successful strategy.
79 Correct 1 ms 352 KB Output is correct
80 Correct 1 ms 340 KB Output is correct
81 Correct 2 ms 360 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 468 KB Output is correct
88 Correct 1 ms 340 KB Output is correct
89 Correct 40 ms 456 KB Output is correct
90 Correct 40 ms 448 KB Output is correct
91 Correct 32 ms 452 KB Output is correct
92 Correct 34 ms 448 KB Output is correct
93 Correct 42 ms 468 KB Output is correct
94 Correct 29 ms 580 KB Output is correct
95 Correct 41 ms 484 KB Output is correct
96 Correct 40 ms 468 KB Output is correct
97 Correct 27 ms 448 KB Output is correct
98 Correct 41 ms 460 KB Output is correct
99 Correct 32 ms 460 KB Output is correct
100 Correct 34 ms 456 KB Output is correct
101 Correct 31 ms 488 KB Output is correct
102 Correct 32 ms 572 KB Output is correct
103 Correct 36 ms 464 KB Output is correct
104 Correct 25 ms 468 KB Output is correct
105 Correct 28 ms 440 KB Output is correct
106 Correct 43 ms 452 KB Output is correct
107 Correct 29 ms 456 KB Output is correct
108 Correct 40 ms 464 KB Output is correct
109 Correct 33 ms 448 KB Output is correct
110 Correct 33 ms 452 KB Output is correct
111 Correct 28 ms 444 KB Output is correct
112 Correct 33 ms 484 KB Output is correct
113 Correct 49 ms 468 KB Output is correct
114 Correct 36 ms 468 KB Output is correct
115 Correct 34 ms 444 KB Output is correct
116 Correct 34 ms 452 KB Output is correct
117 Correct 43 ms 468 KB Output is correct
118 Correct 37 ms 460 KB Output is correct
119 Correct 35 ms 452 KB Output is correct
120 Correct 41 ms 448 KB Output is correct
121 Correct 37 ms 464 KB Output is correct
122 Correct 31 ms 464 KB Output is correct
123 Correct 33 ms 444 KB Output is correct
124 Correct 40 ms 468 KB Output is correct
125 Correct 26 ms 436 KB Output is correct
126 Correct 27 ms 460 KB Output is correct
127 Correct 26 ms 444 KB Output is correct
128 Correct 39 ms 476 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 372 KB Output is correct
134 Correct 39 ms 1288 KB Output is correct
135 Correct 38 ms 1372 KB Output is correct
136 Correct 83 ms 3820 KB Output is correct
137 Correct 54 ms 2228 KB Output is correct
138 Correct 93 ms 2800 KB Output is correct
139 Correct 188 ms 4572 KB Output is correct
140 Correct 24 ms 1092 KB Output is correct
141 Correct 8 ms 596 KB Output is correct
142 Correct 152 ms 4460 KB Output is correct
143 Correct 186 ms 4420 KB Output is correct
144 Correct 34 ms 532 KB Output is correct
145 Correct 31 ms 552 KB Output is correct