啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
头文件
#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>//sort降序greater<int>() 升序less<int>() 需要
#include<iostream>
#include<list>
#include<iomanip>//cout输出格式控制
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdlib.h>
#include<string>
#include<utility>
#include<vector>
const double pi = acos(-1.0);
typedef long long ll;
程序判定结果
- Compiling:代码正在后台编译
- Restricted Function:代码中使用了不安全的函数
- Compilation Error:代码编译错误
- Running:程序运行中
- Time Limit Exceeded:超时
- Memory Limit Exceeded:内存超限
- Runtime Error:SIGFPE:程序运行时错误:浮点数异常
- Runtime Error:SIGSEGV:程序运行时错误:段错误
- Presentation Error:格式错误
- Accepted:程序正确
- Wrong Answer:程序不正确
输入读取文件
freopen("C:\\Users\\Ww\\Desktop\\1.txt","r",stdin);
关闭同步/解除绑定
std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
( a % c = d , b % c = d )→(a - b) % c =0
证明:
我们现在有两个数 a,b 同余 ( a % c = d , b % c = d )
那么 a = xc + d,b = yc + d
那么 a - b = (x - y)c
可证得 (a - b) % c =0
裴蜀定理
若a,b是整数,且gcd(a,b)=d
对于任意整数x,y,ax+by=md
特别地,一定存在整数x,y,使ax+by=d成立(即m=1)
重要推论:a,b互质的充分必要条件是存在整数x,y使ax+by=1
扩展欧几里得定理(Extended Euclidean algorithm, EXGCD)
递归法
//常用于求x,y使得gcd(a, b) = a * x + b * y;
int exgcd(int a, int b, int &x, int &y)
{
if (!b)
{
x = 1;
y = 0;
return a;
}
int d = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - (a / b) * y;
return d;
}
迭代法
// C++11 新特性
//代码运行速度将比递归代码快一点。
int exgcd(int a, int b, int& x, int& y)
{
x = 1, y = 0;
int x1 = 0, y1 = 1, a1 = a, b1 = b;
while (b1)
{
int q = a1 / b1;
tie(x, x1) = make_tuple(x1, x - q * x1);
tie(y, y1) = make_tuple(y1, y - q * y1);
tie(a1, b1) = make_tuple(b1, a1 - q * b1);
}
return a1;
}
快速幂
long long fastPower(long long base, long long power)
{
long long result = 1;
while (power > 0)
{
if (power & 1) //此处等价于if(power%2==1)
result = result * base % 1000;
power >>= 1; //此处等价于power=power/2
base = (base * base) % 1000;
}
return result;
}
数字字符串转换
#include<iostream>
#include<sstream>
using namespace std;
//数字to字符串
int main()
{
int x; string s; stringstream ss;
cin >> x;
ss << x;//数字流入ss
ss >> s;//ss流出字符串
cout << s;
}
//函数类型
string itos(int x,string s)
{
stringstream ss;
ss << x;//数字流入ss
ss >> s;//ss流出字符串
return s;
}
#include<iostream>
#include<sstream>
using namespace std;
//字符串to数字
int main()
{
int x; string s; stringstream ss;
cin >> s;
ss << s;//字符串流入ss
ss >> x;//ss流出数字
cout << x;
}
//函数类型
int stoi(int x,string s)
{
stringstream ss;
ss << s;//字符串流入ss
ss >> x;//ss流出数字
return x;
}
未整理
//====================并查集================//
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int fa[N];
int n, m;
void init()
{
for(int i=1;i<=n;++i)
fa[i] = i;
}
int find(int x)//返回祖先节点 此为路径压缩版本 每次都让一个节点的父节点变成根节点(祖先节点)
{
if(fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main()
{
scanf("%d%d",&n,&m);
init();
char c;
int a,b;
for(int i=1;i<=m;++i)
{
cin >> c >> a >> b;
if(c == 'M') fa[find(a)] = find(b);
else{
if(find(a) == find(b)) printf("Yes\n");
else printf("No\n");
}
}
}
//=========朴素Prim算法求最小生成树O(n^2)============//
#include <bits/stdc++.h>
using namespace std;
const int N = 510,INF = 0x3f3f3f3f;
int n,m;
int g[N][N];
int dist[N];
bool st[N];
int prim()
{
memset(dist,0x3f,sizeof dist);
int res = 0;
for(int i=0;i<n;++i)
{
int t = -1;
for(int j=1;j<=n;++j)
{
if(!st[j] && (t == -1 || dist[t] > dist[j]))
{
t = j;
}
}
if(i && dist[t] == INF) return INF;//必须同时满足:不是第一个点 且 有边连向集合
if(i)res += dist[t];
for(int j=1;j<=n;++j) dist[j] = min(dist[j],g[j][t]);
st[t] = true;
}
return res;
}
int main()
{
cin >> n >> m;
memset(g,0x3f,sizeof g);
for(int i=1;i<=m;++i)
{
int u,v,w;
cin >> u >> v >> w;
g[u][v] = min(g[u][v],w);
g[v][u] = min(g[v][u],w);
}
int ans = prim();
if(ans == INF)puts("impossible");
else cout << ans;
}
//==========堆优化版本prim求最小生成树O(nlogm)===========//
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 510, MAXM = 2 * 1e5 + 10, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
int h[MAXM], e[MAXM], w[MAXM], ne[MAXM], idx;
bool vis[MAXN];
int n, m;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int Prim()
{
memset(vis, false, sizeof vis);
int sum = 0, cnt = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, 1});
while (!q.empty())
{
auto t = q.top();
q.pop();
int ver = t.second, dst = t.first;
if (vis[ver]) continue;
vis[ver] = true, sum += dst, ++cnt;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (!vis[j]) {
q.push({w[i], j});
}
}
}
if (cnt != n) return INF;
return sum;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < m; ++i)
{
int a, b, w;
cin >> a >> b >> w;
add(a, b, w);
add(b, a, w);
}
int t = Prim();
if (t == INF) cout << "impossible" << endl;
else cout << t << endl;
}
//=========kruskal求最小生成树O(mlogm)==========//
#include <bits/stdc++.h>
using namespace std;
// const int N = 1e5+5;
const int M = 2e5+5;
int fa[M];
int n,m;
struct node
{
int u,v,w;
bool operator <(const node& b)const{
return w < b.w;
}
}a[M];
void init()
{
for(int i=1;i<=n;++i) fa[i] = i;
}
int find(int x)
{
if(fa[x] != x)fa[x] = find(fa[x]);
return fa[x];
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
a[i] = {u,v,w};
}
sort(a+1,a+1+m);
int cnt = 0;
int ans = 0;
for(int i=1;i<=m;++i)
{
int u = a[i].u, v = a[i].v, w = a[i].w;
int pa = find(u),pb = find(v);
if(pa != pb)
{
fa[pa] = pb;
ans += w;
++cnt;
}
}
if(cnt < n-1)cout << "impossible";
else cout << ans;
}