有用的板子
常用技巧
inline ll read(){
ll x = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-') w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x << 3) + (x << 1) + (ch - '0');
ch = getchar();
}
return x * w;
} // 快读
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
}// 关同步,可以用 cin 输入,用 printf 输出
//对拍
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
ll cnt=0;
int main(){
while(true){
cnt ++ ;
system("data.exe > T.in");
system("baoli.exe < T.in > baoli.out");
system("std.exe < T.in > std.out");
if(system(fc std.out baoli.out)){
cerr << "WA on test" << cnt << endl;
system("pause");
}
cerr << "AC on test" << cnt << endl;
}
}
// 离散化
std::vector<ll> base, pos;
std::sort(pos.begin(), pos.end());
pos.erase(std::unique(pos.begin(), pos.end()), pos.end());
for(int i = 0; i < n; ++i){
base[i] = std::lower_bound(pos.begin(), pos.end(), base[i]) - pos.begin();
}
图论相关
最短路
struct edge{
ll v, w;
};
struct node{
ll dis,u;
bool operator < (const node& a) const{
return dis > a.dis;
}
}
vector<edge> e[maxn];
ll dis[maxn], vis[maxn];
priority_queue<node> q;
void dijkstra(ll n,ll s){
for(int i = 1; i <= n; i++){
dis[i] = INF, vis[i] = false;
}
dis[s] = 0; q.push((node){0,s});
while(!q.empty()){
ll u = q.top(); q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto to : e[u]){
ll v = to.v, w = to.w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
p.push(node{dis[v],v});
}
}
}
}// Dijkstra
// Floyd
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
f[i][j] = std::min(f[i][j], f[i][k] + f[k][j]);
}
}
}
树的直径
void dfs(ll u,ll fa){
for(auto v : e[u]){
if(v == fa) continue;
d[v] = d[u] + 1;
if(d[v] > d[c]) c = v;
dfs(v, u);
}
}
int main(){
dfs(1,0);
d[c] = 0;
dfc(c,0);
} // 两次dfs
void dfs(ll u, ll fa){
d1[u] = d2[u] = 0;
for(auto v : e[u]){
if(v == fa) continue;
dfs(v, u);
ll tmp = d1[v] + 1;
if(t > d1[u]){
d2[u] = d1[u];
d1[u] = tmp;
}
else if(tmp > d2[u]){
d2[u] = tmp;
}
}
d = d > d1[u] + d2[u] ? d : d1[u] + d2[u];
}
int main(){
dfs(1,0);
cout << d;
}// 树形DP
数学相关
inline ll binpow(ll a,ll b){
a %= mod;
ll res = 1;
while(b > 0){
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return r
} // 快速幂
标签:std,ch,考前,ll,pos,dfs,整理,模板,dis
From: https://www.cnblogs.com/mikufun4405/p/17770274.html