/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 13ms 15.578 MiB
#2 Accepted 10ms 15.238 MiB
#3 Accepted 14ms 16.375 MiB
#4 Accepted 10ms 16.25 MiB
#5 Accepted 9ms 15.25 MiB
#6 Accepted 12ms 15.594 MiB
#7 Accepted 10ms 15.602 MiB
#8 Accepted 13ms 14.848 MiB
#9 Accepted 33ms 17.25 MiB
#10 Accepted 39ms 17.098 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-10-24 09:27:15
评测时间
2017-10-24 09:27:15
评测机
分数
10
总耗时
167ms
峰值内存
17.25 MiB