Blog of RuSun

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

CF1131D Gourmet choice

LuoGu: CF1131D Gourmet choice

CF: D. Gourmet choice

裸的差分约束,复杂度 $O(n ^ 3)$ 。

查看代码
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
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
const int N = 2e3 + 10;
bool vis[N];
int n, m, d[N], cnt[N];
struct Edge
{
int v, w;
};
vector <Edge> g[N];
bool spfa ()
{
queue <int> q;
for (int i = 1; i <= n + m; i++)
d[i] = 1, q.push(i), vis[i] = true;
while (!q.empty())
{
int t = q.front();
q.pop();
vis[t] = false;
for (Edge i : g[t])
if (d[t] + i.w > d[i.v])
{
d[i.v] = d[t] + i.w;
if (++cnt[i.v] > n + m)
return false;
if (!vis[i.v])
q.push(i.v), vis[i.v] = true;
}
}
return true;
}
int main ()
{
read(n), read(m);
for (int i = 1; i <= n; i++)
for (int j = n + 1; j <= n + m; j++)
{
char c = getchar();
while (c != '=' && c != '<' && c != '>')
c = getchar();
if (c == '=')
{
g[i].push_back((Edge){j, 0});
g[j].push_back((Edge){i, 0});
}
else if (c == '<')
g[i].push_back((Edge){j, 1});
else
g[j].push_back((Edge){i, 1});
}
if (!spfa())
return puts("No"), 0;
puts("Yes");
for (int i = 1; i <= n; i++)
write(d[i]), putchar(' ');
puts("");
for (int i = n + 1; i <= n + m; i++)
write(d[i]), putchar(' ');
return 0;
}

$O(n ^ 2)$ 的做法,先用并查集合并相等的,如果一个集合中出现不相等的,无解;拓扑排序,如果形成一个环,无解。最后DP即可。