Submission #1835652


Source Code Expand

#include<bits/stdc++.h>
#define debug(x) cout<<#x<<"="<<x<<endl
typedef long long ll;
using namespace std;
const int maxn = 200009;

int fa[maxn],siz[maxn],son[maxn][2];
struct line{int from,to,val;}a[maxn];
int n,tot;
ll ans[maxn],val[maxn];

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
bool cmp(const line &x,const line &y){return x.val>y.val;}

void dfs(int x)
{
	int L=son[x][0],R=son[x][1];
	if(L+R==0) return ;
	ans[L]=ans[R]=ans[x];
	ans[L]+=siz[R]*val[x];
	ans[R]+=siz[L]*val[x];
	dfs(L);dfs(R);
}

int main()
{
	n=read();tot=n;
	for(int i=1;i<=n;i++) siz[i]=1,fa[i]=i;
	for(int i=1;i<n;i++) a[i]=(line){read(),read(),read()};
	sort(a+1,a+n,cmp);
	for(int i=1;i<n;i++)
	{
		int x=find(a[i].from),y=find(a[i].to),z=a[i].val;
		tot++;fa[tot]=tot;siz[tot]=siz[x]+siz[y];val[tot]=z;
		fa[x]=fa[y]=tot;
		son[tot][0]=x;son[tot][1]=y;
	}
	dfs(tot);
	for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
	return 0;
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User Hash_table
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1147 Byte
Status AC
Exec Time 49 ms
Memory 11136 KB

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 200 / 200 200 / 200 400 / 400
Status
AC × 3
AC × 10
AC × 9
AC × 32
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt
Subtask2 00_example_02.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 3 ms 6400 KB
00_example_02.txt AC 2 ms 6400 KB
00_example_03.txt AC 2 ms 6400 KB
s1_01.txt AC 3 ms 6400 KB
s1_02.txt AC 3 ms 6400 KB
s1_03.txt AC 3 ms 6400 KB
s1_04.txt AC 3 ms 6400 KB
s1_05.txt AC 3 ms 6400 KB
s1_06.txt AC 3 ms 6400 KB
s1_07.txt AC 2 ms 4352 KB
s2_08.txt AC 29 ms 8704 KB
s2_09.txt AC 15 ms 7552 KB
s2_10.txt AC 10 ms 7296 KB
s2_11.txt AC 34 ms 9088 KB
s2_12.txt AC 34 ms 8960 KB
s2_13.txt AC 31 ms 8704 KB
s2_14.txt AC 29 ms 8704 KB
s3_15.txt AC 42 ms 9344 KB
s3_16.txt AC 22 ms 7808 KB
s3_17.txt AC 14 ms 7424 KB
s3_18.txt AC 22 ms 7936 KB
s3_19.txt AC 48 ms 9728 KB
s3_20.txt AC 47 ms 9472 KB
s3_21.txt AC 48 ms 9728 KB
s3_22.txt AC 49 ms 9728 KB
s3_23.txt AC 45 ms 9216 KB
s3_24.txt AC 39 ms 9344 KB
s3_25.txt AC 45 ms 11008 KB
s3_26.txt AC 39 ms 9344 KB
s3_27.txt AC 45 ms 11136 KB
s3_28.txt AC 39 ms 9344 KB
s3_29.txt AC 45 ms 11136 KB