Submission #292719

# Submission time Handle Problem Language Result Execution time Memory
292719 2020-09-07T12:16:32 Z moven(#5823) ROI16_sending (ROI16_sending) C++17
68 / 100
5000 ms 22784 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;

int n, m;
vector<int> gph[MAXN];
pi a[MAXN];

int par[18][MAXN];
int din[MAXN], dout[MAXN], dep[MAXN], piv;

int anc(int x, int v){
	for(int i=0; v; i++){
		if(v & 1) x = par[i][x];
		v >>= 1;
	}
	return x;
}

int lca(int x, int y){
	if(dep[x] > dep[y]) swap(x, y);
	y = anc(y, dep[y] - dep[x]);
	for(int i=17; i>=0; i--){
		if(par[i][x] != par[i][y]){
			x = par[i][x];
			y = par[i][y];
		}
	}
	if(x != y) return par[0][x];
	return x;
}

struct bar{
	int dist, x, y;
	bool operator<(const bar &t)const{
		return dist < t.dist;
	}
};

struct kek{
	int pp, qq, x, y, idx;
	bool operator<(const kek &k)const{
		return pi(pp, qq) < pi(k.pp, k.qq);
	}
};

bar solve(vector<kek> a, int v){
	bar ret = (bar){-1, -1, -1};
	for(int i=0; i<sz(a); i++){
		for(int j=0; j<i; j++){
			bar w = {dep[lca(a[i].x, a[j].x)] + dep[lca(a[i].y, a[j].y)] - 2 * dep[v], a[i].idx, a[j].idx};
			ret = max(ret, w);
		}
	}
	return ret;
}

void dfs(int x){
	din[x] = piv++;
	for(auto &i : gph[x]){
		dep[i] = dep[x] + 1;
		par[0][i] = x;
		dfs(i);
	}
	dout[x] = piv;
}

pi up[MAXN][2];

void upload(int x, pi v){
	if(up[x][0] > v){
		up[x][1] = up[x][0];
		up[x][0] = v;
	}
	else if(up[x][1] > v){
		up[x][1] = v;
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=2; i<=n; i++){
		int x; scanf("%d",&x);
		gph[x].push_back(i);
	}
	dfs(1);
	for(int i=1; i<18; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	for(int i=1; i<=n; i++) up[i][0] = up[i][1] = pi(1e9, 0);
	vector<kek> v;
	for(int i=1; i<=m; i++){
		int x, y; scanf("%d %d",&x,&y);
		int l = lca(x, y);
		if(l != x) upload(x, pi(dep[l], i));
		if(l != y) upload(y, pi(dep[l], i));
		if(l != x && l != y){
			int cx = anc(x, dep[x] - dep[l] - 1);
			int cy = anc(y, dep[y] - dep[l] - 1);
			if(cx > cy) swap(cx, cy), swap(x, y);
			v.push_back({cx, cy, x, y, i});
		}
	}
	sort(all(v));
	bar ans = {0, 1, 2};
	for(int i=n; i; i--){
		for(auto &j : gph[i]){
			upload(i, up[j][0]);
			upload(i, up[j][1]);
		}
		if(up[i][1].second){
			int dist = dep[i] - up[i][1].first;
			if(dist > 0) ans = max(ans, (bar){dist, up[i][0].second, up[i][1].second});
		}
	}
	for(int i=0; i<sz(v); ){
		int e = i;
		while(e < sz(v) && pi(v[e].pp, v[e].qq) == pi(v[i].pp, v[i].qq)) e++;
		vector<kek> w;
		for(int j=i; j<e; j++) w.push_back(v[j]);
		ans = max(ans, solve(w, par[0][v[i].pp]));
		i = e;
	}
	printf("%d\n%d %d\n", ans.dist, ans.x, ans.y);
}

Compilation message

sending.cpp: In function 'int main()':
sending.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |  scanf("%d %d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~~
sending.cpp:87:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |   int x; scanf("%d",&x);
      |          ~~~~~^~~~~~~~~
sending.cpp:99:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   99 |   int x, y; scanf("%d %d",&x,&y);
      |             ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 26 ms 5760 KB Output is correct
22 Correct 5 ms 5888 KB Output is correct
23 Correct 44 ms 5760 KB Output is correct
24 Correct 6 ms 5760 KB Output is correct
25 Correct 5 ms 5632 KB Output is correct
26 Correct 5 ms 5888 KB Output is correct
27 Correct 17 ms 5632 KB Output is correct
28 Correct 46 ms 5632 KB Output is correct
29 Correct 5 ms 5920 KB Output is correct
30 Correct 5 ms 5760 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 121 ms 5760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 26 ms 5760 KB Output is correct
22 Correct 5 ms 5888 KB Output is correct
23 Correct 44 ms 5760 KB Output is correct
24 Correct 6 ms 5760 KB Output is correct
25 Correct 5 ms 5632 KB Output is correct
26 Correct 5 ms 5888 KB Output is correct
27 Correct 17 ms 5632 KB Output is correct
28 Correct 46 ms 5632 KB Output is correct
29 Correct 5 ms 5920 KB Output is correct
30 Correct 5 ms 5760 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 121 ms 5760 KB Output is correct
34 Correct 97 ms 16632 KB Output is correct
35 Correct 120 ms 16760 KB Output is correct
36 Correct 40 ms 22700 KB Output is correct
37 Correct 177 ms 20520 KB Output is correct
38 Correct 39 ms 18808 KB Output is correct
39 Correct 28 ms 15472 KB Output is correct
40 Correct 43 ms 22652 KB Output is correct
41 Correct 56 ms 15488 KB Output is correct
42 Correct 127 ms 16504 KB Output is correct
43 Correct 46 ms 21880 KB Output is correct
44 Correct 44 ms 21880 KB Output is correct
45 Correct 41 ms 21880 KB Output is correct
46 Correct 43 ms 21880 KB Output is correct
47 Correct 314 ms 17656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 26 ms 5760 KB Output is correct
22 Correct 5 ms 5888 KB Output is correct
23 Correct 44 ms 5760 KB Output is correct
24 Correct 6 ms 5760 KB Output is correct
25 Correct 5 ms 5632 KB Output is correct
26 Correct 5 ms 5888 KB Output is correct
27 Correct 17 ms 5632 KB Output is correct
28 Correct 46 ms 5632 KB Output is correct
29 Correct 5 ms 5920 KB Output is correct
30 Correct 5 ms 5760 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 121 ms 5760 KB Output is correct
34 Correct 97 ms 16632 KB Output is correct
35 Correct 120 ms 16760 KB Output is correct
36 Correct 40 ms 22700 KB Output is correct
37 Correct 177 ms 20520 KB Output is correct
38 Correct 39 ms 18808 KB Output is correct
39 Correct 28 ms 15472 KB Output is correct
40 Correct 43 ms 22652 KB Output is correct
41 Correct 56 ms 15488 KB Output is correct
42 Correct 127 ms 16504 KB Output is correct
43 Correct 46 ms 21880 KB Output is correct
44 Correct 44 ms 21880 KB Output is correct
45 Correct 41 ms 21880 KB Output is correct
46 Correct 43 ms 21880 KB Output is correct
47 Correct 314 ms 17656 KB Output is correct
48 Correct 1467 ms 16740 KB Output is correct
49 Correct 2011 ms 17028 KB Output is correct
50 Correct 48 ms 22648 KB Output is correct
51 Correct 4077 ms 20616 KB Output is correct
52 Correct 46 ms 18936 KB Output is correct
53 Correct 37 ms 15608 KB Output is correct
54 Correct 54 ms 22648 KB Output is correct
55 Correct 852 ms 15752 KB Output is correct
56 Correct 2727 ms 16984 KB Output is correct
57 Correct 45 ms 21880 KB Output is correct
58 Correct 42 ms 21880 KB Output is correct
59 Correct 42 ms 22008 KB Output is correct
60 Correct 52 ms 22008 KB Output is correct
61 Correct 4481 ms 21952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5059 ms 17904 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 88 ms 16568 KB Output is correct
2 Correct 94 ms 16760 KB Output is correct
3 Correct 137 ms 22656 KB Output is correct
4 Correct 136 ms 20416 KB Output is correct
5 Correct 131 ms 18812 KB Output is correct
6 Correct 43 ms 15608 KB Output is correct
7 Correct 138 ms 22784 KB Output is correct
8 Correct 48 ms 15480 KB Output is correct
9 Correct 71 ms 16504 KB Output is correct
10 Correct 54 ms 22776 KB Output is correct
11 Correct 52 ms 22776 KB Output is correct
12 Correct 48 ms 22776 KB Output is correct
13 Correct 146 ms 22776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 26 ms 5760 KB Output is correct
22 Correct 5 ms 5888 KB Output is correct
23 Correct 44 ms 5760 KB Output is correct
24 Correct 6 ms 5760 KB Output is correct
25 Correct 5 ms 5632 KB Output is correct
26 Correct 5 ms 5888 KB Output is correct
27 Correct 17 ms 5632 KB Output is correct
28 Correct 46 ms 5632 KB Output is correct
29 Correct 5 ms 5920 KB Output is correct
30 Correct 5 ms 5760 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 121 ms 5760 KB Output is correct
34 Correct 97 ms 16632 KB Output is correct
35 Correct 120 ms 16760 KB Output is correct
36 Correct 40 ms 22700 KB Output is correct
37 Correct 177 ms 20520 KB Output is correct
38 Correct 39 ms 18808 KB Output is correct
39 Correct 28 ms 15472 KB Output is correct
40 Correct 43 ms 22652 KB Output is correct
41 Correct 56 ms 15488 KB Output is correct
42 Correct 127 ms 16504 KB Output is correct
43 Correct 46 ms 21880 KB Output is correct
44 Correct 44 ms 21880 KB Output is correct
45 Correct 41 ms 21880 KB Output is correct
46 Correct 43 ms 21880 KB Output is correct
47 Correct 314 ms 17656 KB Output is correct
48 Correct 1467 ms 16740 KB Output is correct
49 Correct 2011 ms 17028 KB Output is correct
50 Correct 48 ms 22648 KB Output is correct
51 Correct 4077 ms 20616 KB Output is correct
52 Correct 46 ms 18936 KB Output is correct
53 Correct 37 ms 15608 KB Output is correct
54 Correct 54 ms 22648 KB Output is correct
55 Correct 852 ms 15752 KB Output is correct
56 Correct 2727 ms 16984 KB Output is correct
57 Correct 45 ms 21880 KB Output is correct
58 Correct 42 ms 21880 KB Output is correct
59 Correct 42 ms 22008 KB Output is correct
60 Correct 52 ms 22008 KB Output is correct
61 Correct 4481 ms 21952 KB Output is correct
62 Execution timed out 5059 ms 17904 KB Time limit exceeded
63 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 20 ms 5632 KB Output is correct
21 Correct 26 ms 5760 KB Output is correct
22 Correct 5 ms 5888 KB Output is correct
23 Correct 44 ms 5760 KB Output is correct
24 Correct 6 ms 5760 KB Output is correct
25 Correct 5 ms 5632 KB Output is correct
26 Correct 5 ms 5888 KB Output is correct
27 Correct 17 ms 5632 KB Output is correct
28 Correct 46 ms 5632 KB Output is correct
29 Correct 5 ms 5920 KB Output is correct
30 Correct 5 ms 5760 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 121 ms 5760 KB Output is correct
34 Correct 97 ms 16632 KB Output is correct
35 Correct 120 ms 16760 KB Output is correct
36 Correct 40 ms 22700 KB Output is correct
37 Correct 177 ms 20520 KB Output is correct
38 Correct 39 ms 18808 KB Output is correct
39 Correct 28 ms 15472 KB Output is correct
40 Correct 43 ms 22652 KB Output is correct
41 Correct 56 ms 15488 KB Output is correct
42 Correct 127 ms 16504 KB Output is correct
43 Correct 46 ms 21880 KB Output is correct
44 Correct 44 ms 21880 KB Output is correct
45 Correct 41 ms 21880 KB Output is correct
46 Correct 43 ms 21880 KB Output is correct
47 Correct 314 ms 17656 KB Output is correct
48 Correct 1467 ms 16740 KB Output is correct
49 Correct 2011 ms 17028 KB Output is correct
50 Correct 48 ms 22648 KB Output is correct
51 Correct 4077 ms 20616 KB Output is correct
52 Correct 46 ms 18936 KB Output is correct
53 Correct 37 ms 15608 KB Output is correct
54 Correct 54 ms 22648 KB Output is correct
55 Correct 852 ms 15752 KB Output is correct
56 Correct 2727 ms 16984 KB Output is correct
57 Correct 45 ms 21880 KB Output is correct
58 Correct 42 ms 21880 KB Output is correct
59 Correct 42 ms 22008 KB Output is correct
60 Correct 52 ms 22008 KB Output is correct
61 Correct 4481 ms 21952 KB Output is correct
62 Execution timed out 5059 ms 17904 KB Time limit exceeded
63 Halted 0 ms 0 KB -