跳到正文

题解

题解:P1001 A+B Problem

pengyirui约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月31日
篇幅
约 7 分钟
题目
P1001

版本

  1. v1当前

本题解介绍计算方法和“氢弹打蚊子”做法。
加号大家都学过:++。
c++ 里面加法也是这么写,计算 a+ba+b 的代码就是a+b……
放代码!

C++
#include<bits/stdc++.h>//万能头
using namespace std;//声明命名空间
int main()//主函数
{
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

补充介绍:万能头

万能头写法为#include<bits/stdc++.h>,使用后可以使用所有库的函数,建议程序开头直接使用。但是使用后不得使用一些变量名。

做法2

一个一个加……

C++
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(a>=0)for(int i=1;i<=a;i++)b++;
    else for(int i=0;i>a;i--)b--;
    cout<<b;
    return 0;
}

“大炮打蚊子”做法

如果你就这样交上去题解,那么:
很遗憾,您的《题解:P1001 A+B Problem》不符合推荐标准。原因是:不符合“大炮打蚊子”的要求。
所以,本蒟蒻写了个 Kruskal 最小生成树算法膜拜大神……

C++
#include<bits/stdc++.h>
using namespace std;
struct Edge 
{
    int u,v,weight;
    bool operator<(const Edge& other) const 
	{
        return weight<other.weight;
    }
};
class aaa {
    vector<int> parent,rank;
public:
    aaa(int n) 
	{
        parent.resize(n);
        rank.resize(n,0);
        for(int i=0;i<n;i++)parent[i]=i;
    }
    int find(int u) 
	{
        if(u!=parent[u])parent[u]=find(parent[u]);
        return parent[u];
    }
    void unite(int u,int v) 
	{
        u=find(u);
        v=find(v);
        if(u==v)return;
        if(rank[u]>rank[v])parent[v]=u;
        else 
		{
            parent[u]=v;
            if (rank[u]==rank[v])rank[v]++;
        }
    }
};
int add(int a,int b) 
{    
    vector<Edge> edges;
    edges.push_back({0,1,a});
    edges.push_back({1,2,b});
    sort(edges.begin(),edges.end());
    aaa ds(3);
    int total=0;
    for(const Edge& e:edges) 
	  {
        if(ds.find(e.u)!=ds.find(e.v)) 
		    {
            ds.unite(e.u,e.v);
            total+=e.weight;
        }
    }
    return total;
}
int main() {
    int a, b;
    cin>>a>>b;
    int ms=add(a,b);
    cout<<ms<<endl;
    return 0;
}

“氢弹打蚊子”做法

再来一个树链剖分……

C++
#include<bits/stdc++.h>
using namespace std;
struct TreeNode {
    int val;
    int size;
    int depth;
    TreeNode* parent;
    TreeNode* heavy_child;
    vector<TreeNode*> children;
    TreeNode(int v):val(v),size(1),depth(0),parent(nullptr),heavy_child(nullptr){}
};
class  joker {
    vector<int> chain;
    void dfs1(TreeNode* node,TreeNode* parent) 
    {
        node->parent=parent;
        node->depth=parent?parent->depth+1:0;
        node->size = 1;
        for (auto child : node->children) 
		{
            dfs1(child, node);
            node->size+=child->size;
            if (!node->heavy_child||child->size>node->heavy_child->size)node->heavy_child=child;
        }
    }
    void dfs2(TreeNode* node, int& sum) 
	{
        chain.push_back(node->val);
        sum+=node->val;
        if (node->heavy_child)dfs2(node->heavy_child, sum);
        for(auto child:node->children) {
            if (child!=node->heavy_child) {
                dfs2(child,sum);
            }
        }
    }
public:
    int ssuumm(TreeNode* root) 
	{
        chain.clear();
        int sum=0;
        dfs1(root, nullptr);
        dfs2(root, sum);
        return sum;
    }
};
TreeNode* build(int a, int b) {
    TreeNode* root=new TreeNode(0);
    TreeNode* nodeA=new TreeNode(a);
    TreeNode* nodeB=new TreeNode(b);
    root->children.push_back(nodeA);
    root->children.push_back(nodeB);
    return root;
}
int add(int a, int b) {
    TreeNode* tree=build(a, b);
    joker hld;
    int sum=hld.ssuumm(tree);
    return sum;
}
int main() 
{
    int a,b;
    cin>>a>>b;
    int ans=add(a,b);
    cout<<ans<<endl;
    return 0;
}
原文