HDU 2196 求树上所有点能到达的最远距离

其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了...

但是乱搞一下还是有80分....可惜没想到正解啊!

所以今天的考试题是:

巡访 (path.pas/c/cpp)

  Chanxer终于当上了“中华农民联盟”的盟主,他举目四望,决定四处走走,巡视自己的农土。

  “中华农民联盟”的成员有个村庄,在“村村通”计划中,村庄们被条道路联通了起来,Chanxer计划从某个村庄出发,访问所有的村庄。

可是Chanxer出行有一个特殊的要求,那就是必须以农车代步,现在我们知道哪些村庄配备有农车,也就是说,只有配备有农车的村庄才能够被作为出发点。

Chanxer有点懒,他想知道访问全部的村庄所要走的路程长度最小是多少。

树的节点数 n<=10^5

 

题目大意:已知一棵树,求一条满足一个端点为给定的端点的最长链。

 

其实就是求出每个点能到达的最远距离  [这就是我们的标题所给题目要求的]。

 

所以应该怎么做呢?

方法一:

  经过证明,从树上任意一个点出发到达的最远的点一定是这棵树的直径的一个端点。

  反过来,从直径的端点出发到达每个点的距离也一定是最远距离...  [我怎么就没想到...囧]

  所以先找到两个端点[先从随意一个点出发,然后这个点一定是一个端点,端点的最远点就是另一个端点],然后再跑一遍dfs就可以了[ 因为找第二个端点的时候已经跑过一遍了 ]...

  

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<ctime>
 5 #include<queue>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 
10 inline int in(){
11      int x=0;char ch=getchar();
12      while(ch>'9' || ch<'0') ch=getchar();
13      while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
14      return x;
15 }
16 
17 const int maxn=100010;
18 const int INF=0x3f3f3f3f;
19 
20 struct Node{
21     int data,next,weight;
22 }node[maxn<<1];
23 
24 #define now node[point].data
25 #define then node[point].next
26 #define www node[point].weight
27 
28 int n,cnt;
29 long long M_dis,M_sit=1,M_dis1;
30 int star[maxn];
31 int head[maxn];
32 bool vis[maxn];
33 long long ans,Sum;
34 long long f[maxn];
35 
36 inline void add(int u,int v,int w){
37     node[cnt].data=v;node[cnt].next=head[u];node[cnt].weight=w;head[u]=cnt++;
38     node[cnt].data=u;node[cnt].next=head[v];node[cnt].weight=w;head[v]=cnt++;
39 }
40 
41 int dfs(int x,long long sum){
42     vis[x]=true;
43     if(sum>M_dis)
44         M_dis=sum,M_sit=x;
45     for(int point=head[x];point!=-1;point=then)
46         if(!vis[now])
47             dfs(now,sum+www);
48     vis[x]=false;
49 }
50 
51 int dfs2(int x,long long sum){
52     vis[x]=true;f[x]=max(f[x],sum);
53     if(sum>M_dis)
54         M_dis=sum,M_sit=x;
55     for(int point=head[x];point!=-1;point=then)
56         if(!vis[now])
57             dfs2(now,sum+www);
58     vis[x]=false;
59 }
60 
61 int main(){
62     freopen("path.in","r",stdin);
63     freopen("path.out","w",stdout);
64     
65     int u,v,w,cot=0;
66     
67     n=in();
68     for(int i=1;i<=n;i++) head[i]=-1;
69     for(int i=1;i<n;i++){
70         u=in(),v=in(),w=in(),add(u,v,w);
71         Sum+=w<<1;
72     }
73     for(int i=1;i<=n;i++) star[i]=in(),cot+=star[i];
74     
75     dfs(1,0);
76     int t=M_sit;
77     dfs2(t,0);
78     dfs2(M_sit,0);
79     
80     for(int i=1;i<=n;i++)
81         if(star[i])
82             ans=max(ans,f[i]);
83     
84     ans=Sum-ans;
85     
86     printf("%lld",ans);
87     
88     return 0;
89 }
View Code

 

 

方法二:

  

详情可以见代码

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 inline int in(){
 8     int x=0;char ch=getchar();
 9     while(ch>'9' || ch<'0') ch=getchar();
10     while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
11     return x;
12 }
13 
14 const int maxn=100010;
15 
16 struct Node{
17     int data,next,weight;
18 }node[maxn<<1];
19 
20 #define now node[point].data
21 #define then node[point].next
22 #define www node[point].weight
23 
24 int n,cnt,ans,Sum;
25 int head[maxn];
26 int Max[maxn],Maxv[maxn];
27 int Smax[maxn],Smaxv[maxn];
28 
29 inline void add(int u,int v,int w){
30     node[cnt].data=v;node[cnt].next=head[u];node[cnt].weight=w;head[u]=cnt++;
31     node[cnt].data=u;node[cnt].next=head[v];node[cnt].weight=w;head[v]=cnt++;
32 }
33 
34 void dfs(int x,int p){
35     for(int point=head[x];point!=-1;point=then){
36         if(now==p) continue;
37         dfs(now,x);
38         if(Smax[x]<Max[now]+www){
39             Smax[x]=Max[now]+www,Smaxv[x]=now;
40             if(Smax[x]>Max[x]){
41                 swap(Smax[x],Max[x]);
42                 swap(Smaxv[x],Maxv[x]);
43             }
44         }
45     }
46 }
47 
48 void dfs2(int x,int p){
49     for(int point=head[x];point!=-1;point=then){
50         if(now==p) continue;
51         if(now==Maxv[x]){
52             if(Smax[now]<Smax[x]+www){
53                 Smax[now]=Smax[x]+www;Smaxv[now]=x;
54                 if(Smax[now]>Max[now]){
55                     swap(Smax[now],Max[now]);
56                     swap(Smaxv[now],Maxv[now]);
57                 }
58             }
59         }
60         else{
61             if(Smax[now]<Max[x]+www){
62                 Smax[now]=Max[x]+www;Smaxv[now]=x;
63                 if(Smax[now]>Max[now]){
64                     swap(Smax[now],Max[now]);
65                     swap(Smaxv[now],Maxv[now]);
66                 }
67             }
68         }
69         dfs2(now,x);
70     }
71 }
72 
73 int main(){
74     freopen("path.in","r",stdin);
75     freopen("path.out","w",stdout);
76     
77     int u,v,w;
78     
79     n=in();
80     for(int i=1;i<=n;i++) head[i]=-1;
81     for(int i=1;i<n;i++)
82         u=in(),v=in(),w=in(),add(u,v,w),Sum+=(w<<1);
83     
84     dfs(1,-1);
85     dfs2(1,-1);
86     
87     for(int i=1;i<=n;i++)
88         if(u=in()) ans=max(ans,Max[i]);
89             
90     printf("%d",Sum-ans);
91     
92     return 0;
93 }
View Code

另附标题的AC通道:

http://acm.hdu.edu.cn/showproblem.php?pid=2196

原文地址:https://www.cnblogs.com/Robert-Yuan/p/4857643.html