关于我的码风问题

作为OIER,码风其实是一个比较严肃的话题,每个OIER都有不同的码风,这里简单记录一下我的码风。

可能会update

概述

此码风来源于本人DimensionTripper的长期积累,以及参考过教练szwujq与ViXbob和Edgration的代码风格后形成的

预编译

#include <foo>应置于代码最开头,以C++标准库优先;工程代码中对应的.h文件应先于一切库。能使用#include <foo>的地方绝不使用#include "foo"

使用using namespace foo

define foo bar一般在using namespace foo之前,#include <foo>之后。

缩进

使用优美的4空格缩进

空格与换行

换行问题

大括号一定要换行(do-whiledo后面的大括号例外

空格问题

一定要加空格的地方

  1. 多目运算符两侧(,是例外,详情见下)
  2. ,;的右侧,只要它不位于行末。
  3. iffor等控制流关键字与其后的左括号之间
  4. 类型中*&的左侧
  5. do-while中的do与左大括号之间以及右大括号与while之间

一定不能加空格的地方

  1. 单目运算符之后
  2. 左括号右侧与右括号左侧
  3. ,;的左侧
  4. 函数名与左括号之间
  5. .的两侧

其他

若表达式过长时可换行,运算符位于行尾,缩进以对齐为准则

空行

所有#include <foo>using namespace foo;之间不应空行,之后应空一行。

一系列常量定义的上下应有空行。

函数/结构体定义根据其意义和用途酌情空行

一系列全局变量定义的上下应有空行。

语句之间可根据其意义酌情空行。

任何位置不能出现连续的两个(或以上)空行。

函数定义

main函数返回值必须为intreturn 0不可忽略。

若函数体只有一行且整个函数缩成一行后不超过80个字符,可依据当时的心情决定是否缩成一行

命名规则

一般使用英文译名,首字母大写

译文由多个单词组成的中间使用_连接,每个单词首字母都要大写

用英文缩写命名时每个字母都要大写

变量可使用单字母 / 单词 / (单词 + 数字) / (字母 + 数字)四种方式命名

绝不使用拼音(及缩写)命名

实在想不到名字的可以使用例如ViXbobEdgration等神犇常用ID命名

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
#define ri register int
#define il inline
#define ll long long
using namespace std;

const int N = 1e5 + 110;
const int inf = 0x7fffffff;
const int MAXN = 110;
const double eps = 1e-8;

il int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}

int n, m, k, cnt, tot;
int fa[N], head[N], w[N], dfn[N], top[N], dep[N], son[N], siz[N];

struct edge
{
int fr, to, weight;
};
struct Edge
{
int to, nxt;
};
edge e[N];
Edge E[N];
il bool cmp(edge x, edge y)
{
return x.weight < y.weight;
}

il int findf(int x)
{
if (fa[x] == x)
return x;
return fa[x] = findf(fa[x]);
}

il void add_edge(int u, int v)
{
E[++tot] = (Edge){v, head[u]};
head[u] = tot;
E[++tot] = (Edge){u, head[v]};
head[v] = tot;
}
il void Ex_Kruskal()
{
cnt = n;
for (ri i = 1; i <= m; i++)
{
int f1 = findf(e[i].fr);
int f2 = findf(e[i].to);
if (f1 != f2)
{
cnt++;
add_edge(cnt, f1);
add_edge(cnt, f2);
fa[f1] = cnt, fa[f2] = cnt;
w[cnt] = e[i].weight;
}
}
}

il void DFS(int x)
{
dfn[++dfn[0]] = x;
siz[x]++;
for (ri i = head[x]; i; i = E[i].nxt)
{
int v = E[i].to;
if (v == fa[x])
continue;
fa[v] = x;
dep[v] = dep[x] + 1;
DFS(v);
siz[x] += siz[v];
if (siz[v] > siz[son[x]])
son[x] = v;
}
if (!son[x])
son[x] = x;
}
il int LCA(int x, int y)
{
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
return x;
}

int main()
{
n=read(), m=read(), k=read();
for (ri i = 1; i <= m; i++)
e[i].fr = read(), e[i].to = read(), e[i].weight = read();
sort(e + 1, e + 1 + m, cmp);
for (ri i = 1; i <= n; i++)
fa[i] = i, fa[i + n] = i + n;
Ex_Kruskal();
int rt = findf(1);
memset(fa, 0, sizeof(fa));
DFS(rt);
for (ri i = 1; i <= dfn[0]; i++)
top[dfn[i]] = (dfn[i] == son[fa[dfn[i]]]) ? top[fa[dfn[i]]] : dfn[i];
for (ri i = 1; i <= k; i++)
{
int x = read(), y = read();
printf("%d\n", w[LCA(x, y)]);
}
return 0;
}

以上代码为BZOJ 3732代码

0%