Submission #871141

# Submission time Handle Problem Language Result Execution time Memory
871141 2023-11-10T03:40:58 Z willychan Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
2914 ms 29612 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
struct edge{
	int a,b,w;
};
const int N = 505;
vector<int> tree[N];
vector<pair<int,int> > prefix;
set<int> in;
int P[N];
vector<edge> eg;	
int n,m;
void dfs(int cur,int p){
	for(auto i : tree[cur]){
		int a = eg[i].a;
		int b = eg[i].b;
		if(cur==b) swap(a,b);
		if(b==p) continue;
		P[b] = i;
		dfs(b,cur);
	}
}
int minedge(int cur){
	for(int i=0;i<=n;i++) P[i]=-1;
	int x = eg[cur].a;
	int y = eg[cur].b;
	dfs(x,x);
	if(P[y]==-1) return -1;
	int minn = -1;
	while(y!=x){
		int ny = eg[P[y]].a+eg[P[y]].b-y;
		int w = eg[P[y]].w;
		if(minn==-1 || w<eg[minn].w) minn = P[y];
		y = ny;
	}
	return minn;
}


int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;	
	for(int i=0;i<m;i++){
		int a,b,c;cin>>a>>b>>c;
		eg.push_back({a,b,c});
	}
	sort(eg.begin(),eg.end(),[](const edge &a,const edge &b){return a.w<b.w;});
	vector<int> L(m,-1),R(m,1e9+5);
	for(int i=0;i<m;i++){
		int cut = minedge(i);
		L[i]=0;
		if(cut>=0){
			int a = eg[cut].a;
			int b = eg[cut].b;
			int k = find(tree[a].begin(),tree[a].end(),cut)-tree[a].begin();
			tree[a].erase(tree[a].begin()+k);
			k = find(tree[b].begin(),tree[b].end(),cut)-tree[b].begin();
			tree[b].erase(tree[b].begin()+k);
			R[cut] = L[i] = ((eg[cut].w+eg[i].w)/2)+1;
		}
		tree[eg[i].a].push_back(i);
		tree[eg[i].b].push_back(i);
	}
	for(int i=0;i<m;i++){
		prefix.push_back({L[i],i});
		prefix.push_back({R[i],i});
	}
	sort(prefix.begin(),prefix.end());
	int q;cin>>q;
	int head = 0;
	while(q--){
		int x;cin>>x;
		while(head<prefix.size() && prefix[head].first<=x){
			if(in.count(prefix[head].second)) in.erase(prefix[head].second);
			else in.insert(prefix[head].second);
			head++;	
		}
	
		ll ans = 0;
		for(auto e : in){
			ans+=abs(eg[e].w-x);
		}
		cout<<ans<<"\n";
	}
	
	return 0;
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:76:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   while(head<prefix.size() && prefix[head].first<=x){
      |         ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 472 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 472 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 472 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 472 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 642 ms 7108 KB Output is correct
21 Correct 415 ms 7228 KB Output is correct
22 Correct 595 ms 7364 KB Output is correct
23 Correct 710 ms 7100 KB Output is correct
24 Correct 679 ms 7228 KB Output is correct
25 Correct 656 ms 7232 KB Output is correct
26 Correct 677 ms 7360 KB Output is correct
27 Correct 647 ms 7240 KB Output is correct
28 Correct 651 ms 7108 KB Output is correct
29 Correct 617 ms 7224 KB Output is correct
30 Correct 642 ms 7368 KB Output is correct
31 Correct 672 ms 7476 KB Output is correct
32 Correct 654 ms 7244 KB Output is correct
33 Correct 657 ms 7104 KB Output is correct
34 Correct 577 ms 7344 KB Output is correct
35 Correct 645 ms 7248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 500 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2858 ms 27720 KB Output is correct
5 Correct 2876 ms 27504 KB Output is correct
6 Correct 2914 ms 27296 KB Output is correct
7 Correct 2217 ms 29436 KB Output is correct
8 Correct 2251 ms 29344 KB Output is correct
9 Correct 2163 ms 29548 KB Output is correct
10 Correct 2790 ms 28000 KB Output is correct
11 Correct 2261 ms 29388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 472 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 472 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 2012 ms 22388 KB Output is correct
21 Correct 1947 ms 22824 KB Output is correct
22 Correct 1958 ms 22380 KB Output is correct
23 Correct 2025 ms 22352 KB Output is correct
24 Correct 2003 ms 22644 KB Output is correct
25 Correct 2012 ms 22736 KB Output is correct
26 Correct 1963 ms 22312 KB Output is correct
27 Correct 1955 ms 22304 KB Output is correct
28 Correct 1956 ms 22236 KB Output is correct
29 Correct 1998 ms 22380 KB Output is correct
30 Correct 1937 ms 22632 KB Output is correct
31 Correct 2019 ms 22544 KB Output is correct
32 Correct 2112 ms 22944 KB Output is correct
33 Correct 1937 ms 22544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 472 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 472 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 642 ms 7108 KB Output is correct
21 Correct 415 ms 7228 KB Output is correct
22 Correct 595 ms 7364 KB Output is correct
23 Correct 710 ms 7100 KB Output is correct
24 Correct 679 ms 7228 KB Output is correct
25 Correct 656 ms 7232 KB Output is correct
26 Correct 677 ms 7360 KB Output is correct
27 Correct 647 ms 7240 KB Output is correct
28 Correct 651 ms 7108 KB Output is correct
29 Correct 617 ms 7224 KB Output is correct
30 Correct 642 ms 7368 KB Output is correct
31 Correct 672 ms 7476 KB Output is correct
32 Correct 654 ms 7244 KB Output is correct
33 Correct 657 ms 7104 KB Output is correct
34 Correct 577 ms 7344 KB Output is correct
35 Correct 645 ms 7248 KB Output is correct
36 Correct 714 ms 7240 KB Output is correct
37 Correct 449 ms 7428 KB Output is correct
38 Correct 683 ms 7428 KB Output is correct
39 Correct 733 ms 7272 KB Output is correct
40 Correct 721 ms 7424 KB Output is correct
41 Correct 715 ms 7620 KB Output is correct
42 Correct 709 ms 7360 KB Output is correct
43 Correct 712 ms 7436 KB Output is correct
44 Correct 690 ms 7436 KB Output is correct
45 Correct 656 ms 7420 KB Output is correct
46 Correct 694 ms 7436 KB Output is correct
47 Correct 698 ms 7616 KB Output is correct
48 Correct 693 ms 7624 KB Output is correct
49 Correct 686 ms 7688 KB Output is correct
50 Correct 629 ms 7536 KB Output is correct
51 Correct 674 ms 7360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 472 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 472 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 642 ms 7108 KB Output is correct
21 Correct 415 ms 7228 KB Output is correct
22 Correct 595 ms 7364 KB Output is correct
23 Correct 710 ms 7100 KB Output is correct
24 Correct 679 ms 7228 KB Output is correct
25 Correct 656 ms 7232 KB Output is correct
26 Correct 677 ms 7360 KB Output is correct
27 Correct 647 ms 7240 KB Output is correct
28 Correct 651 ms 7108 KB Output is correct
29 Correct 617 ms 7224 KB Output is correct
30 Correct 642 ms 7368 KB Output is correct
31 Correct 672 ms 7476 KB Output is correct
32 Correct 654 ms 7244 KB Output is correct
33 Correct 657 ms 7104 KB Output is correct
34 Correct 577 ms 7344 KB Output is correct
35 Correct 645 ms 7248 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 500 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 2858 ms 27720 KB Output is correct
40 Correct 2876 ms 27504 KB Output is correct
41 Correct 2914 ms 27296 KB Output is correct
42 Correct 2217 ms 29436 KB Output is correct
43 Correct 2251 ms 29344 KB Output is correct
44 Correct 2163 ms 29548 KB Output is correct
45 Correct 2790 ms 28000 KB Output is correct
46 Correct 2261 ms 29388 KB Output is correct
47 Correct 1 ms 600 KB Output is correct
48 Correct 2012 ms 22388 KB Output is correct
49 Correct 1947 ms 22824 KB Output is correct
50 Correct 1958 ms 22380 KB Output is correct
51 Correct 2025 ms 22352 KB Output is correct
52 Correct 2003 ms 22644 KB Output is correct
53 Correct 2012 ms 22736 KB Output is correct
54 Correct 1963 ms 22312 KB Output is correct
55 Correct 1955 ms 22304 KB Output is correct
56 Correct 1956 ms 22236 KB Output is correct
57 Correct 1998 ms 22380 KB Output is correct
58 Correct 1937 ms 22632 KB Output is correct
59 Correct 2019 ms 22544 KB Output is correct
60 Correct 2112 ms 22944 KB Output is correct
61 Correct 1937 ms 22544 KB Output is correct
62 Correct 714 ms 7240 KB Output is correct
63 Correct 449 ms 7428 KB Output is correct
64 Correct 683 ms 7428 KB Output is correct
65 Correct 733 ms 7272 KB Output is correct
66 Correct 721 ms 7424 KB Output is correct
67 Correct 715 ms 7620 KB Output is correct
68 Correct 709 ms 7360 KB Output is correct
69 Correct 712 ms 7436 KB Output is correct
70 Correct 690 ms 7436 KB Output is correct
71 Correct 656 ms 7420 KB Output is correct
72 Correct 694 ms 7436 KB Output is correct
73 Correct 698 ms 7616 KB Output is correct
74 Correct 693 ms 7624 KB Output is correct
75 Correct 686 ms 7688 KB Output is correct
76 Correct 629 ms 7536 KB Output is correct
77 Correct 674 ms 7360 KB Output is correct
78 Correct 2807 ms 26428 KB Output is correct
79 Correct 2632 ms 28688 KB Output is correct
80 Correct 2857 ms 27612 KB Output is correct
81 Correct 2856 ms 27452 KB Output is correct
82 Correct 2778 ms 26788 KB Output is correct
83 Correct 2748 ms 26596 KB Output is correct
84 Correct 2780 ms 26460 KB Output is correct
85 Correct 2817 ms 26264 KB Output is correct
86 Correct 2732 ms 26624 KB Output is correct
87 Correct 2878 ms 27900 KB Output is correct
88 Correct 2761 ms 26288 KB Output is correct
89 Correct 2751 ms 26364 KB Output is correct
90 Correct 2762 ms 26900 KB Output is correct
91 Correct 2763 ms 26396 KB Output is correct
92 Correct 2766 ms 29612 KB Output is correct
93 Correct 2762 ms 27692 KB Output is correct