Submission #532855

# Submission time Handle Problem Language Result Execution time Memory
532855 2022-03-04T05:39:38 Z 79brue Newspapers (CEOI21_newspapers) C++14
100 / 100
98 ms 508 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

namespace Naive{
    int n, m;
    int dissappear[25];
    vector<int> link[25];

    int movemove(int source){
        int rv = 0;
        for(int i=0; i<n; i++){
            if(source & (1<<i)){
                for(int j=0; j<(int)link[i].size(); j++){
                    rv |= (1<<link[i][j]);
                }
            }
        }
    //    printf("movemove %d is %d\n", source, rv);
        return rv;
    }

    void bfs(){
        int temp;
        map<int, bool> visited;
        map<int, pair<int, int> > parent;
        queue<int> que;
        stack<int> ans;
        que.push((1<<n)-1);
        visited[(1<<n)-1] = true;
        while(1){
            if(que.empty()){
                printf("NO\n");
                return;
            }
            temp = que.front();
    //        printf("bfs %d\n", temp);
            que.pop();
            if(!temp) break;
            for(int i=0; i<n; i++){
                if(!(temp & (1<<i))) continue;
                int tmp_val = movemove(temp - (1<<i));
                if(!visited[tmp_val]){
                    que.push(tmp_val);
                    parent[tmp_val] = make_pair(temp, i);
    //                printf("parent of %d is %d(deleted %d)\n", tmp_val, temp, i);
                    visited[tmp_val] = true;
                }
            }
        }
        int cnt = 0, mark = 0;
        while(mark != (1<<n)-1){
            cnt++;
            ans.push(parent[mark].second);
            mark = parent[mark].first;
        }
        printf("YES\n%d\n", cnt);
        for(int i=0; i<cnt; i++){
            printf("%d ", ans.top()+1);
            ans.pop();
        }
        puts("");
    }

    void main(){
        int s, e;
        if(m!=n-1){
            puts("NO");
            exit(0);
        }
        for(int i=0; i<n; i++) link[i].clear();
        for(int i=1; i<=m; i++){
            scanf("%d %d", &s, &e);
            s--, e--;
            link[s].push_back(e), link[e].push_back(s);
        }
        for(int i=0; i<n; i++){
            dissappear[i] = 0;
            for(int j=0; j<(int)link[i].size(); j++){
                if((int)link[link[i][j]].size() == 1) dissappear[i] |= (1<<link[i][j]);
            }
        }
        bfs();
    }
}

int n, m;
vector<int> link[1002];
int maxDepth, maxIdx;
int p[1002];

int dfs(int x, int par = -1, int depth = 0){
    vector<int> vec (1, 0);
    p[x] = par;
    if(maxDepth < depth) maxIdx = x, maxDepth = depth;
    for(auto y: link[x]){
        if(y==par) continue;
        int tmp = dfs(y, x, depth+1);
        vec.push_back(tmp+1);
    }

    sort(vec.rbegin(), vec.rend());
    if(par == -1){
        if(vec.size() >= 3 && vec[2] >= 3){
            puts("NO");
            exit(0);
        }
    }
    return vec[0];
}

int depth[1002];
vector<int> line;
vector<vector<int> > lineChild;
vector<vector<int> > lineChildCnt;
vector<int> ans;

int main(){
    scanf("%d %d", &n, &m);
    if(n<=10){
        Naive::n = n;
        Naive::m = m;
        Naive::main();
        return 0;
    }

    if(m!=n-1){
        puts("NO");
        return 0;
    }

    for(int i=1; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y), link[y].push_back(x);
    }

    for(int i=1; i<=n; i++){
        dfs(i);
    }

    maxDepth = 0; dfs(1);
    int A = maxIdx;
    maxDepth = 0; dfs(A);
    int B = maxIdx;

    {
        int x = B;
        while(x != -1){
            line.push_back(x);
            depth[x] = 1;
            x = p[x];
        }
        reverse(line.begin(), line.end());
    }
    lineChild.resize(line.size());
    lineChildCnt.resize(line.size());

    for(int i=1; i<=n; i++){
        if(depth[i] == 1) continue;
        if(depth[p[i]] == 1){
            depth[i] = 2;
            int idx = find(line.begin(), line.end(), p[i]) - line.begin();
            lineChild[idx].push_back(i);
            lineChildCnt[idx].push_back(0);
        }
    }
    for(int i=1; i<=n; i++){
        if(depth[i] == 1 || depth[i] == 2) continue;
        if(depth[p[i]] == 2){
            depth[i] = 3;
            int idx = find(line.begin(), line.end(), p[p[i]]) - line.begin();
            lineChildCnt[idx][find(lineChild[idx].begin(), lineChild[idx].end(), p[i]) - lineChild[idx].begin()]++;
        }
    }

    for(int i=1; i<(int)line.size()-1; i++){
        ans.push_back(line[i]);
        for(int j=0; j<(int)lineChild[i].size(); j++){
            if(lineChildCnt[i][j]) ans.push_back(lineChild[i][j]), ans.push_back(line[i]);
        }
    }
    if(count(ans.begin(), ans.end(), ans.back()) >= 2) ans.pop_back();
    if(count(ans.begin(), ans.end(), ans[0]) >= 2) ans.erase(ans.begin());

    printf("YES\n%d\n", (int)ans.size()*2);
    for(auto x: ans) printf("%d ", x);
    if((int)ans.size() % 2 == 0){
        reverse(ans.begin(), ans.end());
    }
    for(auto x: ans) printf("%d ", x);
}

