Submission #64790396


Source Code Expand

#include <bits/stdc++.h>
 
using namespace std;

const int MOD=1e9+7;

vector<int> get_dist(vector<vector<int>> & tree, int x){
	vector<int> ans(tree.size(),-1);
	queue<int> q;
	ans[x]=0;
	q.push(x);
	while(q.size()){
		int cur=q.front();
		q.pop();
		for(int y:tree[cur]){
			if(ans[y]==-1){
				ans[y]=ans[cur]+1;
				q.push(y);
			}
		}
	}
	return ans; 
	
	
}

vector<int> get_da(vector<vector<int>> & tree){
	vector<int> ans(tree.size());
	vector<int> temp=get_dist(tree,1);
	
	int d1=max_element(temp.begin(),temp.end())-temp.begin();
	
	vector<int> temp2=get_dist(tree,d1);
	
	int d2=max_element(temp2.begin(),temp2.end())-temp2.begin();
	
	vector<int> temp3=get_dist(tree,d2);
	
	for(int i=1; i<tree.size(); i++){
		ans[i]=max(temp2[i],temp3[i]);
	}
	return ans; 
	
	
}


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
	int n1;
	cin>>n1; 
	vector<vector<int>> tree1(n1+1);
	for(int i=0; i<n1-1; i++){
		int x,y;
		cin>>x>>y;
		tree1[x].push_back(y);
		tree1[y].push_back(x);
		
	}
	
	int n2;
	cin>>n2; 
	vector<vector<int>> tree2(n2+1);
	for(int i=0; i<n2-1; i++){
		int x,y;
		cin>>x>>y;
		tree2[x].push_back(y);
		tree2[y].push_back(x);
		
	}
	
	vector<int> da = get_da(tree1), da2 = get_da(tree2);
	
	vector<int> v1(da.begin()+1,da.end()), v2(da2.begin()+1,da2.end());
	
	sort(v1.begin(),v1.end());
	sort(v2.begin(),v2.end());
	long long ans=0, tot=0;
	
	long long base=max(v1.back(),v2.back());
	
	for(int i=0,j=n2; i<n1; i++){
		long long x=v1[i];
		while(j&&v2[j-1]+v1[i]>=base){
			tot+=v2[--j];
			
		}
		
		ans+=base*j+(n2-j)*(x+1)+tot;
	}
	cout<<ans; 
	
	
	
	
    
    return 0;
}

Submission Info

Submission Time
Task F - Add One Edge 3
User usernameson
Language C++ 20 (gcc 12.2)
Score 500
Code Size 1711 Byte
Status AC
Exec Time 177 ms
Memory 30888 KiB

Compile Error

Main.cpp: In function ‘std::vector<int> get_da(std::vector<std::vector<int> >&)’:
Main.cpp:39:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::vector<int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   39 |         for(int i=1; i<tree.size(); i++){
      |                      ~^~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 49
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3480 KiB
00_sample_01.txt AC 1 ms 3384 KiB
01_random_00.txt AC 139 ms 29272 KiB
01_random_01.txt AC 26 ms 9224 KiB
01_random_02.txt AC 14 ms 6552 KiB
01_random_03.txt AC 2 ms 3940 KiB
01_random_04.txt AC 124 ms 27212 KiB
01_random_05.txt AC 76 ms 19152 KiB
01_random_06.txt AC 54 ms 14860 KiB
01_random_07.txt AC 43 ms 13216 KiB
01_random_08.txt AC 56 ms 15436 KiB
01_random_09.txt AC 45 ms 13720 KiB
01_random_10.txt AC 84 ms 30888 KiB
01_random_11.txt AC 35 ms 16180 KiB
01_random_12.txt AC 67 ms 25224 KiB
01_random_13.txt AC 16 ms 8544 KiB
01_random_14.txt AC 65 ms 24600 KiB
01_random_15.txt AC 20 ms 11112 KiB
01_random_16.txt AC 49 ms 20508 KiB
01_random_17.txt AC 33 ms 14580 KiB
01_random_18.txt AC 87 ms 30876 KiB
01_random_19.txt AC 69 ms 24976 KiB
01_random_20.txt AC 134 ms 28832 KiB
01_random_21.txt AC 81 ms 19832 KiB
01_random_22.txt AC 29 ms 10268 KiB
01_random_23.txt AC 31 ms 10416 KiB
01_random_24.txt AC 61 ms 16384 KiB
01_random_25.txt AC 70 ms 17816 KiB
01_random_26.txt AC 78 ms 19532 KiB
01_random_27.txt AC 65 ms 17024 KiB
01_random_28.txt AC 60 ms 16172 KiB
01_random_29.txt AC 30 ms 9960 KiB
01_random_30.txt AC 149 ms 28700 KiB
01_random_31.txt AC 141 ms 28004 KiB
01_random_32.txt AC 89 ms 20008 KiB
01_random_33.txt AC 108 ms 22936 KiB
01_random_34.txt AC 63 ms 15464 KiB
01_random_35.txt AC 24 ms 8196 KiB
01_random_36.txt AC 52 ms 13596 KiB
01_random_37.txt AC 37 ms 10508 KiB
01_random_38.txt AC 40 ms 11552 KiB
01_random_39.txt AC 43 ms 11688 KiB
01_random_40.txt AC 143 ms 29236 KiB
01_random_41.txt AC 156 ms 28812 KiB
01_random_42.txt AC 177 ms 28708 KiB
01_random_43.txt AC 169 ms 28692 KiB
01_random_44.txt AC 159 ms 28888 KiB
01_random_45.txt AC 1 ms 3400 KiB
01_random_46.txt AC 70 ms 16912 KiB