【模板】栈
#include <stack>
#include <string>
#include <iostream>
using namespace std;
int main() {
cin.tie(0);
int t, n;
for (cin >> t; t; --t) {
stack<unsigned long long int> s;
for (cin >> n; n; --n) {
string t;
cin >> t;
if (t == "push") {
unsigned long long x;
cin >> x;
s.push(x);
} else if (t == "pop") {
if (s.empty()) {
cout << "Empty\n";
} else {
s.pop();
}
} else if (t == "query") {
if (s.empty()) {
cout << "Anguei!\n";
} else {
cout << s.top() << '\n';
}
} else {
cout << s.size() << '\n';
}
}
}
return 0;
}
题目:link
【模板】队列
#include<iostream>
using namespace std;
int n, x, m;
int queue[10005], head = 1, tail = 0;
int main() {
cin >> n;
while (n--) {
cin >> x;
switch (x) {
case 1:
cin >> m;
queue[++tail] = m;
break;
case 2:
if (head > tail) {
cout << "ERR_CANNOT_POP" << '\n';
} else {
head++;
}
break;
case 3:
if (head > tail) {
cout << "ERR_CANNOT_QUERY" << '\n';
} else {
cout << queue[head] << '\n';
}
break;
case 4:
cout << tail - head + 1 << '\n';
break;
}
}
return 0;
}
题目:link
【模板】Floyd 算法
#include <bits/stdc++.h>
using namespace std;
int n, m;
int u, v, w;
int l[105][105];
int main() {
memset(l, 0x3f, sizeof(l));
for (int i = 1; i <= 100; i++) {
l[i][i] = 0;
}
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> w;
l[u][v] = w;
l[v][u] = w;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
for (int k = 1; k <= n; k++) {
if (l[j][k] > l[i][j] + l[i][k]) {
l[j][k] = l[i][j] + l[i][k];
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << l[i][j] << " ";
}
cout << '\n';
}
return 0;
}
题目:link
【模板】传递闭包
#include <bits/stdc++.h>
using namespace std;
int n, m;
int u, v, w;
int l[105][105];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> l[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
for (int k = 1; k <= n; k++) {
l[j][k] |= (l[j][i] & l[i][k]);
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << l[i][j] << " ";
}
cout << '\n';
}
return 0;
}
题目:link
【模板】快速幂 || 取余运算
#include <bits/stdc++.h>
using namespace std;
long long pow_(long long a, int b, int p) {
long long s = 1;
while (b) {
if (b & 1) {
s *= a % p, s %= p;
}
a = a % p * a % p;
a %= p;
b /= 2;
s %= p;
}
return s % p;
}
int main() {
int a, b, p;
cin >> a >> b >> p;
printf("%lld^%lld mod %lld=%lld", a, b, p, pow_(a, b, p));
return 0;
}
题目:link
【模板】并查集
#include <bits/stdc++.h>
using namespace std;
int size[10005];
int fa[10005];
int FF(int x) {
return x == fa[x] ? x : fa[x] = FF(fa[x]);
}
void add(int u, int v) {
u = FF(u), v = FF(v);
if (u == v) return;
if (size[u] < size[v]) swap(u, v);
fa[v] = u;
size[u] += size[v];
}
int main() {
for (int i = 1; i <= 10000; i++) fa[i] = i;
fill(size + 1, size + 10001, 0);
int n, m;
int z, x, y;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &z, &x, &y);
if (z == 1) {
add(x, y);
} else {
if (FF(x) == FF(y))
cout << 'Y' << endl;
else
cout << 'N' << endl;
}
}
return 0;
}
题目:link
标签:各种,int,namespace,cin,long,include,模板 From: https://www.cnblogs.com/Meng-ke-ai/p/mu-ban.html