Submission #931768

# Submission time Handle Problem Language Result Execution time Memory
931768 2024-02-22T10:42:52 Z vjudge1 Network (BOI15_net) C++17
0 / 100
1 ms 604 KB
// in the name of God
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fast() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mk make_pair
#define ppb pop_back
#define endl '\n'
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define file() freopen("input.txt", "r", stdin);
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7, maxn = 1e3 + 10, sq = 3163, inf = 1e18 + 10, lg = 25, pp = 4447, modd = 1e9 + 9;

int n;
vector<vector<int>> g(maxn), par(lg, vector<int> (maxn, -1));
int h[maxn], mark[maxn];

void dfs(int u = 0, int parr = -1){
	par[0][u] = parr;
	for(int i = 1; i < lg; ++i){
		if(par[i - 1][u] < 0) break;
		par[i][u] = par[i - 1][par[i - 1][u]];
	}
	for(int v : g[u]){
		if(v != parr){
			h[v] = h[u] + 1;
			dfs(v, u);
		}
	}
}
int lca(int a, int b){
	if(h[a] < h[b]) swap(a, b);
	for(int i = lg - 1; i >= 0; --i){
		if(par[i][a] > -1 && h[par[i][a]] > h[b]) a = par[i][a];
	}
	if(a == b) return a;
	for(int i = lg - 1; i >= 0; --i){
		if(par[i][a] > -1 && par[i][b] > -1 && par[i][a] != par[i][b]){
			a = par[i][a];
			b = par[i][b];
		}
	}
	return par[0][a];
}
signed main(){
	fast();//file();
	cin >> n;
	for(int i = 1; i < n; ++i){
		int u, v; cin >> u >> v;
		u--; v--;
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs();
	vector<int> b;
	vector<pair<int, pii>> a;
	for(int i = 0; i < n; ++i) if(g[i].size() == 1) b.pb(i);
	for(int i = 0; i < b.size(); ++i){
		for(int j = i + 1; j < b.size(); ++j){
			int x = h[b[i]] + h[b[j]] - 2 * h[lca(b[i], b[j])];
			a.pb(mk(x, mk(b[i], b[j])));
		}
	}
	cout << ceil((double) b.size() / 2) << endl;
	sort(all(a));
	reverse(all(a));
	for(int i = 0; i < a.size(); ++i){
		int x = a[i].ff, y = a[i].ss.ff, z = a[i].ss.ss;
		if(!mark[y] && !mark[z]){
			mark[y] = true; mark[z] = true;
			cout << y + 1 << " " << z + 1 << endl;
		}
	}
	for(int i = 0; i < b.size(); ++i){
		if(!mark[b[i]]){
			cout << b[i] + 1 << " " << b.back() + 1;
		}
	}
}
// Running from the daylight...

Compilation message

net.cpp: In function 'int main()':
net.cpp:67:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |  for(int i = 0; i < b.size(); ++i){
      |                 ~~^~~~~~~~~~
net.cpp:68:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |   for(int j = i + 1; j < b.size(); ++j){
      |                      ~~^~~~~~~~~~
net.cpp:76:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |  for(int i = 0; i < a.size(); ++i){
      |                 ~~^~~~~~~~~~
net.cpp:77:7: warning: unused variable 'x' [-Wunused-variable]
   77 |   int x = a[i].ff, y = a[i].ss.ff, z = a[i].ss.ss;
      |       ^
net.cpp:83:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |  for(int i = 0; i < b.size(); ++i){
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Incorrect 1 ms 604 KB Breaking single line is causing network to disconnect.
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Incorrect 1 ms 604 KB Breaking single line is causing network to disconnect.
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Incorrect 1 ms 604 KB Breaking single line is causing network to disconnect.
15 Halted 0 ms 0 KB -