列表

详情


NC24751. [USACO 2010 Nov G]Visiting Cows

描述

After many weeks of hard work, Bessie is finally getting a vacation! After many weeks of hard work, Bessie is finally getting a vacation! numbered 1..N. The cows have set up quite an unusual road network with exactly N-1 roads connecting pairs of cows C1 and C2 (1 <= C1 <= N; 1 <= C2 <= N; C1 != C2) in such a way that there exists a unique path of roads between any two cows.
FJ wants Bessie to come back to the farm soon; thus, he has instructed Bessie that if two cows are directly connected by a road, she may not visit them both. Of course, Bessie would like her vacation to be as long as possible, so she would like to determine the maximum number of cows she can visit.

输入描述

* Line 1: A single integer: N
* Lines 2..N: Each line describes a single road with two space-separated integers: C1 and C2

输出描述

* Line 1: A single integer representing the maximum number of cows that Bessie can visit.

示例1

输入:

7 
6 2 
3 4 
2 3 
1 2 
7 6 
5 6 

输出:

4

说明:

Bessie knows 7 cows. Cows 6 and 2 are directly connected by a road, as are cows 3 and 4, cows 2 and 3, etc.
Bessie can visit four cows. The best combinations include two cows on the top row and two on the bottom. She can't visit cow 6 since that would preclude visiting cows 5 and 7; thus she visits 5 and 7. She can also visit two cows on the top row: {1,3}, {1,4}, or {2,4}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 49ms, 内存消耗: 14812K, 提交时间: 2022-09-04 10:56:53

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+10;
int n,dp[N][2];
vector<int> V[N];
void dfs(int u,int fa)
{
    dp[u][1]=1;
    for(int i=0;i<V[u].size();i++)
    {
        int v=V[u][i];
        if(v==fa) continue;
        dfs(v,u);
        dp[u][1]+=dp[v][0];
        dp[u][0]+=max(dp[v][1],dp[v][0]);
    }
}
signed main()
{
    cin>>n;
    for(int i=1,u,v;i<n;i++)
    {
        cin>>u>>v;
        V[u].push_back(v);
        V[v].push_back(u);
    }
    dfs(1,0);
    cout<<max(dp[1][0],dp[1][1]);
    return 0;
}

C++14(g++5.4) 解法, 执行用时: 33ms, 内存消耗: 7256K, 提交时间: 2019-10-22 19:18:56

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+5;
vector<int>a[N];
int dp[N][2];
void dfs(int x,int fa){
	dp[x][1]=1;
	for(int i=0;i<a[x].size();i++){
		int y=a[x][i];
		if(y==fa)continue;
		dfs(y,x);
		dp[x][0]+=max(dp[y][1],dp[y][0]);
		dp[x][1]+=dp[y][0];
	}
}
int main(){
	int n;scanf("%d",&n);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		a[x].push_back(y);a[y].push_back(x);
	}
	dfs(1,0);
	printf("%d\n",max(dp[1][0],dp[1][1]));
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 55ms, 内存消耗: 4728K, 提交时间: 2020-02-26 20:58:56

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int dp[N][2];
vector<int>G[N];
int n;
void dfs(int u,int fa)
{
	dp[u][1]++;
	for(int v:G[u])
	{
		if(v==fa) continue;
		dfs(v,u);
		dp[u][1]+=dp[v][0];
		dp[u][0]+=max(dp[v][1],dp[v][0]);
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<n;++i)
	{
		int x,y;
		cin>>x>>y;
		G[x].push_back(y);
		G[y].push_back(x);
		
	}
	dfs(1,0);
	printf("%d\n",max(dp[1][0],dp[1][1]));
}

上一题