Compilation message

newspapers.cpp: In function 'void Naive::main()':
newspapers.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |             scanf("%d %d", &s, &e);
      |             ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp: In function 'int main()':
newspapers.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 320 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 0 ms 316 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 316 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 320 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 320 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 67 ms 496 KB Output is correct
12 Correct 22 ms 416 KB Output is correct
13 Correct 24 ms 452 KB Output is correct
14 Correct 28 ms 452 KB Output is correct
15 Correct 31 ms 428 KB Output is correct
16 Correct 68 ms 492 KB Output is correct
17 Correct 96 ms 504 KB Output is correct
18 Correct 68 ms 500 KB Output is correct
19 Correct 66 ms 492 KB Output is correct
20 Correct 63 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 320 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 0 ms 316 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 316 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 320 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 320 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 1 ms 324 KB Output is correct
66 Correct 1 ms 316 KB Output is correct
67 Correct 1 ms 332 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 320 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 1 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 1 ms 320 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 1 ms 360 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 332 KB Output is correct
86 Correct 2 ms 332 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 1 ms 332 KB Output is correct
89 Correct 86 ms 400 KB Output is correct
90 Correct 67 ms 396 KB Output is correct
91 Correct 69 ms 332 KB Output is correct
92 Correct 56 ms 396 KB Output is correct
93 Correct 92 ms 428 KB Output is correct
94 Correct 65 ms 388 KB Output is correct
95 Correct 94 ms 428 KB Output is correct
96 Correct 88 ms 408 KB Output is correct
97 Correct 49 ms 396 KB Output is correct
98 Correct 98 ms 428 KB Output is correct
99 Correct 69 ms 452 KB Output is correct
100 Correct 65 ms 332 KB Output is correct
101 Correct 59 ms 392 KB Output is correct
102 Correct 62 ms 400 KB Output is correct
103 Correct 86 ms 416 KB Output is correct
104 Correct 55 ms 384 KB Output is correct
105 Correct 55 ms 392 KB Output is correct
106 Correct 70 ms 416 KB Output is correct
107 Correct 64 ms 396 KB Output is correct
108 Correct 88 ms 404 KB Output is correct
109 Correct 75 ms 332 KB Output is correct
110 Correct 68 ms 392 KB Output is correct
111 Correct 77 ms 392 KB Output is correct
112 Correct 72 ms 408 KB Output is correct
113 Correct 92 ms 416 KB Output is correct
114 Correct 75 ms 420 KB Output is correct
115 Correct 58 ms 332 KB Output is correct
116 Correct 70 ms 408 KB Output is correct
117 Correct 86 ms 412 KB Output is correct
118 Correct 78 ms 332 KB Output is correct
119 Correct 61 ms 396 KB Output is correct
120 Correct 79 ms 420 KB Output is correct
121 Correct 86 ms 408 KB Output is correct
122 Correct 62 ms 412 KB Output is correct
123 Correct 53 ms 388 KB Output is correct
124 Correct 89 ms 416 KB Output is correct
125 Correct 55 ms 332 KB Output is correct
126 Correct 55 ms 400 KB Output is correct
127 Correct 50 ms 404 KB Output is correct
128 Correct 88 ms 404 KB Output is correct
129 Correct 1 ms 332 KB Output is correct
130 Correct 1 ms 332 KB Output is correct
131 Correct 1 ms 332 KB Output is correct
132 Correct 1 ms 332 KB Output is correct
133 Correct 1 ms 332 KB Output is correct
134 Correct 1 ms 204 KB Output is correct
135 Correct 1 ms 204 KB Output is correct
136 Correct 1 ms 324 KB Output is correct
137 Correct 0 ms 324 KB Output is correct
138 Correct 1 ms 204 KB Output is correct
139 Correct 0 ms 332 KB Output is correct
140 Correct 1 ms 332 KB Output is correct
141 Correct 1 ms 328 KB Output is correct
142 Correct 1 ms 332 KB Output is correct
143 Correct 1 ms 332 KB Output is correct
144 Correct 66 ms 508 KB Output is correct
145 Correct 86 ms 496 KB Output is correct