Submission #1869785


Source Code Expand

//while (clock()<=69*CLOCKS_PER_SEC)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

const int nax=1000*10007;
using ll=long long;

int n;
vector < pair<int,ll> > graf[nax];

ll tab[nax];

ll wyn;


int bylcen[nax];

vector <int> spo;
int roz[nax];
int maxroz[nax];

vector < pair <int,ll> > ter;

vector < pair <ll , pair <int,int> > > kra;

int oj[nax];

int fin(int v)
{
	if (v!=oj[v])
		oj[v]=fin(oj[v]);
	return oj[v];
}

bool mniej(pair <int,ll> a, pair <int,ll> b)
{
	return a.second<b.second;
}

void dfs1(int v, int oj)
{
	roz[v]=1;
	maxroz[v]=0;
	spo.push_back(v);
	for (auto i : graf[v])
	{
		if(i.first==oj || bylcen[i.first])
			continue;
		dfs1(i.first, v);
		roz[v]+=roz[i.first];
		maxroz[v]=max(maxroz[v], roz[i.first]);
	}
}

void dfs2(int v, int oj, ll odl)
{
	ter.push_back({v, odl+tab[v]});
	for (auto i : graf[v])
	{
		if(i.first==oj || bylcen[i.first])
			continue;
		dfs2(i.first, v, odl+i.second);
	}
}

void szuk(int v)
{
	if (bylcen[v])
		return;
	spo.clear();
	dfs1(v, 0);
	
	int c=-1;
	int ss=spo.size();
	for (int i : spo)
		if (2*maxroz[i]<=ss && 2*(ss-roz[i])<=ss)
			c=i;
	
	bylcen[c]=1;
	
	ter.clear();
	dfs2(c, 0, 0LL);
	
	sort(ter.begin(), ter.end(), mniej);
	
	for (auto i : ter)
		kra.push_back({i.second+ter[0].second, {i.first, ter[0].first}});
	
	for (auto i : graf[c])
		szuk(i.first);
}

int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%lld", &tab[i]);
	for (int i=1; i<n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		graf[a].push_back({b, c});
		graf[b].push_back({a, c});
	}
	szuk(1);
	
	for (int i=1; i<=n; i++)
		oj[i]=i;
	
	sort(kra.begin(), kra.end());
	
	for (auto i : kra)
	{
		int x=fin(i.second.first);
		int y=fin(i.second.second);
		if (x!=y)
		{
			oj[x]=y;
			wyn+=i.first;
		}
	}
	
	printf("%lld\n", wyn);
	return 0;
}

Submission Info

Submission Time
Task J - Tree MST
User Stonefeang
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3056 Byte
Status MLE
Exec Time 1035 ms
Memory 340448 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:134:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
./Main.cpp:136:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &tab[i]);
                         ^
./Main.cpp:140:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
                              ^

Judge Result

