Submission #1089948

#TimeUsernameProblemLanguageResultExecution timeMemory
1089948JanPhamNetwork (BOI15_net)C++17
0 / 100
112 ms262144 KiB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define TranLeThanhTam "TranLeThanhTam"
#define all(v) v.begin(),v.end()
#define reset(a,i) memset(a,i,sizeof(a))
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; --i)
#define compact(v) sort(all(v)), v.erase(unique(all(v)), end(v))
#define sz(v) (int) v.size()
#define MASK(i) (1 << (i))

typedef long long ll;
typedef long double lb;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;

const int Thanh_Tam = 5 * 1e5 + 17;
const int inf = 1e9 + 17;
const long long infll = 1e18 + 17;
const int MOD = 1e9 + 7;
const int Log = 17;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
const int max_block_size = 270;
const ll P = 311;
const int maxA = 1e2 + 17;
const int addi = 1e9;
const char dir[4] = {'W', 'S', 'N', 'E'};
const int Mod[3] = {998244353, MOD, 759186269};

ll mul(ll a, ll b, ll mod);
ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1) res = mul(res,a,mod);
        b >>= 1;
        a = mul(a,a,mod);
    }
    return res;
}
ll add(ll a, ll b, ll mod) { return (a + b) % mod;                 }
ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod;          }
ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
ll div(ll a, ll b, ll mod) { return mul(a, binpow(b, mod - 2, mod), mod); }

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

int n, root = 1;
vector<int> adj[Thanh_Tam];
deque<int> leaf[Thanh_Tam];
vector<pii> need_edges;

void dp_on_tree(int u, int par = 0)
{
    if (sz(adj[u]) == 1)
    {
        leaf[u].pb(u);
        return;
    }

    for (int v : adj[u])
    {
        if (par == v) continue;
        dp_on_tree(v, u);
        if (sz(leaf[v]) > 0)
            for (int k : leaf[v]) leaf[u].pb(k);
        leaf[v].clear();

        if (sz(leaf[u]) > 2)
        {
            int x = leaf[u].back();
            leaf[u].pop_back();
            int y = leaf[u].front();
            leaf[u].pop_front();
            need_edges.pb({x, y});
        }
    }

    return;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

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

    cin >> n;
    FOR(i,2,n)
    {
        int u,v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    FOR(i,1,n) 
        if (sz(adj[i]) > 1)
        {
            root = i;
            break;
        }

    dp_on_tree(root);
    if (sz(leaf[root]) > 0)
    {
        if (sz(leaf[root]) == 2) need_edges.pb({leaf[root][0], leaf[root][1]});
        else need_edges.pb({need_edges.back().se, leaf[root].back()});
    }
    cout << sz(need_edges) << '\n';
    for (pii k : need_edges) cout << k.fi << ' ' << k.se << '\n';
    return 0;
}

Compilation message (stderr)

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