# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
931790 |
2024-02-22T10:56:11 Z |
vjudge1 |
Network (BOI15_net) |
C++17 |
|
314 ms |
51632 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 = 2e3 + 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){
int mxf = -1, r = 0;
if(!mark[b[i]]){
for(int j = 0; j < b.size(); ++j){
int x = h[b[i]] + h[b[j]] - 2 * h[lca(b[i], b[j])];
if(mxf < x) {mxf = x; r = b[j];}
}
cout << b[i] + 1 << " " << r + 1;
break;
}
}
}
// 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){
| ~~^~~~~~~~~~
net.cpp:86:21: 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]
86 | for(int j = 0; j < b.size(); ++j){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
282 ms |
51488 KB |
Output is correct |
23 |
Correct |
255 ms |
51632 KB |
Output is correct |
24 |
Correct |
314 ms |
51632 KB |
Output is correct |
25 |
Correct |
95 ms |
26832 KB |
Output is correct |
26 |
Correct |
62 ms |
15304 KB |
Output is correct |
27 |
Correct |
164 ms |
27324 KB |
Output is correct |
28 |
Correct |
155 ms |
27356 KB |
Output is correct |
29 |
Correct |
90 ms |
14284 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
168 ms |
26048 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
860 KB |
Output is correct |
34 |
Correct |
0 ms |
856 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
1116 KB |
Output is correct |
42 |
Correct |
13 ms |
5584 KB |
Output is correct |
43 |
Correct |
60 ms |
13592 KB |
Output is correct |
44 |
Correct |
1 ms |
1112 KB |
Output is correct |
45 |
Correct |
4 ms |
1624 KB |
Output is correct |
46 |
Correct |
23 ms |
4052 KB |
Output is correct |
47 |
Correct |
0 ms |
860 KB |
Output is correct |
48 |
Correct |
5 ms |
1748 KB |
Output is correct |
49 |
Correct |
58 ms |
14284 KB |
Output is correct |
50 |
Correct |
1 ms |
860 KB |
Output is correct |
51 |
Correct |
0 ms |
872 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
13 ms |
2516 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
9 ms |
2516 KB |
Output is correct |
56 |
Correct |
1 ms |
856 KB |
Output is correct |
57 |
Correct |
17 ms |
4816 KB |
Output is correct |
58 |
Correct |
312 ms |
51212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
282 ms |
51488 KB |
Output is correct |
23 |
Correct |
255 ms |
51632 KB |
Output is correct |
24 |
Correct |
314 ms |
51632 KB |
Output is correct |
25 |
Correct |
95 ms |
26832 KB |
Output is correct |
26 |
Correct |
62 ms |
15304 KB |
Output is correct |
27 |
Correct |
164 ms |
27324 KB |
Output is correct |
28 |
Correct |
155 ms |
27356 KB |
Output is correct |
29 |
Correct |
90 ms |
14284 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
168 ms |
26048 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
860 KB |
Output is correct |
34 |
Correct |
0 ms |
856 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
1116 KB |
Output is correct |
42 |
Correct |
13 ms |
5584 KB |
Output is correct |
43 |
Correct |
60 ms |
13592 KB |
Output is correct |
44 |
Correct |
1 ms |
1112 KB |
Output is correct |
45 |
Correct |
4 ms |
1624 KB |
Output is correct |
46 |
Correct |
23 ms |
4052 KB |
Output is correct |
47 |
Correct |
0 ms |
860 KB |
Output is correct |
48 |
Correct |
5 ms |
1748 KB |
Output is correct |
49 |
Correct |
58 ms |
14284 KB |
Output is correct |
50 |
Correct |
1 ms |
860 KB |
Output is correct |
51 |
Correct |
0 ms |
872 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
13 ms |
2516 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
9 ms |
2516 KB |
Output is correct |
56 |
Correct |
1 ms |
856 KB |
Output is correct |
57 |
Correct |
17 ms |
4816 KB |
Output is correct |
58 |
Correct |
312 ms |
51212 KB |
Output is correct |
59 |
Runtime error |
1 ms |
1368 KB |
Execution killed with signal 11 |
60 |
Halted |
0 ms |
0 KB |
- |