洛谷__P3496 [POI 2010] GIL-Guilds(二分图)
题目链接:P3496 [POI 2010] GIL-Guilds - 洛谷
题目大意:
-
每个城镇对于裁缝行会(K) 和 缝纫行会(S) 分别要满足自己或邻居有该行会办事处。
-
任意城镇不能同时为 K 和 S。
如果无法满足条件输出 NIE ,否则输出 TAK 且接下来的 行应给出一种可行的办事处设置方案
-
字母
K—— 表示城镇 应设有裁缝行会(The Tailors Guild)的办事处,或 -
字母
S—— 表示城镇 应设有缝纫行会(The Sewers Guild)的办事处,或 -
字母
N—— 表示城镇 不应设有任何行会的办事处。
思路:
当存在孤立的城镇时,必定不满足上述条件,输出 NIE 即可
否则,显然整张图的节点只染黑白即可 (因为不涂色是不划算的)
那就不用考虑输出 字母N 了,变成了二分图染色问题
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
using namespace std;
typedef pair<int, int> pii;
const int N = 200086, M = 500086 * 2, mod = 998244353;
int n, m;
int h[N], ne[M], e[M], idx;
int d[N];
int col[N];
void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
void dfs(int u, int s) {
col[u] = s;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!col[j]) dfs(j, 3 - s);
}
}
void solve() {
mst(h, -1);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
d[a]++, d[b]++;
}
for (int i = 1; i <= n; i++) {
if (!col[i]) {
dfs(i, 1);
}
if (d[i] == 0) {
cout << "NIE" << endl;
return;
}
}
cout << "TAK" << endl;
for (int i = 1; i <= n; i++) {
if (col[i] == 1) cout << "K" << endl;
else if (col[i] == 2) cout << "S" << endl;
else cout << "N" << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号