Set Name sample All
Score / Max Score 0 / 0 0 / 1900
Status
AC × 3
AC × 10
MLE × 86
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
All sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt, 01-57.txt, 01-58.txt, 01-59.txt, 01-60.txt, 01-61.txt, 01-62.txt, 01-63.txt, 01-64.txt, 01-65.txt, 01-66.txt, 01-67.txt, 01-68.txt, 01-69.txt, 01-70.txt, 01-71.txt, 01-72.txt, 01-73.txt, 01-74.txt, 01-75.txt, 01-76.txt, 01-77.txt, 01-78.txt, 01-79.txt, 01-80.txt, 01-81.txt, 01-82.txt, 01-83.txt, 01-84.txt, 01-85.txt, 01-86.txt, 01-87.txt, 01-88.txt, 01-89.txt, 01-90.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 171 ms 244220 KB
01-02.txt AC 107 ms 244096 KB
01-03.txt AC 80 ms 244096 KB
01-04.txt AC 103 ms 246964 KB
01-05.txt MLE 885 ms 325856 KB
01-06.txt MLE 872 ms 325856 KB
01-07.txt MLE 891 ms 327520 KB
01-08.txt MLE 838 ms 326496 KB
01-09.txt MLE 825 ms 325856 KB
01-10.txt MLE 851 ms 327648 KB
01-11.txt MLE 839 ms 328160 KB
01-12.txt MLE 814 ms 327512 KB
01-13.txt MLE 857 ms 327648 KB
01-14.txt MLE 933 ms 328336 KB
01-15.txt MLE 849 ms 325856 KB
01-16.txt MLE 834 ms 327904 KB
01-17.txt MLE 823 ms 326368 KB
01-18.txt MLE 806 ms 326496 KB
01-19.txt MLE 794 ms 326496 KB
01-20.txt MLE 895 ms 327264 KB
01-21.txt MLE 885 ms 327520 KB
01-22.txt MLE 948 ms 327776 KB
01-23.txt MLE 905 ms 327264 KB
01-24.txt MLE 899 ms 327136 KB
01-25.txt MLE 895 ms 328928 KB
01-26.txt MLE 885 ms 327136 KB
01-27.txt MLE 888 ms 327648 KB
01-28.txt MLE 899 ms 328408 KB
01-29.txt MLE 894 ms 327508 KB
01-30.txt MLE 360 ms 278632 KB
01-31.txt MLE 368 ms 277612 KB
01-32.txt MLE 366 ms 278892 KB
01-33.txt MLE 374 ms 279820 KB
01-34.txt MLE 373 ms 278376 KB
01-35.txt MLE 1023 ms 339416 KB
01-36.txt MLE 1035 ms 339808 KB
01-37.txt MLE 1012 ms 338912 KB
01-38.txt MLE 1000 ms 339688 KB
01-39.txt MLE 1006 ms 339852 KB
01-40.txt MLE 983 ms 339424 KB
01-41.txt MLE 269 ms 272492 KB
01-42.txt MLE 271 ms 274284 KB
01-43.txt MLE 265 ms 272364 KB
01-44.txt MLE 267 ms 272236 KB
01-45.txt MLE 883 ms 327648 KB
01-46.txt MLE 907 ms 329440 KB
01-47.txt MLE 904 ms 327008 KB
01-48.txt MLE 1004 ms 339304 KB
01-49.txt MLE 1001 ms 339672 KB
01-50.txt MLE 996 ms 339552 KB
01-51.txt MLE 1001 ms 339040 KB
01-52.txt MLE 986 ms 338920 KB
01-53.txt MLE 991 ms 340448 KB
01-54.txt MLE 993 ms 339168 KB
01-55.txt MLE 750 ms 325964 KB
01-56.txt MLE 745 ms 325988 KB
01-57.txt MLE 708 ms 326240 KB
01-58.txt MLE 811 ms 327072 KB
01-59.txt MLE 796 ms 326624 KB
01-60.txt MLE 790 ms 327520 KB
01-61.txt MLE 723 ms 326500 KB
01-62.txt MLE 791 ms 328416 KB
01-63.txt MLE 735 ms 327524 KB
01-64.txt MLE 802 ms 327268 KB
01-65.txt MLE 807 ms 328032 KB
01-66.txt MLE 816 ms 328672 KB
01-67.txt MLE 714 ms 328036 KB
01-68.txt MLE 729 ms 325984 KB
01-69.txt MLE 752 ms 327396 KB
01-70.txt MLE 797 ms 330596 KB
01-71.txt MLE 794 ms 328164 KB
01-72.txt MLE 797 ms 328928 KB
01-73.txt MLE 651 ms 325856 KB
01-74.txt MLE 638 ms 327392 KB
01-75.txt MLE 626 ms 326244 KB
01-76.txt MLE 700 ms 328676 KB
01-77.txt MLE 731 ms 326752 KB
01-78.txt MLE 707 ms 326628 KB
01-79.txt MLE 641 ms 326240 KB
01-80.txt MLE 665 ms 328540 KB
01-81.txt MLE 678 ms 326240 KB
01-82.txt MLE 725 ms 328932 KB
01-83.txt MLE 746 ms 327776 KB
01-84.txt MLE 718 ms 327652 KB
01-85.txt MLE 655 ms 327908 KB
01-86.txt MLE 658 ms 326112 KB
01-87.txt MLE 655 ms 326116 KB
01-88.txt MLE 747 ms 328800 KB
01-89.txt MLE 725 ms 329444 KB
01-90.txt MLE 744 ms 327892 KB
sample-01.txt AC 80 ms 244096 KB
sample-02.txt AC 80 ms 244096 KB
sample-03.txt AC 80 ms 244096 KB