补题记录-BNU2019排位赛(Codeforces,problemset/problem/618/D)

题面

链接:https://codeforces.com/problemset/problem/618/D
链接2:https://codeforces.com/gym/247474/problem/D

D. Hamiltonian Spanning Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are roads in total. It takes exactly y seconds to traverse any single road.
A spanning tree is a set of roads containing exactly n - 1 roads such that it’s possible to travel between any two cities using only these roads.
Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from y to x seconds. Note that it’s not guaranteed that x is smaller than y.
You would like to travel through all the cities using the shortest path possible. Given n, x, y and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once.
Input
The first line of the input contains three integers n, x and y (2 ≤ n ≤ 200 000, 1 ≤ x, y ≤ 109).
Each of the next n - 1 lines contains a description of a road in the spanning tree. The i-th of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of the cities connected by the i-th road. It is guaranteed that these roads form a spanning tree.
Output
Print a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.
Examples
inputCopy
5 2 3
1 2
1 3
3 4
5 3
outputCopy
9
inputCopy
5 3 2
1 2
1 3
3 4
5 3
outputCopy
8
Note
In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is .
In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is .

题意:

给一个n个节点完全图,所有边权为y。给一个生成树,生成树上的每个边的权值变为x。求每个点走一遍最小权值(起点和重点任意)。

方法

分为三种情况:
第一种是x==y的情况,直接(n-1)*y即可;
第二种是x>y的情况,需要判断给的生成树是不是菊花图(一个节点的度为n-1),如果是,那么一定需要走一条边权为x的边,答案是(n-2)*y+x;
第三种情况是x<y的情况,就是尽量走多的x边。一开始我以为是生成树的直径,但是反例很容易找。然后注意到每个节点的度最多为2,再多的话就要不能了。但是不能直接遍历所有边的度然后减去大于二2的值。因为减去的边可能重了,所以减边就减多了。大佬们用的二维dp来找树种的不想交的链的最长值。可惜我看的不太懂(看懂了,不过很难想到)。也可以直接dfs来维护最多能走的边权为x的边的个数。

dfs代码

//#include<bits/stdc++.h>
#include<complex>
#include<iostream>
#include<iomanip>
#include<ostream>
#include<cstring>
#include<string.h>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include<map>
#include<cstdlib>
#include<time.h>
#include<ctime>
#include<bitset>
#define pb push_back
#define _fileout freopen("out.txt","w",stdout)
#define _filein freopen("in.txt","r",stdin)
#define ok(i) printf("ok%d\n",i)
using namespace std;
typedef double db;
typedef long long ll;
const double PI = acos(-1.0);
const ll MOD=1e9+7;
const int MAXN=2e5+10;
const int INF=0x3f3f3f3f;
const ll ll_INF=9223372036854775807;
const double eps=1e-8;
ll qm(ll a,ll b){ll ret = 1;while(b){if (b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
int n,x,y;
ll ans;
vector<int>v[MAXN];
int cnt;
int vis[MAXN];
bool dfs(int x)
{   
    vis[x]=1;
    int len=v[x].size();
    int son=2;
    for(int i=0;i<len;i++)
    {
        int y=v[x][i];
        if(vis[y])continue;
        if(dfs(y)&&son>0)
        {
            cnt++;
            son--;
        }
    }
    return son>0;
}
int main()
{
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        v[x].pb(y);
        v[y].pb(x);
    }
    if(n==2){printf("%I64d\n",(ll)x);return 0;}
    else if(n==3){printf("%I64d\n",(ll)x+y);return 0;}
    if(x<y)
    {
        dfs(1);
        ans=(ll)cnt*x+(ll)(n-1-cnt)*y;
        // printf("cnt=%d\n",cnt);
        printf("%I64d",ans);
    }
    else if(x==y)
    {
        ans=(ll)(n-1)*x;
        printf("%I64d\n",ans);
    }
    else
    {
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if((int)v[i].size()==n-1){flag=0;break;}
        }
        if(flag)ans=(ll)(n-1)*y;
        else ans=(ll)(n-2)*y+x;
        printf("%I64d",ans);
    }
    return 0;
}

DP方法

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1 << 18;
vector<int> adj[maxn];
int dp[maxn][2];

void dfs(int u, int p) {
	int cal = 0, mx[2] = { 0 };
	for (const auto& v : adj[u]) if (v != p) {
		dfs(v, u);
		cal += dp[v][1];
		if (mx[1] < dp[v][0] + 1 - dp[v][1]) mx[1] = dp[v][0] + 1 - dp[v][1];
		if (mx[0] < mx[1]) swap(mx[0], mx[1]);
	}
	dp[u][0] = cal + mx[0];
	dp[u][1] = dp[u][0] + mx[1];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;//可以从这儿开始看
	int64_t x, y;
	cin >> n >> x >> y;
	for (int i = 1; i != n; ++i) {
		int u, v;
		cin >> u >> v;
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}
	if (n == 2) return cout << x << endl, 0;
	if (n == 3) return cout << x + y << endl, 0;
	if (x > y) {
		int mx = 0;
		for (int u = 1; u <= n; ++u) mx = max(mx, int(adj[u].size()));
		cout << (n - 1) * y + (mx == n - 1 ? x - y : 0) << endl;
	} else {
		dfs(1, 0);
		cout << (n - 1 - dp[1][1]) * y + dp[1][1] * x << endl;
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值