P12913 [POI 2020/2021 R2] 小矮人摄影 / Zdjęcia krasnali
假设原图不存在孤点。
权值为
权值为
我们钦定
假设解存在。那么可以在图上,按照
这启发我们用一个类似拓扑排序的过程来求出一组
如果这样的遍历不能完全进行,即除了
若原图存在孤点,它们的权值也可以为
时间复杂度
#include <bits/stdc++.h>
#define F(i, a, b) for(int i = (a); i <= (b); ++i)
#define dF(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, LL> pii;
const int N = 5e5 + 5;
int n, m, deg[N], h[N]; vector<int> e[N];
int main() {
// freopen("zyq.in", "r", stdin);
// freopen("zyq.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
F(i, 1, m) {
int u, v; cin >> u >> v;
e[u].push_back(v), e[v].push_back(u);
++deg[u], ++deg[v];
}
F(i, 3, n) deg[i] >>= 1;
h[1] = 1, h[2] = n;
queue<int> q; int now = 1;
F(i, 3, n) if (!deg[i]) h[i] = ++now;
for (auto v : e[1]) --deg[v];
F(i, 3, n) if (!h[i] && !deg[i]) q.push(i);
while (!q.empty()) {
int u = q.front(); q.pop(), h[u] = ++now;
if (deg[u]) return cout << "NIE\n", 0;
for (auto v : e[u]) if (!h[v] && !(--deg[v])) q.push(v);
}
if (now < n - 1) return cout << "NIE\n", 0;
cout << "TAK\n";
F(i, 1, n) cout << h[i] << " ";
return 0;
}