• 蓝桥杯 大臣的旅费


    看完题,大概就懂是求树的直径了,然后求树的直径,就自己选方法了。。。。。。(从网上抄的)

    #include<bits/stdc++.h>
    
    #define LL long long
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    
    using namespace std;
    
    LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
    LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
    LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    const int N=1e5+3;
    LL dp[N],ans;
    int n;
    vector<pair<int,int> >v[N];
    int vis[N];
    void dfs(int now){
    	vis[now]=1;
    	for(int i=0;i<v[now].size();i++){
    		pair<int,int>k=v[now][i];
    		if(vis[k.fi])continue;
    		dfs(k.fi);
    		ans=max(ans,dp[now]+dp[k.fi]+k.se);
    		dp[now]=max(dp[now],dp[k.fi]+k.se);
    	}
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>n;
    	for(int i=1;i<n;i++){
    		int s,t,w;
    		cin>>s>>t>>w;
    		v[s].pb({t,w});
    		v[t].pb({s,w});
    	}
    	dfs(1);
    	LL res=0;
    	ans=10*ans+(1ll+ans)*ans/2;
    	cout<<ans<<endl;
    	return 0;
    }
    
  • 相关阅读:
    重拾数学--初中--有理数
    Python中的运算符
    PyQt5实现虚拟摇杆
    Python无重复字符的最长子串
    Python两数相加
    Python两数之和
    DBMS,B树和B+树
    浮点数表示
    Lamada表达式
    Java编程思想P159页的错误
  • 原文地址:https://www.cnblogs.com/pubgoso/p/10759719.html
Copyright © 2020-2023  润新知