一、输入输出
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' void solve() { } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int _ = 1; // cin >> _; while(_--) { solve(); } return 0; }二、并查集
Disjoint Set Union
中文:不相交集合合并/并查集
struct DSU { vector<int> fa, sz; DSU(int n) :fa(n + 1) ,sz(n + 1, 1) { iota(fa.begin(), fa.end(), 0); } int find(int x) { while(x != fa[x]) x = fa[x] = fa[fa[x]]; return x; } int size(int x) { return sz[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } bool merge(int x, int y) { x = find(x), y = find(y); if(x == y) return false; // 如果x的集合更大,就交换x和y,保证把小的挂到大的上 if(sz[x] > sz[y]) swap(x, y); sz[y] += sz[x]; fa[x] = y; return true; } };三、快速输入输出模板
模板题:
P10815 【模板】快速读入 - 洛谷
代码:
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' inline int read() { int ret = 0, flag = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret * flag; } inline void print(int x) { if(x < 0) { putchar('-'); x = -x; } if(x > 9) print(x / 10); putchar(x % 10 + '0'); } void solve() { int n = read(); int sum = 0; for(int i = 1; i <= n; i++) { int x = read(); sum += x; } print(sum); } signed main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); int _ = 1; // cin >> _; while(_--) { solve(); } return 0; }