제출 #1251315

#제출 시각아이디문제언어결과실행 시간메모리
1251315tradzNetwork (BOI15_net)C++20
100 / 100
403 ms60888 KiB
#include <bits/stdc++.h>
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())

using namespace std;
const int  N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
long long GetRandom(long long l, long long r) {
    return uniform_int_distribution<long long> (l, r)(rng);
}

int n;
vector <int> g[N];

int dp[N];

void dfs(int u, int pre = 0) {
    dp[u] = 1;
    for (int v: g[u]) {
        if (v == pre) continue;
        dfs(v, u);
        dp[u] += dp[v];
    }
}

int centroid(int u, int pre = 0) {
    for (int v: g[u]) {
        if (v == pre) continue;
        if (dp[v] * 2 >= n) {
            return centroid(v, u);
        }
    }

    return u;
}

vector <int> leaf;

void dfs2(int u, int pre = 0) {
    dp[u] = 1;
    for (int v: g[u]) {
        if (v == pre) continue;
        dfs2(v, u);
        dp[u] += dp[v];
    }
    if (dp[u] == 1) leaf.push_back(u);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    #define TASK ""
    if (fopen (".inp", "r")) {
        freopen (".inp", "r", stdin);
        freopen (".out", "w", stdout);
    }
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

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

    dfs(1);
    int cen = centroid(1);

    dfs2(cen);

    int sz = leaf.size();

    cout << (sz + 1) / 2 << '\n';
    for (int i = 0; i < (sz + 1) / 2; i++) {
        cout << leaf[i] << " " << leaf[(i + sz / 2) % sz] << '\n';
    }


    cerr << "Time elapsed: " << TIME << " s.\n";
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

net.cpp: In function 'int main()':
net.cpp:65:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen (".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
net.cpp:66:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen (".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...