Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

SP300 CABLETV - Cable TV Network

SPOJ: CABLETV - Cable TV Network

LuoGu: SP300 CABLETV - Cable TV Network

求使图不连通的最少割点。图不连通只要存在两点不连通即可,枚举两点,作为源点和汇点。割点,拆点即可。

查看代码
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
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <climits>
#define inf INT_MAX
using namespace std;
typedef pair <int, int> PII;
const int N = 110, M = 6e3 +10;
int n, m, st, ed, ans, d[N], cur[N];
int idx, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
for (int i = 0; i < n << 1; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue <int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed)
return true;
q.push(edg[i]);
}
}
return false;
}
int exploit(int x, int limit)
{
if (x == ed)
return limit;
int res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
int t = exploit(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = exploit(st, inf))
{
if (flow == inf)
return inf;
else
res += flow;
if (res >= ans)
return res;
}
return res;
}
void add(int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
void init()
{
ans = n;
idx = -1;
for (int i = 0; i < n << 1; i++)
hd[i] = -1;
}
PII div(string s)
{
int x = 0, y = 0;
bool flag = false;
s.erase(0, 1);
s.erase(s.size() - 1, 1);
for (int i = 0; i < s.size(); i++)
if (s[i] == ',')
flag = true;
else
{
if (!flag)
x = x * 10 + s[i] - '0';
else
y = y * 10 + s[i] - '0';
}
return make_pair(x, y);
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
init();
for (int i = 0; i < n; i++)
{
add(i << 1, i << 1 | 1, 1);
add(i << 1 | 1, i << 1, 0);
}
for (int i = 1, a, b; i <= m; i++)
{
string s;
cin >> s;
a = div(s).first;
b = div(s).second;
add(a << 1 | 1, b << 1, inf);
add(b << 1, a << 1 | 1, 0);
add(b << 1 | 1, a << 1, inf);
add(a << 1, b << 1 | 1, 0);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
{
st = i <<1 | 1;
ed = j << 1;
ans = min(ans, dinic());
for (int k = 0; k <= idx; k += 2)
{
wt[k] += wt[k ^ 1];
wt[k ^ 1] = 0;
}
}
cout << ans << endl;
}
return 0;
}