Submission #716461

# Submission time Handle Problem Language Result Execution time Memory
716461 2023-03-30T06:34:17 Z ajab_01 Dynamic Diameter (CEOI19_diameter) C++17
31 / 100
5000 ms 15008 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long

const int N = 1e5 + 5;
vector<pair<pair<int , int> , int> > edge;
vector<pair<int , int> > g[N];
int dp[N][2] , level[N] , d , e , n , q , w , last , star = -1;
bool ch = 1;
multiset<int , greater<int>> st;

void dfs(int ver , int par){
	for(auto i : g[ver]){
		int uu = i.first , ww = i.second;
		if(uu == par) continue;
		level[uu] = level[ver] + ww;
		dfs(uu , ver);
	}
}

void cal(){
	for(int i = 1 ; i <= n ; i++) g[i].clear();
	for(auto i : edge){
		int U = i.first.first , V = i.first.second , W = i.second;
		g[U].push_back({V , W});
		g[V].push_back({U , W});
	}

	dfs(1 , 0);

	int mx = 0 , vr = 1;

	for(int i = 1 ; i <= n ; i++){
		if(mx < level[i]){
			mx = level[i];
			vr = i;
		}
	}

	level[vr] = 0;

	dfs(vr , 0);

	mx = 0;
	for(int i = 1 ; i <= n ; i++)
		mx = max(mx , level[i]);

	cout << mx << '\n';
	last = mx;
	return;
}

void solve1(){
	while(q--){
		cin >> d >> e;
		d = (d + last) % (n - 1);
		e = (e + last) % w;
		pair<pair<int , int> , int> p;
		p = edge[d];
		p.second = e;
		swap(edge[d] , p);
		cal();
	}
}

void solve2(){
	for(auto i : edge)
		st.insert(i.second);
	while(q--){
		cin >> d >> e;
		d = (d + last) % (n - 1);
		e = (e + last) % w;
		st.erase(st.find(edge[d].second));
		pair<pair<int , int> , int> p;
		p = edge[d];
		p.second = e;
		swap(edge[d] , p);
		st.insert(e);
		last = *st.begin() + *next(st.begin());
		cout << last << '\n';
	}
}

void pre(int ver , int pr){
	st.erase(st.find(dp[ver][0]));
	int mxx = 0;
	for(int i = 0 ; i < (int)g[ver].size() ; i++){
		int uu = g[ver][i].first , ww = g[ver][i].second;
		if(uu == pr) continue;
		dp[ver][0] = max(dp[ver][0] , dp[uu][1] + ww + mxx);
		mxx = max(mxx , dp[uu][1] + ww);
		dp[ver][1] = max(dp[ver][1] , dp[uu][1] + ww);
	}
	st.insert(dp[ver][0]);
}

void dfs2(int ver , int par){
	if(g[ver].size() == 1) return;
	for(auto i : g[ver]){
		int uu = i.first , ww = i.second;
		if(uu == par) continue;
		dfs2(uu , ver);
	}
	pre(ver , par);
}

void solve3(){
	dfs2(1 , 0);
	while(q--){
		cin >> d >> e;
		d = (d + last) % (n - 1);
		e = (e + last) % w;
		int uu = edge[d].first.first , vv = edge[d].first.second;
		pair<pair<int , int> , int> p;
		p = edge[d];
		p.second = e;
		swap(edge[d] , p);
		if(uu > vv) swap(uu , vv);
		pre(uu , uu / 2);
		while(uu != 1){
			uu /= 2;
			pre(uu , uu / 2);
		}
		last = *st.begin();
		cout << last << '\n';
	}
}

