倒计时57天

3-1知识点续:

cpp 复制代码
//给string设置大小:
/*
1.s.resize(N):

resize()函数可以改变string的大小,并根据需要添加或删除字符。如果新的大小比当前大小大,将会在末尾添加字符;如果新的大小比当前大小小,将会删除末尾的字符。
*/

//例子:
void solve() {
	string s = "abcdef";
	s.resize(5);
	cout << s << endl;//输出abcde
}

/*
2.s.reserve(N):
但使用reserve()函数只是预留了存储空间,并不会改变实际的字符数量。
*/


//例子:
void solve() {
	string s = "abcdef";
	s.reserve(5);
	cout << s << endl;//输出abcdef
}
cpp 复制代码
//将string转化为long long型
void solve() {
	string s = "123";
	int a = strtoll(s.c_str(), NULL, 10);
	cout << a / 10;
}

复习3-2:习题篇:

倒计时68天-CSDN博客

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+6;
const int inf=0x3f3f3f3f;
typedef pair<int,int> pii;
vector<pii>ve[N];
int dis[N];
void dfs(int x,int fa)
{
  for(auto [u,w]:ve[x])
  {
    if(u==fa)continue;
    dis[u]=dis[x]+w;
    dfs(u,x);
  }
}
void solve()
{
	int n,cn=0;
  cin>>n;
  for(int i=1;i<n;i++)
  {
    int u,v,w;
    cin>>u>>v>>w;
    ve[u].push_back({v,w});
    ve[v].push_back({u,w});
    cn+=2*w;
  }
  dfs(1,0);
  int max1=-inf,flag;
  for(int i=1;i<=n;i++)
  {
    if(max1<dis[i])
    {
      max1=dis[i];
      flag=i;
    }
  }
  memset(dis,0,sizeof dis);
  dfs(flag,0);
  max1=-inf;
  for(int i=1;i<=n;i++)
  {
    max1=max(max1,dis[i]);
  }
  cout<<cn-max1;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	int t;
	//cin>>t;
	t=1;
	while(t--)
	{
		solve();
	}
  return 0;
}
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+6;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
vector<int>ve[N];
int dp[N][2];
void dfs(int x,int fa)
{
    dp[x][0]=1,dp[x][1]=1;
    for(auto i:ve[x])
    {
        if(i==fa)continue;
        dfs(i,x);
        dp[x][0]=dp[x][0]*dp[i][1]%mod;
        dp[x][1]=dp[x][1]*(dp[i][0]+dp[i][1])%mod;
    }
}
void solve()
{
	int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    dfs(1,0);
    cout<<(dp[1][0]+dp[1][1])%mod;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	int t;
	//cin>>t;
	t=1;
	while(t--)
	{
		solve();
	}
  return 0;
}

待续,,,

相关推荐
微尘83 分钟前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
金博客22 分钟前
Qt 模型视图(二):模型类QAbstractItemModel
c++·qt6.7.2
五味香41 分钟前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆41 分钟前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
Beauty.5681 小时前
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
数据结构·c++·算法
jimmy.hua1 小时前
C++刷怪笼(5)内存管理
开发语言·数据结构·c++
xiaobai12 31 小时前
二叉树的遍历【C++】
开发语言·c++·算法
DieSnowK1 小时前
[项目][WebServer][Makefile & Shell]详细讲解
开发语言·c++·http·makefile·shell·项目·webserver
dc爱傲雪和技术2 小时前
在 VS Code 中调试 C++ 项目
开发语言·c++
如意.7592 小时前
【C++】——多态详解
开发语言·c++