/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 7ms 15.0 MiB
#2 Accepted 7ms 16.34 MiB
#3 Accepted 8ms 15.625 MiB
#4 Accepted 6ms 14.875 MiB
#5 Accepted 6ms 15.5 MiB
#6 Accepted 8ms 16.375 MiB
#7 Accepted 5ms 16.125 MiB
#8 Accepted 5ms 15.117 MiB
#9 Accepted 18ms 16.977 MiB
#10 Accepted 21ms 16.465 MiB

代码

#include<bits/stdc++.h>
const int maxn=1e6;
inline const void read(int &a)
{
	a=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar();
	}
}
inline const void write(int a)
{
	if(a>=10)write(a/10);
	putchar(a%10+'0');
}
class SIDE
{
	public:
		int from,to,len,next;
		bool operator<(const SIDE&b)
		{
			if(len<b.len)return true;
			else return false;
		}
}s[maxn];
int n,ans=0,point[maxn],dis[maxn],son[maxn];
bool exist[maxn],wei[maxn];
inline const void add(int i)
{
	s[i].next=point[s[i].from];
	point[s[i].from]=i;
}
bool been[maxn];
void link_cut_tree(int p)
{
	been[p]=true;
	int side=point[p];
	while(side)
	{
		if(!been[s[side].to])
		{
			link_cut_tree(s[side].to);
			if(dis[s[side].to]+s[side].len>dis[p])
			{
				dis[p]=dis[s[side].to]+s[side].len;
				son[p]=s[side].to;
			}
		}
		side=s[side].next;
	}
}
void dfs(int p)
{
	if(wei[p])wei[son[p]]=true;
	been[p]=true;
	int side=point[p];
	while(side)
	{
		if(!been[s[side].to])
		{
			ans+=s[side].len*2;
			dfs(s[side].to);
			if(wei[s[side].to])ans-=s[side].len;
		}
		side=s[side].next;
	}
}
int main()
{
	memset(wei,false,sizeof(wei));
	memset(son,0,sizeof(son));
	memset(dis,0,sizeof(dis));
	memset(been,false,sizeof(been));
	memset(point,0,sizeof(point));
	read(n);
	for(int i=1;i<=n-1;i++)
	{
		read(s[i].from);read(s[i].to);read(s[i].len);
		add(i);
		s[i+n-1].from=s[i].to;s[i+n-1].to=s[i].from;
		s[i+n-1].len=s[i].len;exist[i+n-1]=exist[i];
		add(i+n-1);
	}
	link_cut_tree(1);
	memset(been,false,sizeof(been));
	wei[1]=true;
	dfs(1);
	write(ans);
	return 0;
}

信息

递交者
类型
递交
题目
游 T2
题目数据
下载
语言
C++
递交时间
2017-09-09 15:17:45
评测时间
2017-09-09 15:17:45
评测机
分数
10
总耗时
96ms
峰值内存
16.977 MiB