跳到正文

M163Comet

1496980

这个人很勤奋,什么都写|K佬|ENTP-A

在洛谷打开
资料、奖项与版本

资料

编号
1496980
抓取

奖项

2025 CSP-J
三等奖

https://www.cnblogs.com/

https://kiomet.com

01背包

C++
for (int i = 1; i <= n; i++)
  for (int l = W; l >= w[i]; l--) f[l] = max(f[l], f[l - w[i]] + v[i]);

神秘的快读

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int in()
{
    int k=0,f=1;
    char c=getchar_unlocked();
    while(c<'0'||c>'9')
    {
        if(c=='-')f=-1;
        c=getchar_unlocked();
    }
    while(c>='0'&&c<='9')k=k*10+c-'0',c=getchar_unlocked();
    return k*f;
}
void out(int x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else out(x/10),putchar(x%10+'0');
}
signed main()
{
    int n=in(),sum=0;
    while(n--)sum+=in();
    out(sum);
    return 0;
}

并查集

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

const int N = 2e5+5;
int fa[N];
int n, m;

int find(int x){
	if(fa[x] == x) return x;
	else return fa[x] = find(fa[x]); // 比起朴素代码只改了这里 
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) fa[i] = i; // 初始化
	for(int i = 1; i <= m; ++i){
		int op, x, y;
		cin >> op >> x >> y;
		if(op == 1){ // 合并操作 
			x = find(x), y = find(y); // 查询各自的代表元素
			if(x == y) continue; // 如果已经在同一个集合,跳过
			fa[x] = y; // 这里的合并顺序可以任意 
		}
		else{ // 查询操作的一种变形 
			x = find(x), y = find(y); // 查询各自的代表元素
			if(x == y) cout << "Y" << endl; // 是否在同一个集合
			else       cout << "N" << endl; 
		}
	} 
	return 0;
}

KMP讲解 https://www.cnblogs.com/lijingran/p/8619711.html

还没有收录这位用户的文章。