Submission #641198

# Submission time Handle Problem Language Result Execution time Memory
641198 2022-09-16T07:41:53 Z ghostwriter Network (BOI15_net) C++14
0 / 100
14 ms 23764 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
    Tran The Bao
    CTL - Da Lat
    Cay ngay cay dem nhung deo duoc cong nhan
*/
const int N = 5e5 + 5;
int n, d[N], tin[N], rpos[N], s[N], root = -1, cnt = 0;
vi adj[N], leafs, a[N];
vpi ans;
void dfs(int u, int p) {
	tin[u] = ++cnt;
	s[u] = 1;
	EACH(v, adj[u]) {
		if (v == p) continue;
		dfs(v, u);
		s[u] += s[v];
	}
	if (sz(adj[u]) == 1) leafs.pb(tin[u]);
}
void solve(int u, int p) {
	int pos = -1, cnt = 0;
	FOR(i, 1, cnt) a[i].clear();
	vi b(1, 0);
	EACH(v, adj[u]) {
		if (v == p) continue;
		if (leafs.back() < tin[v] || leafs.front() > tin[v] + s[v] - 1) continue;
		int l = -1, r = -1;
		l = lb(all(leafs), tin[v]) - leafs.begin();
		if (leafs.back() <= tin[v] + s[v] - 1) r = sz(leafs) - 1;
		else r = ub(all(leafs), tin[v] + s[v] - 1) - leafs.begin() - 1;
		if (l > r) continue;
		++cnt;
		FOR(i, l, r) a[cnt].pb(leafs[i]);
		pos = v;
		b.pb(v);
	}
	if (cnt == 1) solve(pos, u);
	else {
		multiset<pi, greater<pi> > s;
		FOR(i, 1, cnt) s.insert({sz(a[i]), i});
		WHILE(sz(s) > 1) {
			int u = (*s.begin()).nd, v = (*++s.begin()).nd;
			s.erase(s.begin());
			s.erase(s.begin());
			ans.pb({a[u].back(), a[v].back()});
			a[u]._pb();
			a[v]._pb();
			if (!a[u].empty()) s.insert({sz(a[u]), u});
			if (!a[v].empty()) s.insert({sz(a[v]), v});
		}
		if (s.empty()) return;
		int cur = (*s.begin()).nd;
		if (sz(a[cur]) == 1) {
			ans.pb({a[cur].back(), a[cur].back() == 1? 2 : 1});
			return;
		}
		leafs.clear();
		EACH(i, a[cur]) leafs.pb(i);
		solve(b[cur], u);
	}
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    FOR(i, 1, n - 1) {
    	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;
    dfs(root, 0);
    FOR(i, 1, n) rpos[tin[i]] = i;
    solve(root, 0);
    cout << sz(ans) << '\n';
    EACH(i, ans) cout << rpos[i.st] << ' ' << rpos[i.nd] << '\n';
    return 0;
}
/*
8
1 2
2 3
3 4
4 5
3 6
3 7
3 8
*/

Compilation message

net.cpp: In function 'void dfs(int, int)':
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:43:2: note: in expansion of macro 'EACH'
   43 |  EACH(v, adj[u]) {
      |  ^~~~
net.cpp: In function 'void solve(int, int)':
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:52:2: note: in expansion of macro 'FOR'
   52 |  FOR(i, 1, cnt) a[i].clear();
      |  ^~~
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:54:2: note: in expansion of macro 'EACH'
   54 |  EACH(v, adj[u]) {
      |  ^~~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:63:3: note: in expansion of macro 'FOR'
   63 |   FOR(i, l, r) a[cnt].pb(leafs[i]);
      |   ^~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:70:3: note: in expansion of macro 'FOR'
   70 |   FOR(i, 1, cnt) s.insert({sz(a[i]), i});
      |   ^~~
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:88:3: note: in expansion of macro 'EACH'
   88 |   EACH(i, a[cur]) leafs.pb(i);
      |   ^~~~
net.cpp: In function 'int main()':
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:97:5: note: in expansion of macro 'FOR'
   97 |     FOR(i, 1, n - 1) {
      |     ^~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:103:5: note: in expansion of macro 'FOR'
  103 |     FOR(i, 1, n)
      |     ^~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:107:5: note: in expansion of macro 'FOR'
  107 |     FOR(i, 1, n) rpos[tin[i]] = i;
      |     ^~~
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:110:5: note: in expansion of macro 'EACH'
  110 |     EACH(i, ans) cout << rpos[i.st] << ' ' << rpos[i.nd] << '\n';
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Incorrect 14 ms 23732 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Incorrect 14 ms 23732 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Incorrect 14 ms 23732 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -