「轉載】張 正錦 菌給的模板 I 圖論

第一輯 圖論

  • LCA
    • 倍增求LCA
    • 樹鏈剖分求LCA
  • 最短路
    • Dijkstra
    • SPFA (判負環)
    • SPFA (不判負環)
    • Floyd (多源最短路)
    • 次短路
  • 分層圖
  • 樹鏈剖分
  • 樹上差分
  • 差分約束
  • 二分圖匹配
    • Hungary算法 (社會主義分老婆)
  • 最小生成樹
    • Kruskal
  • 强連通分量
    • Tarjan縮點 + 染色

LCA

by Binary Lifting

C++
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<queue>
#define nqp main

using namespace std;

const int maxn = 500010;
int head[maxn],f[maxn][21],deep[maxn];
int n,m,root,cur;

struct hzw
{
int to,next;
}tree[maxn<<1];

void add(int u, int v)
{
tree[++cur].next = head[u];
tree[cur].to = v;
head[u] = cur;
}

void dfs(int u, int fa)
{
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for(int i=head[u]; i; i=tree[i].next)
{
int v = tree[i].to;
if(v == fa) continue;
dfs(v,u);
}
}

int LCA(int a, int b)
{
if(deep[a] > deep[b]) swap(a,b);
for(int i=20; i>=0; i--)
{
if(deep[a] <= deep[b] - (1<<i))
{
b = f[b][i];
}
}
if(a == b) return a;
for(int i=20; i>=0; i--)
{
if(f[a][i] == f[b][i]) continue;
a = f[a][i];
b = f[b][i];
}
return f[a][0];
}

int nqp()
{
ios :: sync_with_stdio(false);
cin >> n >> m >> root;
for(int i=1; i<=n; i++)
{
int u,v;
cin >> u >> v;
add(u,v);
add(v,u);
}
dfs(root,0);
for(int i=1; i<=20; i++)
{
for(int j=1; j<=n; j++)
{
f[j][i] = f[f[j][i-1]][i-1];
}
}
int ans = 0;
for(int i=1; i<=m; i++)
{
int a,b;
cin >> a >> b;
int ans = LCA(a,b);
cout << ans << endl;
}
return 0;
}

by HLD

C++
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cstdio>

using namespace std;

int read()
{
char c = ' ';
while(c<'0' || c>'9') c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v * 10 + c - '0';
c = getchar();
}
return v;
}

const int maxn = 100010;
int head[maxn],cur,n,m,s,son[maxn],id[maxn],size[maxn],f[maxn],top[maxn],deep[maxn];

struct hzw
{
int next,to;
}edge[maxn<<1];

void add(int u, int v)
{
edge[++cur].next = head[u];
edge[cur].to = v;
head[u] = cur;
}

void dfs1(int u, int fa, int dep)
{
size[u] = 1;
deep[u] = dep;
f[u] = fa;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
dfs1(v,u,dep+1);
size[u] += size[v];
if(son[u] == -1 || size[v] > size[son[u]]) son[u] = v;
}
}

void dfs2(int u, int t)
{
top[u] = t;
if(son[u]==-1) return;
dfs2(son[u],t);
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(v == f[u] || v == son[u]) continue;
dfs2(v,v);
}
}
int LCA(int x, int y)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x,y);
x = f[top[x]];
}
if(deep[x] > deep[y]) swap(x,y);
return x;
}

int main()
{
memset(son,-1,sizeof(son));
memset(head,-1,sizeof(head));
n = read(), m = read(), s = read();
for(int i=1; i<=n-1; i++)
{
int a,b;
a = read(), b = read();
add(a,b);
add(b,a);
}
dfs1(s,0,0);
dfs2(s,s);
for(int i=1; i<=m; i++)
{
int a,b;
a = read(), b = read();
cout << LCA(a,b) << endl;
}
return 0;
}

Shortest Path

Dijkstra

C++
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 100010;
int head[maxn],cur,n,m,vis[maxn],dis[maxn]; //dis[i]到i的最短路

struct hzw
{
int next,to,w;
}edge[maxn];

void add(int u, int v, int w) //边表建图
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}

struct heapnode //小根堆
{
int d,u;
bool operator < (const heapnode & h) const
{
return d > h.d;
}
};

void dijkstra(int s)
{
priority_queue <heapnode> q;
memset(dis,0x3f,sizeof(dis));
dis[s] = 0;
q.push((heapnode){dis[s],s});
while(!q.empty())
{
heapnode p = q.top();
q.pop();
int u = p.u;
if(vis[u]) continue; //如果已经访问过则跳过
vis[u] = 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
if(dis[edge[i].to] > dis[u] + edge[i].w)
dis[edge[i].to] = dis[u] + edge[i].w;
q.push((heapnode){dis[edge[i].to],edge[i].to}); //扩展下一个点
}
}
}

int main()
{
memset(head,-1,sizeof(head));
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
}
dijkstra(1);
cout << dis[n];
return 0;
}

SPFA (with judging negative ring)

C++
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<queue>
#define nqp main

using namespace std;

const int maxn = 500050;
int head[maxn],cur,n,m,s,dis[maxn],vis[maxn];

struct hzw
{
int to,next,w;
}edge[maxn];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}
int flag;int num[maxn];
void SPFA(int s)
{
for(int i=1; i<=n; i++) dis[i] = 2147483647;
queue <int> q;
dis[s] = 0;
q.push(s);
while(!q.empty())
{
int p;
p = q.front();
q.pop();
vis[p] = 0;
for(int i=head[p]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[p]+edge[i].w)
{
if(num[v]>n)
{
flag = 2333;
return ;
}
dis[v] = dis[p]+edge[i].w;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
num[v] = num[s]+1;
}
}
}
}
}
int nqp()
{
memset(head,-1,sizeof(head));
cin >> n >> m >> s;
for(int i=1; i<=m; i++)
{
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
}
SPFA(s);


for(int i=1; i<=n; i++)
{
cout << dis[i] << " ";
}
return 0;
}

SPFA (without judging negative ring)

C++
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>

using namespace std;

const int maxn = 500010;
int head[maxn],cur,n,m,dis[maxn],vis[maxn];

struct hzw
{
int next,to,w;
}edge[maxn];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}

void SPFA(int u)
{
queue <int> q;
memset(dis,0x3f,sizeof(dis));
dis[u] = 0;
q.push(u);
vis[u] = 1;
while(!q.empty())
{
int p = q.front();
q.pop();
vis[p] = 0;
for(int i=head[p]; i!=-1; i=edge[i].next)
{
if(dis[edge[i].to] > dis[p] + edge[i].w)
{
dis[edge[i].to] = dis[p] + edge[i].w;
if(!vis[edge[i].to])
{
q.push(edge[i].to);
vis[edge[i].to] = 1;
}
}
}
}
}

int s;
int main()
{
memset(head,-1,sizeof(head));
cin >> n >> m >> s;
for(int i=1; i<=m; i++)
{
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
}
SPFA(s);
for(int i=1; i<=n; i++)
{
cout << dis[i] << ' ';
}
return 0;
}

Floyd

C++
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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 1005;
int Map[maxn][maxn],n,m;

int main()
{
memset(Map,INF,sizeof(Map));
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int a,b,c;
cin >> a >> b >> c;
Map[a][b] = c;
}
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(k!=i && i!=j && k!=j)
Map[i][j] = min(Map[i][j],Map[i][k]+Map[k][j]);
cout << Map[1][n];
return 0;
}

Second Shortest Path

C++Luogu P2865
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
#include <iostream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

int read()
{
char c = ' ';
while(c<'0' || c>'9') c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v * 10 + c - '0';
c = getchar();
}
return v;
}

const int maxn = 100010;
int head[maxn],cur,vis[maxn],dis1[maxn],dis2[maxn],n,m;

struct hzw
{
int next,to,w;
}edge[maxn<<1];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}

struct heapnode
{
int d,u;
bool operator < (const heapnode & h) const
{
return d > h.d;
}
};


void dijkstra(int s, int dis[])
{
priority_queue <heapnode> q;
dis[s] = 0;
q.push((heapnode){dis[s],s});
int u;
while(!q.empty())
{
heapnode p = q.top();
q.pop();
u = p.u;
if(vis[u]) continue;
vis[u] = 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
if( dis[edge[i].to] > dis[u] + edge[i].w)
dis[edge[i].to] = dis[u] + edge[i].w;
q.push((heapnode){dis[edge[i].to],edge[i].to});
}
}
}

int main()
{
memset(dis1,0x3f,sizeof(dis1));
memset(dis2,0x3f,sizeof(dis2));
memset(head,-1,sizeof(head));
n = read(), m = read();
for(int i=1; i<=m; i++)
{
int a,b,c;
a = read(), b = read(), c = read();
add(a,b,c);
add(b,a,c);
}
dijkstra(1,dis1);
memset(vis,0,sizeof(vis));
dijkstra(n,dis2);
int ans = 0x3f3f3f3f;
for(int u=1; u<=n; u++)
{
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int cmp = dis1[u] + dis2[edge[i].to] + edge[i].w;
if(cmp > dis1[n] && cmp < ans) ans = cmp;
}
}
cout << ans;
return 0;
}

Layered Graph

C++Luogu P2939
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
#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;
int read()
{
char c = ' ';
while(c<'0' || c>'9') c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v * 10 + c - '0';
c = getchar();
}
return v;
}

const int maxn = 100100;
int head[maxn],cur,n,m,k,dis[maxn][21],vis[maxn][21];

struct hzw
{
int next,to,w;
}edge[maxn<<1];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}

struct heapnode
{
int d,u,h;
bool operator < (const heapnode & h) const
{
return d > h.d;
}
};

void dijkstra(int s)
{
priority_queue <heapnode> q;
memset(dis,0x3f,sizeof(dis));
dis[1][0] = 0;
q.push((heapnode){0,1,0});
while(!q.empty())
{
heapnode p = q.top();
q.pop();
int u = p.u;
int h = p.h;
if(vis[u][h]) continue;
vis[u][h] = 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
if(dis[edge[i].to][h] > dis[u][h] + edge[i].w)
{
dis[edge[i].to][h] = dis[u][h] + edge[i].w;
q.push((heapnode){dis[edge[i].to][h],edge[i].to,h});
}
if(h < k)
{
if(dis[edge[i].to][h+1] > dis[u][h])
{
dis[edge[i].to][h+1] = dis[u][h];
q.push((heapnode){dis[edge[i].to][h+1],edge[i].to,h+1});
}
}
}
}
}

int main()
{
memset(head,-1,sizeof(head));
n = read(), m = read(), k = read();
for(int i=1; i<=m; i++)
{
int a,b,c;
a = read(), b = read(), c = read();
add(a,b,c);
add(b,a,c);
}
dijkstra(1);
cout << dis[n][k];
return 0;
}

HLD (Heavy-Light Decomposition)

C++
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#define nqp main

using namespace std;

const int maxn = 300030;
int head[maxn],cur,n,m,r,p,top[maxn],son[maxn],tid[maxn],rnk[maxn],size[maxn],val[maxn];
int deep[maxn],f[maxn];

int read()
{
char c = ' ';
int f = 1;
while(c<'0' || c>'9')
{
if(c == '-') f = -1;
c = getchar();
}
int v = 0;
while(c>='0' && c<='9')
{
v = v*10 + c -'0';
c = getchar();
}
return v * f;
}

struct hzw
{
int next,to;
}edge[maxn];

void add(int u, int v)
{
edge[++cur].next = head[u];
edge[cur].to = v;
head[u] = cur;
}

struct ljm
{
int sum,tag,lc,rc;
}a[maxn];

void update(int rt)
{
a[rt].sum = a[a[rt].lc].sum + a[a[rt].rc].sum;
}

int tot = 1;
void build(int l, int r, int rt)
{
if(l == r)
{
a[rt].sum = rnk[l];
return;
}
int mid = (l+r) >> 1;
a[rt].lc = ++tot;
build(l,mid,a[rt].lc);
a[rt].rc = ++tot;
build(mid+1,r,a[rt].rc);
update(rt);
}

void pushdown(int l, int r, int rt)
{
// printf("%d %d\n",l,r);
int mid = (l+r) / 2;
a[a[rt].lc].sum += (mid-l+1) * a[rt].tag;
a[a[rt].lc].tag += a[rt].tag;
a[a[rt].rc].sum += (r-mid) * a[rt].tag;
a[a[rt].rc].tag += a[rt].tag;
a[rt].tag = 0;
}

int query(int l, int r, int rt, int nowl, int nowr)
{
int ans = 0;
if(l == nowl && r == nowr)
{
return a[rt].sum;
}
int mid = (l+r) >> 1;
pushdown(l,r,rt);
if(nowr <= mid) ans += query(l,mid,a[rt].lc,nowl,nowr);
else if(nowl > mid) ans += query(mid+1,r,a[rt].rc,nowl,nowr);
else
{
ans += query(l,mid,a[rt].lc,nowl,mid);
ans += query(mid+1,r,a[rt].rc,mid+1,nowr);
}
return ans;
}


void modify(int l, int r, int rt, int nowl, int nowr, int w)
{
if(l == nowl && r == nowr)
{
a[rt].sum += (r-l+1) * w;
a[rt].tag += w;
return;
}
int mid = (l+r) >> 1;
pushdown(l,r,rt);
if(nowr <= mid) modify(l,mid,a[rt].lc,nowl,nowr,w);
else if(nowl > mid) modify(mid+1,r,a[rt].rc,nowl,nowr,w);
else
{
modify(l,mid,a[rt].lc,nowl,mid,w);
modify(mid+1,r,a[rt].rc,mid+1,nowr,w);
}
update(rt);
}



//-----------------------------------------------------build tree

void dfs1(int u, int fa, int dep)
{
size[u] = 1;
deep[u] = dep;
f[u] = fa;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
dfs1(v,u,dep+1);
size[u] += size[v];
if(son[u] == -1 || size[v] > size[son[u]]) son[u] = v;
}
}

int cnt = 1;
void dfs2(int u, int t)
{
top[u] = t;
tid[u] = cnt;
rnk[cnt] = val[u];
cnt++;
if(son[u] == -1) return;
dfs2(son[u],t);
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(!tid[v]) dfs2(v,v);
}
}

int query_path(int x, int y)
{
int ans = 0;
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x,y);
ans += query(1,n,1,tid[top[x]],tid[x]);
x = f[top[x]];
}
if(deep[x] > deep[y]) swap(x,y);
ans += query(1,n,1,tid[x],tid[y]);
return ans;
}

void modify_path(int x, int y, int w)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x,y);
modify(1,n,1,tid[top[x]],tid[x],w);
x = f[top[x]];
}
if(deep[x] > deep[y]) swap(x,y);
modify(1,n,1,tid[x],tid[y],w);
}

int nqp()
{
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(size));
cin >> n >> m >> r >> p;
for(int i=1; i<=n; i++)
{
cin >> val[i];
}
for(int i=1; i<=n-1; i++)
{
int a,b;
cin >> a >> b;
add(a,b);
add(b,a);
}
dfs1(r,r,1);
dfs2(r,r);
build(1,n,1);
for(int i=1; i<=m; i++)
{
int x;
x = read();
if(x == 1)
{
int y,z,k;
y = read(), z = read(), k = read();
modify_path(y,z,k);
}
else if(x == 2)
{
int y,z;
y = read(), z = read();
int ans = query_path(y,z);
cout << ans << endl;
}
else if(x == 3)
{
int y,z;
y = read(), z = read();
modify(1,n,1,tid[y],tid[y]+size[y]-1,z);
}
else
{
int y;
y = read();
cout << query(1,n,1,tid[y],tid[y]+size[y]-1) << endl;
}
}
return 0;
}

Tree Diff

C++Luogu P3128
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>

using namespace std;

const int maxn = 500050;
int head[maxn],cur,f[maxn][21],deep[maxn],faa[maxn];
int d[maxn];
int maxx = -0x3f3f3f3f;

struct hzw
{
int next,to,w;
}edge[maxn<<1];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
head[u] = cur;
}

void dfs(int u, int fa)
{
faa[u] = fa;
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(fa == v) continue;
dfs(v,u);
}
}

int LCA(int a, int b)
{
if(deep[a] > deep[b]) swap(a,b);
for(int i=20; i>=0; i--)
{
if(deep[a] <= deep[b] - (1<<i))
{
b = f[b][i];
}
}
if(a == b) return a;
for(int i=20; i>=0; i--)
{
if(f[a][i] == f[b][i]) continue;
a = f[a][i];
b = f[b][i];
}
return f[a][0];
}

void dfs1(int a, int fa)
{
for(int i=head[a]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
dfs1(v,a);
d[a] += d[v];
}
maxx = max(d[a],maxx);
}

int n,m;

int read()
{
char c = ' ';
while(c<'0' || c>'9') c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v * 10 + c - '0';
c = getchar();
}
return v;
}

int main()
{
memset(head,-1,sizeof(head));
n = read(), m = read();
for(int i=1; i<=n-1; i++)
{
int a,b;
a = read(), b = read();
add(a,b,0);
add(b,a,0);
}
dfs(1,0);
for(int i=1; i<=20; i++)
{
for(int j=1; j<=n; j++)
{
f[j][i] = f[f[j][i-1]][i-1];
}
}
for(int i=1; i<=m; i++)
{
int a,b;
a = read(), b = read();
d[a]++;d[b]++;d[LCA(a,b)]--;d[f[LCA(a,b)][0]]--;
}
dfs1(1,0);
cout << maxx;
return 0;
}

Diff Constraints

我们可以用松弛来理解一下差分约束.

对于松弛来说 dis[v] > dis[u] + edge[i].w;
我们可以认为 v > u + c. 移项可得 v – u > w 所以我们就可以建一条从u->v的边,边权为c

例如:

a + b >= c 即 add(b,a,c);
a – b <= c 即 add(b,a,-c);
a – b == 0 即 add(a,b,0) add(b,a,0);

用差分约束建图即可

  • 注: 一定要用SPFA判负环!!!!!!

Bipartite Graph Match

Hungary Algorithm

社会主义分老婆

C++Luogu P3386
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>

using namespace std;

int read()
{
char c = ' ';
while(c<'0' || c>'9') c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v * 10 + c -'0';
c = getchar();
}
return v;
}

const int maxn = 1005;
int flag[maxn][maxn],vis[maxn],n,m,e,cx[maxn],cy[maxn];

int dfs(int x)
{
for(int y=1; y<=m; y++)
{
if(flag[x][y] && !vis[y])
{
vis[y] = 1;
if(!cy[y] || dfs(cy[y]))
{
cx[x] = y;
cy[y] = x;
return 1;
}
}
}
return 0;
}

int main()
{
n = read(), m = read(), e = read();
for(int i=1; i<=e; i++)
{
int x,y;
x = read(), y = read();
flag[x][y] = 1;
}
int ans = 0;
for(int i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));
ans += dfs(i);
}
cout << ans;
return 0;
}

MST (Minimum Spanning Tree)

Kruskal

C++
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#define nqp main

using namespace std;

int getint()
{
char c = ' ';
while(c<'0' || c>'9')
c = getchar();
int v = 0;
while(c>='0' && c<='9')
{
v = v*10 + c - '0';
c = getchar();
}
return v;
}

const int maxn = 600000;
int cur,head[maxn],n,m,vis[maxn],ans,f[maxn];

struct hzw
{
int to,next,w,u;
bool operator < (const hzw &h) const
{
return w < h.w;
}
}edge[maxn];

void add(int u, int v, int w)
{
edge[++cur].next = head[u];
edge[cur].to = v;
edge[cur].w = w;
edge[cur].u = u;
head[u] = cur;
}

int find(int x)
{
if(f[x]!=x)
f[x] = find(f[x]);
return f[x];
}

void un(int x, int y)
{
f[find(x)] = find(y);
}

bool query(int x, int y)
{
return find(x) == find(y);
}

int num;
void kru()
{
sort(edge+1,edge+m+1);
for(int i=1; i<=m; i++)
{
if(!query(edge[i].u,edge[i].to))
{
un(edge[i].to,edge[i].u);
num++;
ans += edge[i].w;
}
if(num == n-1) break;
}
}

int nqp()
{
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int a,b,c;
a = getint(),b = getint(),c = getint();
add(a,b,c);
}
for(int i=1; i<=m; i++)
{
f[i] = i;
}
kru();
cout << ans << endl;
return 0;
}

SCC (Strongly Connected Components)

Tarjan

C++Luogu P1726
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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

const int maxn = 100010;
int head[maxn],cur,m,vis[maxn],dfn[maxn],low[maxn],n,belong[maxn];

struct hzw
{
int to,next;
}edge[maxn];

void add(int u, int v)
{
edge[++cur].next = head[u];
edge[cur].to = v;
head[u] = cur;
}

int tot,col;
stack <int> s;

void tarjan(int u)
{
// cout<<"now "<<u<<endl;
dfn[u] = low[u] = ++tot;
s.push(u);
vis[u] = 1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
if(!dfn[edge[i].to])
{
tarjan(edge[i].to);
low[u] = min(low[u],low[edge[i].to]);
}
else if(vis[edge[i].to])
{
low[u] = min(low[u],dfn[edge[i].to]);
}
}
if(low[u] == dfn[u])
{
col++;
while(s.top() != u)
{
int front = s.top();
belong[front] = col;
s.pop();
vis[front] = 0;
}
int front = s.top();
belong[front] = col;
s.pop();
vis[front] = 0;
}
}

struct ljm
{
int val,color;
bool operator < (const ljm & j) const
{
return val > j.val;
}
}size[maxn];

int main()
{
memset(head,-1,sizeof(head));
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(z==1) add(x,y);
else add(x,y),add(y,x);
}
for(int i=1; i<=n; i++)
if(!dfn[i]) tarjan(i);
for(int i=1; i<=n; i++)
{
size[belong[i]].val++;//记录环的长度
size[belong[i]].color = belong[i];//记录环的颜色
}
sort(size+1,size+1+n);
cout << size[1].val << "\n";
for(int i=1; i<=n; i++)
{
if(belong[i] == size[1].color) cout << i << " ";
}
return 0;
}