【51nod 1737 配对】+ 链式前向星 + dfs + 输入外挂
1737 配对
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
给出一棵n个点的树,将这n个点两两配对,求所有可行的方案中配对两点间的距离的总和最大为多少。
Input
一个数n(1<=n<=100,000,n保证为偶数)
接下来n-1行每行三个数x,y,z表示有一条长度为z的边连接x和y(0<=z<=1,000,000,000)
Output
一个数表示答案
Input示例
6
1 2 1
1 3 1
1 4 1
3 5 1
4 6 1
Output示例
7
//配对方案为(1,2)(3,4)(5,6)
一条边的贡献 = 点较少的一边的点的个数 * 这条边的权值
AC代码(431ms)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int K = 1e5 + 10; 6 typedef long long LL; 7 LL n,head[K],s[K],num,ans; 8 struct node{ 9 LL to,vl,next; 10 }st[K * 2]; 11 void add(LL a,LL b,LL c){ 12 st[num] = node{b,c,head[a]}; head[a] = num++; 13 st[num] = node{a,c,head[b]}; head[b] = num++; 14 } 15 void dfs(LL x,LL f){ 16 s[x] = 1; 17 for(int i = head[x]; i != -1; i = st[i].next) 18 if(st[i].to != f) 19 dfs(st[i].to,x),s[x] += s[st[i].to],ans += min(s[st[i].to],n - s[st[i].to]) * st[i].vl; 20 } 21 int main() 22 { 23 LL a,b,c; 24 num = 0; 25 memset(head,-1,sizeof(head)); 26 scanf("%d",&n); 27 for(int i = 1; i < n; i++) 28 scanf("%lld %lld %lld",&a,&b,&c),add(a,b,c); 29 ans = 0; 30 dfs(1,0); 31 printf("%lld\n",ans); 32 return 0; 33 }
AC代码 + 输入外挂(171ms)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int K = 1e5 + 10; 7 typedef long long LL; 8 LL n,head[K],s[K],num,ans; 9 struct node{ 10 LL to,vl,next; 11 }st[K * 2]; 12 LL read(){ 13 LL x = 0, y = 1; 14 char s = getchar(); 15 while(s < '0' || s > '9') { if(s == '-') y = -1;s = getchar();} 16 while(s >= '0' && s <= '9') x = x * 10 + s - '0',s = getchar(); 17 return x * y; 18 } 19 void add(LL a,LL b,LL c){ 20 st[num] = node{b,c,head[a]}; head[a] = num++; 21 st[num] = node{a,c,head[b]}; head[b] = num++; 22 } 23 void dfs(LL x,LL f){ 24 s[x] = 1; 25 for(int i = head[x]; i != -1; i = st[i].next) 26 if(st[i].to != f) 27 dfs(st[i].to,x),s[x] += s[st[i].to],ans += min(s[st[i].to],n - s[st[i].to]) * st[i].vl; 28 } 29 void out(LL a){ 30 if(a < 0) putchar('-'), a = -a; 31 if(a >= 10) out(a / 10); putchar(a % 10 + '0'); 32 } 33 int main() 34 { 35 LL a,b,c; 36 num = 0; 37 memset(head,-1,sizeof(head)); 38 scanf("%d",&n); 39 for(int i = 1; i < n; i++) 40 a = read(),b = read(),c = read(),add(a,b,c); 41 ans = 0; 42 dfs(1,0); 43 out(ans); 44 return 0; 45 }

浙公网安备 33010602011771号