跳到正文

题解

题解:CF2245D2 Construct an Array (Hard Version)

Leicx约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月19日
收录
2026年8月11日
更新
2026年10月27日
篇幅
约 6 分钟
获赞
1
题目
CF2245D2

版本

  1. v1当前

不会 D1 的可以看这里。

解题思路

延续 D1 的思路,考虑将限制转化为形如 i→ji \to j 的边,相对于 D1,我们不知道每个数的正负,所以限制不好利用。

观察一下每条限制,对于 ai+aj≥0a_i + a_j \ge 0,可以转化为条件 ai≥0a_i \ge 0 或 aj≥0a_j \ge 0,同理 ai+aj<0a_i + a_j < 0 可以转化为 ai<0a_i < 0 或 aj<0a_j < 0,我们需要通过这些条件先找到一种合理的正负分配方式再建图跑拓扑确定大小。

做过 2-SAT 模板的看到这个肯定瞬间懂了,这不就是 xix_i 为真或 xjx_j 为真,xix_i 为假或 xjx_j 为假两种限制条件吗。

因此根据限制跑 2-SAT 得出每个数字的正负后再像 D1 那样做赋值即可。

时空复杂度均为 O(n+m)O(n + m)。

赛时代码

C++
#include <bits/stdc++.h>
#define int long long
#define pf push_front
#define pb push_back
#define pii pair<int,int>
#define pdd pair<double,double>
#define mod 998244353
#define Mod 1000000007
#define inf 1e18
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define putYES return cout << "YES\n", void();
#define putNO return cout << "NO\n", void();
#define putYes return cout << "Yes\n", void();
#define putNo return cout << "No\n", void();
using namespace std;
const int N = 4e5 + 10;
int n, m, idx, cnt;
int dfn[N], low[N], scc[N], a[N], deg[N], ton[N];
bool vis[N];
vector<int> e[N], g[N];
stack<int> st;
void tarjan(int u) {
	dfn[u] = low[u] = ++idx, st.push(u), vis[u] = 1;
	for (auto v : e[u])
		if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
		else if (vis[v]) low[u] = min(low[u], dfn[v]);
	if (low[u] == dfn[u]) {
		scc[u] = ++cnt;
		while (st.top() != u)
			scc[st.top()] = cnt, vis[st.top()] = 0, st.pop();
		vis[u]=0, st.pop();
	}
}
struct node {
    int o, x, y;
};
void solve(){
    cin >> n >> m;
    for (int i = 1; i <= 2 * n; i++)
        e[i].clear(), g[i].clear(), deg[i] = dfn[i] = low[i] = scc[i] = vis[i] = 0;
    vector<node> qu;
    for (int i = 1; i <= m; i++) {
        int o, x, y; cin >> o >> x >> y;
        if (o == 1) e[x].pb(y + n), e[y].pb(x + n);
        else e[x + n].pb(y), e[y + n].pb(x);
        if (x != y) qu.pb({o, x, y});
    }
    idx = cnt = 0;
    for (int i = 1; i <= 2 * n; i++) if (!dfn[i]) tarjan(i);
    for (int i = 1; i <= n; i++)
        if (scc[i] == scc[i + n]) putNO;
    for (int i = 1; i <= n; i++)
        a[i] = (scc[i] > scc[i + n] ? 1 : -1);
    for (auto [o, x, y] : qu) {
        if (o == 1 && a[x] == -1 && a[y] == -1) putNO;
        if (o == 2 && a[x] == 1 && a[y] == 1) putNO;
        if (a[x] != a[y]) {
            if (o == 1) {
                if (a[x] > a[y]) g[x].pb(y), deg[y]++;
                else g[y].pb(x), deg[x]++;
            } else {
                if (a[x] > a[y]) g[y].pb(x), deg[x]++;
                else g[x].pb(y), deg[y]++;
            }
        }
    }
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (!deg[i]) q.push(i);
    int cnt = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        ton[u] = ++cnt;
        for (auto v : g[u])
            if (--deg[v] == 0) q.push(v);
    }
    for (int i = 1; i <= n; i++) if (deg[i]) putNO;
    cout << "YES\n";
    for (int i = 1; i <= n; i++)
        cout << (n - ton[i] + 2) * a[i] << ' ';
    cout << '\n';
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}
原文