int32_t main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> q >> w;
	for(int i = 0 ; i < n - 1 ; i++){
		int U , V , W;
		cin >> U >> V >> W;
		g[U].push_back({V , W});
		g[V].push_back({U , W});
		edge.push_back({{U , V} , W});
		if(g[U].size() == n - 1) star = U;
		if(g[V].size() == n - 1) star = V;
		if(U > V) swap(U , V);
		if(abs(V - 2 * U) > 1) ch = 0;
	}
	if(n <= 5000 && q <= 5000){
		solve1();
		return 0;
	}
	if(star != -1){
		solve2();
		return 0;
	}
	if(ch){
		solve3();
		return 0;
	}
	return 0;
}

Compilation message

diameter.cpp: In function 'void dfs2(long long int, long long int)':
diameter.cpp:101:22: warning: unused variable 'ww' [-Wunused-variable]
  101 |   int uu = i.first , ww = i.second;
      |                      ^~
diameter.cpp: In function 'int32_t main()':
diameter.cpp:139:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  139 |   if(g[U].size() == n - 1) star = U;
      |      ~~~~~~~~~~~~^~~~~~~~
diameter.cpp:140:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  140 |   if(g[V].size() == n - 1) star = V;
      |      ~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2580 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2580 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 244 ms 2756 KB Output is correct
20 Correct 241 ms 2764 KB Output is correct
21 Correct 237 ms 2772 KB Output is correct
22 Correct 256 ms 2792 KB Output is correct
23 Correct 1589 ms 3164 KB Output is correct
24 Correct 1665 ms 3272 KB Output is correct
25 Correct 1684 ms 3176 KB Output is correct
26 Correct 1673 ms 3332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 4 ms 2644 KB Output is correct
4 Correct 9 ms 2772 KB Output is correct
5 Correct 32 ms 3028 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 6 ms 2644 KB Output is correct
9 Correct 38 ms 2644 KB Output is correct
10 Correct 11 ms 2760 KB Output is correct
11 Correct 36 ms 3164 KB Output is correct
12 Correct 4 ms 3164 KB Output is correct
13 Correct 4 ms 3156 KB Output is correct
14 Correct 5 ms 3284 KB Output is correct
15 Correct 12 ms 3284 KB Output is correct
16 Correct 45 ms 3796 KB Output is correct
17 Correct 69 ms 14396 KB Output is correct
18 Correct 59 ms 14352 KB Output is correct
19 Correct 58 ms 14316 KB Output is correct
20 Correct 72 ms 14492 KB Output is correct
21 Correct 161 ms 15008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 2764 KB Output is correct
2 Execution timed out 5052 ms 2644 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 76 ms 10236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2580 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 244 ms 2756 KB Output is correct
20 Correct 241 ms 2764 KB Output is correct
21 Correct 237 ms 2772 KB Output is correct
22 Correct 256 ms 2792 KB Output is correct
23 Correct 1589 ms 3164 KB Output is correct
24 Correct 1665 ms 3272 KB Output is correct
25 Correct 1684 ms 3176 KB Output is correct
26 Correct 1673 ms 3332 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 4 ms 2644 KB Output is correct
30 Correct 9 ms 2772 KB Output is correct
31 Correct 32 ms 3028 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 3 ms 2644 KB Output is correct
34 Correct 6 ms 2644 KB Output is correct
35 Correct 38 ms 2644 KB Output is correct
36 Correct 11 ms 2760 KB Output is correct
37 Correct 36 ms 3164 KB Output is correct
38 Correct 4 ms 3164 KB Output is correct
39 Correct 4 ms 3156 KB Output is correct
40 Correct 5 ms 3284 KB Output is correct
41 Correct 12 ms 3284 KB Output is correct
42 Correct 45 ms 3796 KB Output is correct
43 Correct 69 ms 14396 KB Output is correct
44 Correct 59 ms 14352 KB Output is correct
45 Correct 58 ms 14316 KB Output is correct
46 Correct 72 ms 14492 KB Output is correct
47 Correct 161 ms 15008 KB Output is correct
48 Correct 41 ms 2764 KB Output is correct
49 Execution timed out 5052 ms 2644 KB Time limit exceeded
50 Halted 0 ms 0 KB -