Blog of RuSun

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

CF1239D Catowice City

LuoGu: CF1239D Catowice City

CF: D. Catowice City

给定一个左右均有 $n$ 个点的二分图,保证左部第 $i$ 个点和右部第 $i$ 个点一定有边。左部右部共找出 $n$ 个点(都至少一个),使得选择的点之间没有边。构造方案。

考虑一个一般图,在原图中的一条边 $(u, v) (u \in Left, v \in Right, u \neq v)$ ,存在一条 $(u, v)$ 表示 $u$ 选择了左部,那么 $v$ 不能选择右部,一旦出现了强连通分量,那么整个强连通分量中一定只能选择一样的。注意到一个强连通分量如果选择左部可能还会作为入边对其他强连通分量产生影响,所以我们选择没有出度的强连通分量作为左部,其他的作为右部即可。

tarjan的一个性质:第一个强连通分量一定是没有出度的,所以可以很方便地实现。

查看代码
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 <cstdio>
#include <vector>
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);
}
template <class Type>
void chkmin (Type &x, Type k)
{
k < x && (x = k);
}
const int N = 1e6 + 10;
bool vis[N];
int n, m, cnt;
int top, stk[N];
int stmp, dfn[N], low[N];
vector <int> g[N], scc[N];
void tarjan (int x)
{
dfn[x] = low[x] = ++stmp;
vis[stk[++top] = x] = true;
for (int i : g[x])
if (!dfn[i])
{
tarjan(i);
chkmin(low[x], low[i]);
}
else if (vis[i])
chkmin(low[x], dfn[i]);
if (dfn[x] ^ low[x])
return;
int y;
cnt++;
do
{
vis[y = stk[top--]] = false;
scc[cnt].push_back(y);
} while (x ^ y);
}
int main ()
{
int T;
read(T);
while (T--)
{
read(n), read(m);
while (top)
vis[stk[top--]] = false;
while (cnt)
scc[cnt--].clear();
stmp = 0;
for (int i = 1; i <= n; i++)
{
dfn[i] = 0;
g[i].clear();
}
for (int a, b; m; m--)
{
read(a), read(b);
g[a].push_back(b);
}
for (int i = 1; i <= n; i++)
!dfn[i] && (tarjan(i), 0);
if (cnt == 1)
{
puts("No");
continue;
}
puts("Yes");
write(scc[1].size()), putchar(' '), write(n - scc[1].size()), puts("");
for (int i : scc[1])
write(i), putchar(' ');
puts("");
for (int i = 2; i <= cnt; i++)
for (int j : scc[i])
write(j), putchar(' ');
puts("");
}
return 0;
}