답안 #514685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
514685 2022-01-18T10:58:25 Z kshitij_sodani Paths (RMI21_paths) C++14
56 / 100
600 ms 10024 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'
llo n,k;
vector<pair<llo,llo>> adj[100001];
llo ans[100001];
pair<llo,llo> val[100001];

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ord tree<pair<llo,llo>,null_type,greater<pair<llo,llo>>,rb_tree_tag,tree_order_statistics_node_update>
ord cur;
vector<llo> cur2;
void dfs(llo no,llo par=-1){
	vector<pair<llo,llo>> ss;
	for(auto j:adj[no]){
		if(j.a!=par){
			
			dfs(j.a,no);
			ss.pb({val[j.a].a+j.b,val[j.a].b});
		}
	}
	if(ss.size()==0){
		val[no]={0,no};
		return;
	}
	sort(ss.begin(),ss.end());
	for(llo j=0;j<ss.size();j++){
		if(j+1==ss.size()){
			val[no]=ss[j];
		}
		else{
			cur2.pb(ss[j].a);
			//cur.insert(ss[j]);
		}
	}
}
llo su=0;
void dfs2(llo no,llo par=-1){
	ans[no]=su;
	for(auto j:adj[no]){
		if(j.a!=par){
			int x=cur.order_of_key({val[j.a].a+j.b,val[j.a].b});
			llo su2=su;
			
			cur.erase({val[j.a].a+j.b,val[j.a].b});
			if(x<k){
				su-=(val[j.a].a+j.b);
				if(cur.size()>=k){
					su+=(*cur.find_by_order(k-1)).a;
				}
			}
			if(cur.order_of_key({val[j.a].a,val[j.a].b})<k){
				if(cur.size()>=k){
					su-=(*cur.find_by_order(k-1)).a;
				}
				su+=val[j.a].a;
			}
			cur.insert({val[j.a].a,val[j.a].b});
			dfs2(j.a,no);
			su=su2;

		}
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>k;
	llo ma=0;
	for(llo i=0;i<n-1;i++){
		llo aa,bb,cc;
		cin>>aa>>bb>>cc;
		aa--;
		bb--;
		ma=cc;
		adj[aa].pb({bb,cc});
		adj[bb].pb({aa,cc});
	}
	
	if(n==1){
		cout<<0<<endl;
		return 0;
	}
	if(n==2){
		cout<<ma<<endl;
		cout<<ma<<endl;
		return 0;
	}
	for(int i=0;i<n;i++){
		cur2.clear();
		dfs(i);
		llo ans2=0;
		//cur.insert(val[i]);
		cur2.pb(val[i].a);
		sort(cur2.begin(),cur2.end());
		reverse(cur2.begin(),cur2.end());
		for(llo j=0;j<k;j++){
			if(j>=cur2.size()){
				continue;
			}
			ans2+=cur2[j];
		}
		cout<<ans2<<endl;
	}
/*	llo ind=0;
	for(llo i=0;i<n;i++){
		if(adj[i].size()>1){
			ind=i;
		}
	}
	dfs(ind);
	k=min(k,(llo)(cur.size()));
	
	cur.insert(val[ind]);
	for(llo j=0;j<k;j++){
		su+=(*cur.find_by_order(j)).a;
	}
	for(auto j:cur){
		cout<<j.a<<":"<<j.b<<endl;
	}
	cout<<ind<<":"<<su<<endl;
	dfs2(ind);
	for(int i=0;i<n;i++){
		cout<<ans[i]<<endl;
	}
*/


 
	return 0;
}

Compilation message

Main.cpp: In function 'void dfs(llo, llo)':
Main.cpp:35:15: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for(llo j=0;j<ss.size();j++){
      |              ~^~~~~~~~~~
Main.cpp:36:9: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   if(j+1==ss.size()){
      |      ~~~^~~~~~~~~~~
Main.cpp: In function 'void dfs2(llo, llo)':
Main.cpp:56:18: warning: comparison of integer expressions of different signedness: '__gnu_pbds::detail::bin_search_tree_set<std::pair<long long int, long long int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, long long int> >, __gnu_pbds::detail::tree_traits<std::pair<long long int, long long int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, long long int> >, __gnu_pbds::tree_order_statistics_node_update, __gnu_pbds::rb_tree_tag, std::allocator<char> >, std::allocator<char> >::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long long int'} [-Wsign-compare]
   56 |     if(cur.size()>=k){
      |        ~~~~~~~~~~^~~
Main.cpp:60:48: warning: comparison of integer expressions of different signedness: '__gnu_pbds::tree_order_statistics_node_update<__gnu_pbds::detail::bin_search_tree_const_node_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >, __gnu_pbds::detail::bin_search_tree_const_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >*, std::pair<long long int, long long int>, std::pair<long long int, long long int>*, const std::pair<long long int, long long int>*, std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&, true, std::allocator<char> >, __gnu_pbds::detail::bin_search_tree_const_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >*, std::pair<long long int, long long int>, std::pair<long long int, long long int>*, const std::pair<long long int, long long int>*, std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&, true, std::allocator<char> >, std::allocator<char> >, __gnu_pbds::detail::bin_search_tree_const_node_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >, __gnu_pbds::detail::bin_search_tree_const_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >*, std::pair<long long int, long long int>, std::pair<long long int, long long int>*, const std::pair<long long int, long long int>*, std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&, true, std::allocator<char> >, __gnu_pbds::detail::bin_search_tree_const_it_<__gnu_pbds::detail::rb_tree_node_<std::pair<long long int, long long int>, long unsigned int, std::allocator<char> >*, std::pair<long long int, long long int>, std::pair<long long int, long long int>*, const std::pair<long long int, long long int>*, std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&, true, std::allocator<char> >, std::allocator<char> >, std::greater<std::pair<long long int, long long int> >, std::allocator<char> >::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long long int'} [-Wsign-compare]
   60 |    if(cur.order_of_key({val[j.a].a,val[j.a].b})<k){
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
Main.cpp:61:18: warning: comparison of integer expressions of different signedness: '__gnu_pbds::detail::bin_search_tree_set<std::pair<long long int, long long int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, long long int> >, __gnu_pbds::detail::tree_traits<std::pair<long long int, long long int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, long long int> >, __gnu_pbds::tree_order_statistics_node_update, __gnu_pbds::rb_tree_tag, std::allocator<char> >, std::allocator<char> >::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long long int'} [-Wsign-compare]
   61 |     if(cur.size()>=k){
      |        ~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:106:8: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |    if(j>=cur2.size()){
      |       ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 4 ms 2636 KB Output is correct
4 Correct 4 ms 2636 KB Output is correct
5 Correct 4 ms 2648 KB Output is correct
6 Correct 4 ms 2636 KB Output is correct
7 Correct 4 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 4 ms 2636 KB Output is correct
4 Correct 4 ms 2636 KB Output is correct
5 Correct 4 ms 2648 KB Output is correct
6 Correct 4 ms 2636 KB Output is correct
7 Correct 4 ms 2636 KB Output is correct
8 Correct 72 ms 2744 KB Output is correct
9 Correct 65 ms 2796 KB Output is correct
10 Correct 54 ms 2756 KB Output is correct
11 Correct 71 ms 2760 KB Output is correct
12 Correct 61 ms 2756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 4 ms 2636 KB Output is correct
4 Correct 4 ms 2636 KB Output is correct
5 Correct 4 ms 2648 KB Output is correct
6 Correct 4 ms 2636 KB Output is correct
7 Correct 4 ms 2636 KB Output is correct
8 Correct 72 ms 2744 KB Output is correct
9 Correct 65 ms 2796 KB Output is correct
10 Correct 54 ms 2756 KB Output is correct
11 Correct 71 ms 2760 KB Output is correct
12 Correct 61 ms 2756 KB Output is correct
13 Correct 304 ms 2828 KB Output is correct
14 Correct 296 ms 2948 KB Output is correct
15 Correct 212 ms 2884 KB Output is correct
16 Correct 302 ms 2980 KB Output is correct
17 Correct 262 ms 2764 KB Output is correct
18 Correct 176 ms 2800 KB Output is correct
19 Correct 315 ms 2756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 10024 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 4 ms 2636 KB Output is correct
4 Correct 4 ms 2636 KB Output is correct
5 Correct 4 ms 2648 KB Output is correct
6 Correct 4 ms 2636 KB Output is correct
7 Correct 4 ms 2636 KB Output is correct
8 Correct 72 ms 2744 KB Output is correct
9 Correct 65 ms 2796 KB Output is correct
10 Correct 54 ms 2756 KB Output is correct
11 Correct 71 ms 2760 KB Output is correct
12 Correct 61 ms 2756 KB Output is correct
13 Correct 304 ms 2828 KB Output is correct
14 Correct 296 ms 2948 KB Output is correct
15 Correct 212 ms 2884 KB Output is correct
16 Correct 302 ms 2980 KB Output is correct
17 Correct 262 ms 2764 KB Output is correct
18 Correct 176 ms 2800 KB Output is correct
19 Correct 315 ms 2756 KB Output is correct
20 Execution timed out 1089 ms 10024 KB Time limit exceeded
21 Halted 0 ms 0 KB -