QQ密码:原洛谷账户名zhongyuan…
我的steam账户:zhongyuan8812
密码:我的手机号
Intel密码:1Q2w3e4r!
#include <stdio.h>
#include <stdlib.h>
int main() {
system("shutdown -s -t 6");
return 0;
}在c++打开上面这个并运行
int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}std::ios::sync_with_stdio(0);
std::cin.tie(0);最小生成树代码:
#include<bits/stdc++.h>
using namespace std;
int a[5005];
int find(int x){
if(a[x]==x) return x;
return a[x]=find(a[x]);
}
int main(){
int n,m;
cin>>n;
while(n!=0){
cin>>m;
int ans=0;
for(int i=1;i<=n;i++){
a[i]=i;
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
int fx=find(x),fy=find(y);
if(fx!=fy){
a[fx]=fy;
}
}
for(int i=1;i<=n;i++){
if(a[i]==i) ans++;
}
cout<<ans<<endl;
cin>>n;
}
return 0;
}平衡树代码:
#include<bits/stdc++.h>
using namespace std;
// 定义树的节点
struct TreeNode{
int value; // 节点的值
TreeNode* left; // 左子树
TreeNode* right; // 右子树
int height; // 节点的高度
};
// 计算节点的高度
int getHeight(TreeNode* node){
if(node==nullptr) return 0; // 如果节点为空
return node->height;
}
// 计算节点的平衡因子(左子树高度 - 右子树高度)
int getBalance(TreeNode* node) {
if (node == nullptr) { // 如果节点为空
return 0;
}
return getHeight(node->left) - getHeight(node->right);
}
// 创建一个新节点
TreeNode* createNode(int value) {
TreeNode* newNode = new TreeNode(); // 分配内存
newNode->value = value; // 设置节点的值
newNode->left = nullptr; // 左子树为空
newNode->right = nullptr; // 右子树为空
newNode->height = 1; // 初始高度为 1
return newNode;
}
// 右旋操作(解决左子树过高的问题)
TreeNode* rightRotate(TreeNode* y) {
TreeNode* x = y->left; // x 是 y 的左子树
TreeNode* T2 = x->right; // T2 是 x 的右子树
// 旋转操作
x->right = y; // y 变成 x 的右子树
y->left = T2; // T2 变成 y 的左子树
// 更新高度
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
return x; // 返回新的根节点
}
// 左旋操作(解决右子树过高的问题)
TreeNode* leftRotate(TreeNode* x) {
TreeNode* y = x->right; // y 是 x 的右子树
TreeNode* T2 = y->left; // T2 是 y 的左子树
// 旋转操作
y->left = x; // x 变成 y 的左子树
x->right = T2; // T2 变成 x 的右子树
// 更新高度
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y; // 返回新的根节点
}
// 插入节点
TreeNode* insertNode(TreeNode* node, int value) {
// 如果当前节点为空,创建一个新节点
if (node == nullptr) {
return createNode(value);
}
// 按照二叉搜索树的规则插入节点
if (value < node->value) {
node->left = insertNode(node->left, value); // 插入到左子树
} else if (value > node->value) {
node->right = insertNode(node->right, value); // 插入到右子树
} else {
return node; // 如果值已经存在,直接返回
}
// 更新当前节点的高度
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
// 检查平衡因子,判断是否需要旋转
int balance = getBalance(node);
// 左子树过高,且新节点插入到左子树的左子树
if (balance > 1 && value < node->left->value) {
return rightRotate(node); // 右旋
}
// 右子树过高,且新节点插入到右子树的右子树
if (balance < -1 && value > node->right->value) {
return leftRotate(node); // 左旋
}
// 左子树过高,且新节点插入到左子树的右子树
if (balance > 1 && value > node->left->value) {
node->left = leftRotate(node->left); // 先左旋
return rightRotate(node); // 再右旋
}
// 右子树过高,且新节点插入到右子树的左子树
if (balance < -1 && value < node->right->value) {
node->right = rightRotate(node->right); // 先右旋
return leftRotate(node); // 再左旋
}
return node; // 返回当前节点
}
// 主函数
int main(){
TreeNode* root = nullptr; // 初始化根节点为空
// 插入节点
root = insertNode(root, 10);
root = insertNode(root, 20);
root = insertNode(root, 30);
root = insertNode(root, 40);
root = insertNode(root, 50);
root = insertNode(root, 25);
return 0;
}线段树代码:
//线段树模板
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100005;
int n,m;
ll a[N],w[N*4],t[N*4];
void pushup(const int u){
w[u]=w[u*2]+w[u*2+1];
}
void build(const int u,int l,int r){
if(l==r){
w[u]=a[l];
return;
}
int m=(l+r)/2;
build(u*2,l,m);
build(u*2+1,m+1,r);
pushup(u);
}//建树
bool in(int L,int R,int l,int r){
return (l<=L)&&(R<=r);
}
bool out(int L,int R,int l,int r){
return (L>r)||(R<l);
}
void make(int u,int len,ll x){
t[u]+=x;
w[u]+=len*x;
}
void pushdown(int u,int l,int r){
int m=(l+r)/2;
make(u*2,m-l+1,t[u]);
make(u*2+1,r-m,t[u]);
t[u]=0;
}
void update(int u,int L,int R,int l,int r,ll x){
if(in(L,R,l,r)) make(u,R-L+1,x);
else if(!out(L,R,l,r)){
int m=(L+R)/2;
pushdown(u,L,R);
update(u*2,L,m,l,r,x);
update(u*2+1,m+1,R,l,r,x);
pushup(u);
}
}//区间修改
ll query(int u,int L,int R,int l,int r){
if(in(L,R,l,r)) return w[u];
else if(!out(L,R,l,r)){
int m=(L+R)/2;
pushdown(u,L,R);
return query(u*2,L,m,l,r)+query(u*2+1,m+1,R,l,r);
}
else return 0;
}//区间求和
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
for(int i=1;i<=m;i++){
ll op,x,y,k;
cin>>op>>x>>y;
if(op==1){
cin>>k;
update(1,1,n,x,y,k);
}
else cout<<query(1,1,n,x,y)<<endl;
}
return 0;
}最短路代码:
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,m,s,d[N],v[N];
struct node{
int v,w;
};
vector<node>g[N];
priority_queue<pair<int,int> > q;
void dj(){
for(int i=1;i<N;i++) d[i]=1e9;
d[1]=0;
q.push({0,s});
while(!q.empty()){
pair<int,int> t=q.top();
q.pop();
int x=t.second;
if(v[x]==1) continue;
v[x]=1;
for(int i=0;i<g[x].size();i++){
int v=g[x][i].v,w=g[x][i].w;
if(d[v]>d[x]+w){
d[v]=d[x]+w;
q.push({-d[v],v});
}
}
}
}
int main(){
cin>>n>>m>>s;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
g[x].push_back({y,z});
}
dj();
for(int i=1;i<=n;i++){
cout<<d[i]<<" ";
}
return 0;
} 强连通分量代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,dfn[20005],low[20005],v[20005],ans,ti;
vector<int>g[20005];
stack<int>q;
void tarjan(int s){
dfn[s]=low[s]=++ti;
q.push(s);
v[s]=1;
for(int i=0;i<g[s].size();i++){
int y=g[s][i];
if(dfn[y]==0){
tarjan(y);
low[s]=min(low[y],low[s]);
}
else if(v[y]==1){
low[s]=min(low[s],dfn[y]);
}
}
if(dfn[s]==low[s]){
int cnt=0;
while(!q.empty()){
int t=q.top();
q.pop();
v[t]=0;
cnt++;
if(t==s) break;
}
if(cnt>1) ans++;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
g[x].push_back(y);
}
for(int i=1;i<=n;i++){
if(dfn[i]==0) tarjan(i);
}
cout<<ans;
return 0;
}LCA代码:
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
int n,m,s,v[N],ans[N],fa[N];
vector<int>g[N];
vector<pair<int,int> >p[N];
int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void dfs(int t){
v[t]=1;
for(int i=0;i<g[t].size();i++){
int h=g[t][i];
if(!v[h]){
dfs(h);
fa[h]=t;
}
}
for(int i=0;i<p[t].size();i++){
int y=p[t][i].first;
if(v[y]) ans[p[t][i].second]=find(y);
}
}
int main(){
cin>>n>>m>>s;
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
p[a].push_back({b,i});
p[b].push_back({a,i});
}
dfs(s);
for(int i=1;i<=m;i++){
cout<<ans[i]<<endl;
}
return 0;
}拓扑排序代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5;
int n,ind[N];
queue<int>q;
vector<int>g[N],a;
string s;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int x;
while(cin>>x&&x!=0){
g[i].push_back(x);
ind[x]++;
}
}
for(int i=1;i<=n;i++){
if(ind[i]==0) q.push(i);
}
while(!q.empty()){
int u=q.front();
q.pop();
a.push_back(u);
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
ind[v]--;
if(ind[v]==0) q.push(v);
}
}
for(int i=0;i<a.size();i++){cout<<a[i]<<" ";}
return 0;
} 并查集:
#include<bits/stdc++.h>
using namespace std;
int n,m,f[10005];
int find(int i){
if(f[i]==i) return i;
return f[i]=find(f[i]);
}
void un(int x,int y){
f[find(x)]=find(y);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){f[i]=i;}
for(int i=1;i<=m;i++){
int op,x,y;
cin>>op>>x>>y;
if(op==1) un(x,y);
else{
if(find(x)==find(y)) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}欧拉路径代码:
#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int n,m,u,v,d[N],du[N][2],s=1,cnt[2],flag;
stack<int>st;
vector<int>g[N];
void dfs(int w){
for(int i=d[w];i<g[w].size();i=d[w]){
d[w]=i+1;
dfs(g[w][i]);
}
st.push(w);
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u>>v;
g[u].push_back(v);
du[u][1]++;
du[v][0]++;
}
for(int i=1;i<=n;i++) sort(g[i].begin(),g[i].end());
for(int i=1;i<=n;i++){
if(du[i][1]!=du[i][0]){
flag=1;
if(du[i][1]-du[i][0]==1) cnt[1]++,s=i;
else if(du[i][0]-du[i][1]==1) cnt[0]++;
else{
cout<<"No";
return 0;
}
}
}
if(flag&&!(cnt[0]==cnt[1]&&cnt[0]==1)){
cout<<"No";
return 0;
}
dfs(s);
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
return 0;
}多重背包:
#include<bits/stdc++.h>
using namespace std;
long long n,m,w[1005],v[1005],s[1005],f[10005];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>w[i]>>v[i]>>s[i];
}
for(int i=1;i<=n;i++){
for(int j=m;j>=w[i];j--){
for(int k=0;k<=s[i]&&k*w[i]<=j;k++)
f[j]=max(f[j],f[j-w[i]*k]+v[i]*k);
}
}
cout<<f[m];
return 0;
}