1 条题解

  • 0
    @ 2026-1-12 22:25:31

    #include <bits/stdc++.h>
    #define EB emplace_back
    #define True(x) (2 * (x) - 1)
    #define False(x) (2 * (x))
    #define ad(x) ((((x) - 1) ^ 1) + 1)
    using std::cin;
    using std::cout;
    
    typedef std::vector <int> vector;
    const int N = 1000054;
    
    int n;
    char s[N];
    int v1[N], v2[N], rp[N];
    vector all[N];
    
    inline void down(int &x, const int y) {x > y ? x = y : 0;}
    
    namespace Graph {
    	const int N = ::N * 4, M = ::N * 8;
    
    	struct edge {
    		int u, v;
    		edge (int u0 = 0, int v0 = 0) : u(u0), v(v0) {}
    	} e[M];
    
    	int V, E = 0, V1;
    	int first[N], next[N];
    	int cnt = 0, id[N], low[N], topo[N];
    	int stc = 0, stack[N], top[N];
    	bool in_stack[N];
    	int col[N], conflict[N];
    
    	inline void addedge(int u, int v) {e[++E] = edge(u, v), next[E] = first[u], first[u] = E;}
    	inline void implies(int u, int v) {addedge(u, v), addedge(ad(v), ad(u));}
    	inline void must(int x) {addedge(ad(x), x);}
    
    	void dfs(int x) {
    		int i, y; id[x] = low[x] = ++cnt, in_stack[x] = true, stack[stc++] = x;
    		for (i = first[x]; i; i = next[i])
    			if (!id[y = e[i].v])
    				dfs(y), down(low[x], low[y]);
    			else if (in_stack[y])
    				down(low[x], id[y]);
    		if (id[x] == low[x]) {
    			for (y = 0; y != x; y = stack[--stc], in_stack[y] = false, top[y] = x);
    			topo[V1++] = x;
    		}
    	}
    
    	inline bool _2_SAT() {
    		int i, u, v;
    		for (i = 1; i <= V; ++i) if (!id[i]) dfs(i);
    		for (i = 1; i <= V; i += 2) {
    			if ((u = top[i]) == (v = top[i + 1])) return false;
    			conflict[u] = v, conflict[v] = u;
    		}
    		for (i = 0; i < V1; ++i)
    			if (!col[v = topo[i]]) col[v] = 1, col[conflict[v]] = -1;
    		for (i = 1; i <= V; ++i) col[i] = col[top[i]];
    		return true;
    	}
    }
    
    namespace trie {
    	int V = 1, d[N][2], p[N], ch[N];
    
    	int append(const char *s) {
    		int t = 1, id;
    		for (; *s; ++s) id = *s & 1, t = (d[t][id] ? d[t][id] : (p[++V] = t, ch[V] = id, d[t][id] = V));
    		return t;
    	}
    
    	void travel(int t, char *s) {
    		char *q = s;
    		for (; t != 1; t = p[t]) *q++ = ch[t] | 48;
    		std::reverse(s, q), *q = 0;
    	}
    }
    
    int main() {
    	int i, j, l, z, &c = Graph::V;
    	std::ios::sync_with_stdio(false), cin.tie(NULL);
    	cin >> n;
    	for (i = 0; i < n; ++i) {
    		cin >> s, l = strlen(s), j = std::find(s, s + l, '?') - s;
    		if (j == l) v1[i] = trie::append(s), Graph::must(True(i + 1));
    		else s[j] = 48, v1[i] = trie::append(s), s[j] = 49, v2[i] = trie::append(s);
    		all[v1[i]].EB(True(i + 1));
    		if (v2[i]) all[v2[i]].EB(False(i + 1));
    	}
    	c = trie::V + n;
    	for (i = 2; i <= trie::V; ++i) {
    		rp[i] = trie::p[i] + n;
    		if (!(z = all[i].size())) continue;
    		for (j = 0; j < z; ++j)
    			Graph::implies(all[i][j], True(rp[i])),
    			Graph::implies(True(c + j + 1), ad(all[i][j])),
    			Graph::implies(True(c + j + 1), True(rp[i])),
    			rp[i] = c + j + 1;
    		c += z;
    	}
    	for (i = 2; i <= trie::V; ++i) Graph::implies(True(i + n), True(rp[i]));
    	c *= 2;
    	if (!Graph::_2_SAT()) return cout << "NO\n", 0;
    	cout << "YES\n";
    	for (i = 0; i < n; ++i) trie::travel(Graph::col[True(i + 1)] == 1 ? v1[i] : v2[i], s), cout << s << '\n';
    	return 0;
    }
    
    
    • 1

    信息

    ID
    6509
    时间
    2000ms
    内存
    1024MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者