답안 #810412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810412 2023-08-06T09:24:39 Z RecursiveCo Network (BOI15_net) C++14
0 / 100
0 ms 212 KB
// CF template, version 3.0

#include <bits/stdc++.h>

using namespace std;

#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}

#define int long long int

vector<vector<int>> subleaves;
vector<bool> visited;
vector<bool> leaf;
vector<vector<int>> adjList;

void dfs(int v, int p) {
    visited[v] = true;
    if (p != -1 && leaf[v]) subleaves[p].push_back(v);
    for (int con: adjList[v]) {
        if (!visited[con]) {
            dfs(con, p == -1? con: p);
        }
    }
}

signed main() {
    improvePerformance;

    get(n);
    adjList.resize(n);
    forto(n - 1, i) {
        get(a);
        get(b);
        a--;
        b--;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    leaf.resize(n, false);
    int leaves = 0;
    forto(n, i) {
        if (adjList[i].size() == 1) leaf[i] = true, leaves++;
    }
    // we choose to root the tree at some non-leaf
    int root = -1;
    forto(n, i) {
        if (adjList[i].size() != 1) {
            root = i;
            break;
        }
    }
    vector<pair<int, int>> edges;
    subleaves.resize(n);
    visited.resize(n, false);
    dfs(root, -1);
    vector<vector<int>> leafvecs;
    forto(n, i) {
        if (!subleaves[i].empty()) {
            leafvecs.push_back(subleaves[i]);
        }
    }
    for (auto& vec: leafvecs) {
        while (vec.size() > 2) {
            edges.push_back({vec.back(), vec[vec.size() - 2]});
            vec.pop_back();
            vec.pop_back();
        }
    }
    if (leaves % 2) {
        bool found = false;
        for (auto& vec: leafvecs) {
            if (vec.size() > 1) {
                found = true;
                int chosen = vec.back();
                vector<bool> taken(n, false);
                for (int con: adjList[chosen]) taken[con] = true;
                forto(n, i) {
                    if (!taken[i] && i != chosen) {
                        edges.push_back({chosen, i});
                        break;
                    }
                }
                vec.pop_back();
                break;
            }
        }
        if (!found) {
            for (auto& vec: leafvecs) {
                if (!vec.empty()) {
                    int chosen = vec.back();
                    vector<bool> taken(n, false);
                    for (int con: adjList[chosen]) taken[con] = true;
                    forto(n, i) {
                        if (!taken[i] && i != chosen) {
                            edges.push_back({chosen, i});
                            break;
                        }
                    }
                    vec.pop_back();
                    break;
                }
            }
        }
    }
    vector<vector<int>> ordered;
    int ones = 0;
    int twos = 0;
    for (auto vec: leafvecs) {
        if (vec.size() == 1) ordered.push_back(vec), ones++;
    }
    for (auto vec: leafvecs) {
        if (vec.size() == 2) ordered.push_back(vec), twos++;
    }
    if (twos % 2) {
        if (ones == 0) {
            vector<int> all;
            for (auto vec: ordered) {
                all.push_back(vec[0]);
                all.push_back(vec[1]);
            }
            forto(twos, i) {
                edges.push_back({all[i], all[i + twos]});
            }
        } else {
            forto(ones / 2 - 1, i) {
                edges.push_back({ordered[2 * i][0], ordered[2 * i + 1][0]});
            }
            forto(twos / 2 - 1, i) {
                edges.push_back({ordered[ones + i + 1][0], ordered[ones + i + 2][0]});
                edges.push_back({ordered[ones + i + 1][1], ordered[ones + i + 2][1]});
            }
            edges.push_back({ordered[ones][0], ordered[ones - 1][0]});
            edges.push_back({ordered[ones][1], ordered[ones - 2][0]});
        }
    } else {
        forto(ones / 2, i) {
            edges.push_back({ordered[2 * i][0], ordered[2 * i + 1][0]});
        }
        forto(twos / 2, i) {
            edges.push_back({ordered[ones + i][0], ordered[ones + i + 1][0]});
            edges.push_back({ordered[ones + i][1], ordered[ones + i + 1][1]});
        }
    }
    out(edges.size());
    for (auto edge: edges) {
        out("\n");
        out(edge.first + 1);
        out(" ");
        out(edge.second + 1);
    }
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
net.cpp:38:5: note: in expansion of macro 'get'
   38 |     get(n);
      |     ^~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:40:5: note: in expansion of macro 'forto'
   40 |     forto(n - 1, i) {
      |     ^~~~~
net.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
net.cpp:41:9: note: in expansion of macro 'get'
   41 |         get(a);
      |         ^~~
net.cpp:10:23: warning: unnecessary parentheses in declaration of 'b' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
net.cpp:42:9: note: in expansion of macro 'get'
   42 |         get(b);
      |         ^~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:50:5: note: in expansion of macro 'forto'
   50 |     forto(n, i) {
      |     ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:55:5: note: in expansion of macro 'forto'
   55 |     forto(n, i) {
      |     ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:66:5: note: in expansion of macro 'forto'
   66 |     forto(n, i) {
      |     ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:86:17: note: in expansion of macro 'forto'
   86 |                 forto(n, i) {
      |                 ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:102:21: note: in expansion of macro 'forto'
  102 |                     forto(n, i) {
      |                     ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:130:13: note: in expansion of macro 'forto'
  130 |             forto(twos, i) {
      |             ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:134:13: note: in expansion of macro 'forto'
  134 |             forto(ones / 2 - 1, i) {
      |             ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:137:13: note: in expansion of macro 'forto'
  137 |             forto(twos / 2 - 1, i) {
      |             ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:145:9: note: in expansion of macro 'forto'
  145 |         forto(ones / 2, i) {
      |         ^~~~~
net.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
net.cpp:148:9: note: in expansion of macro 'forto'
  148 |         forto(twos / 2, i) {
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -