Submission #527503

#TimeUsernameProblemLanguageResultExecution timeMemory
527503jalsolNetwork (BOI15_net)C++11
63 / 100
2089 ms41348 KiB
#include <bits/stdc++.h>

using namespace std;

#define Task ""

struct __Init__ {
    __Init__() {
        cin.tie(nullptr)->sync_with_stdio(false);
        if (fopen(Task".inp", "r")) {
            freopen(Task".inp", "r", stdin);
            freopen(Task".out", "w", stdout); }
    }
} __init__;

using ll = long long;

#ifdef LOCAL
#define debug(x) cerr << "[" #x " = " << x << "]\n";
#else
#define debug(...)
#endif // LOCAL

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)

template<class C> int isz(const C& c) { return c.size(); }
template<class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

constexpr int eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;

// =============================================================================

constexpr int maxN = 5e5 + 5;

struct Edge {
    int u, v;

    int other(int x) const {
        return u ^ v ^ x;
    }
};

int n;
vector<int> g[maxN];
Edge e[maxN];
bool isBridge[maxN];

vector<pair<int, int>> ans;
int d[maxN];
int trace[maxN];
queue<int> q;

void Bfs(int s) {
    For (i, 1, n) {
        d[i] = -1;
        trace[i] = 0;
    }
    d[s] = 0;
    trace[s] = -1;
    q.push(s);

    while (isz(q)) {
        int u = q.front(); q.pop();

        for (int id : g[u]) {
            int v = e[id].other(u);

            if (d[v] == -1) {
                d[v] = d[u] + isBridge[id];
                q.push(v);
                trace[v] = id;
            }
        }
    }
}

signed main() {
    cin >> n;

    For (i, 1, n - 1) {
        int u, v; cin >> u >> v;
        e[i] = {u, v};
        g[u].push_back(i);
        g[v].push_back(i);
    }

    ans.reserve(n);
    For (i, 1, n - 1) {
        isBridge[i] = true;
    }

    for (;;) {
        Bfs(1);
        int s = max_element(d + 1, d + n + 1) - d;
        Bfs(s);
        int t = max_element(d + 1, d + n + 1) - d;

        if (d[t] == 0) break;

        ans.emplace_back(s, t);
        int u = t;

        while (u != s) {
            int id = trace[u];
            isBridge[id] = false;

            int p = e[id].other(u);
            u = p;
        }
    }

    cout << isz(ans) << '\n';
    for (const auto& p : ans) {
        cout << p.fi << ' ' << p.se << '\n';
    }
}

/*

*/

Compilation message (stderr)

net.cpp: In constructor '__Init__::__Init__()':
net.cpp:11:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |             freopen(Task".inp", "r", stdin);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:12:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |             freopen(Task".out", "w", stdout); }
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...