QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#237377#7686. The Phantom Menaceucup-team027#WA 465ms3644kbC++234.7kb2023-11-04 14:01:022023-11-04 14:01:02

Judging History

你现在查看的是最新测评结果

  • [2024-10-08 14:11:03]
  • hack成功,自动添加数据
  • (/hack/941)
  • [2024-10-08 10:05:28]
  • hack成功,自动添加数据
  • (/hack/940)
  • [2024-10-07 19:51:15]
  • hack成功,自动添加数据
  • (/hack/938)
  • [2024-10-07 19:28:01]
  • hack成功,自动添加数据
  • (/hack/937)
  • [2024-10-07 17:16:32]
  • hack成功,自动添加数据
  • (/hack/936)
  • [2024-10-07 16:53:09]
  • hack成功,自动添加数据
  • (/hack/935)
  • [2024-10-07 16:22:17]
  • hack成功,自动添加数据
  • (/hack/934)
  • [2023-11-04 14:01:02]
  • 评测
  • 测评结果:WA
  • 用时:465ms
  • 内存:3644kb
  • [2023-11-04 14:01:02]
  • 提交

answer

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma,popcnt")
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

struct H {
	typedef uint64_t ull;
	ull x; H(ull x=0) : x(x) {}
#define OP(O,A,B) H operator O(H o) { ull r = x; asm \
	(A "addq %%rdx, %0\n adcq $0,%0" : "+a"(r) : B); return r; }
	OP(+,,"d"(o.x)) OP(*,"mul %1\n", "r"(o.x) : "rdx")
	H operator-(H o) { return *this + ~o.x; }
	ull get() const { return x + !~x; }
	bool operator==(H o) const { return get() == o.get(); }
	bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11+3; // (order ~ 3e9; random also ok)

struct HashInterval {
	vector<H> ha, pw;
	HashInterval(string& str) : ha(sz(str)+1), pw(ha) {
		pw[0] = 1;
		rep(i,0,sz(str))
			ha[i+1] = ha[i] * C + str[i],
			pw[i+1] = pw[i] * C;
	}
	H hashInterval(int a, int b) { // hash [a, b)
		return ha[b] - ha[a] * pw[b - a];
	}
};

H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;}

// end of hash

bool zeroCyc(vector<string> a, vector<string> b) {
	map<string, vector<int>> pa;
	for (int i = 0; i < a.size(); i++) {
		pa[a[i]].push_back(i);
	}

	vector<int> ans;
	for (int i = 0; i < b.size(); i++) {
		if (pa[b[i]].size()) {
			ans.push_back(pa[b[i]].back());
			pa[b[i]].pop_back();
		} else {
			return 0;
		}
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << i+1 << ' ';
	}
	cout << '\n';
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i]+1 << ' ';
	}
	cout << '\n';
	return 1;
}

// end of case

vi eulerWalk(vector<vector<pii>>& gr, int nedges, int src=0) {
	int n = sz(gr);
	vi D(n), its(n), eu(nedges), ret, s = {src};
	D[src]++; // to allow Euler paths, not just cycles
	while (!s.empty()) {
		int x = s.back(), y, e, &it = its[x], end = sz(gr[x]);
		if (it == end){ ret.push_back(x); s.pop_back(); continue; }
		tie(y, e) = gr[x][it++];
		if (!eu[e]) {
			D[x]--, D[y]++;
			eu[e] = 1; s.push_back(y);
		}}
	for (int x : D) if (x < 0 || sz(ret) != nedges+1) return {};
	return {ret.rbegin(), ret.rend()};
}

// end of euler walk

void solve() {
	int n, m; cin >> n >> m;
	vector<string> a(n), b(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) cin >> b[i];

	if (zeroCyc(a, b)) {
		return;
	}

	vector<HashInterval> hsa, hsb;
	for (int i = 0; i < n; i++) {
		hsa.push_back(HashInterval(a[i]));
		hsb.push_back(HashInterval(b[i]));
	}

	//cout << hsb[0].hashInterval(0, 2).x << '\n';
	//cout << hsa[0].hashInterval(1, 3).x << '\n';

	for (int i = 1, j = m-1; i < m; i++, j--) {\
		//cout << "Cutting " << i << '\n';
		// A-prefix: [0, i) --> forward edges
		// B-suffix: [j, m) --> backward edges

		int idx = 0;
		map<pair<H, int>, int> nds;
		vector<tuple<int, int, int>> edges;
		for (int k = 0; k < n; k++) {
			H l = hsa[k].hashInterval(0, i);
			H r = hsa[k].hashInterval(i, m);

			if (nds.count({l, 0}) == 0) {
				nds[{l, 0}] = idx;
				idx++;
			}
			if (nds.count({r, 1}) == 0) {
				nds[{r, 1}] = idx;
				idx++;
			}
			edges.emplace_back(nds[{l, 0}], nds[{r, 1}], k);
		}
		for (int k = 0; k < n; k++) {
			H l = hsb[k].hashInterval(j, m);
			H r = hsb[k].hashInterval(0, j);

			if (nds.count({l, 0}) == 0) {
				nds[{l, 0}] = idx;
				idx++;
			}
			if (nds.count({r, 1}) == 0) {
				nds[{r, 1}] = idx;
				idx++;
			}
			edges.emplace_back(nds[{r, 1}], nds[{l, 0}], k+n);
		}

		vector<vector<pii>> gr(idx);
		for (auto [u, v, id]: edges) {
			//cout << "EDGE " << u << ' ' << v << ' ' << id << '\n';
			gr[u].emplace_back(v, id);
		}
		//cout << '\n';
		vi res = eulerWalk(gr, edges.size(), 0);

		if (res.empty()) continue;
		else {
			//cout << "Found euler path\n";
			//for (int k: res) cout << k << ' ';
			//	cout << '\n';

			// found answer
			map<pair<int, int>, vector<int>> eds;
			for (auto [u, v, id]: edges) {
				eds[{u, v}].push_back(id);
			}

			vector<int> p1, p2;
			for (int k = 1; k < res.size(); k++) {
				int u = res[k-1], v = res[k];
				int edid = eds[{u, v}].back();
				eds[{u, v}].pop_back();

				if (edid < n) p1.push_back(edid);
				else p2.push_back(edid-n);
			}

			for (int k: p1) cout << k+1 << ' ';
				cout << '\n';
			for (int k: p2) cout << k+1 << ' ';
				cout << '\n';
			return;
		}
	}
	cout << "-1\n";
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);

	int t; cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

input:

2
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def

output:

1 3 2 
1 2 3 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 426ms
memory: 3596kb

input:

1000000
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
...

output:

1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
...

result:

ok 1000000 cases (1000000 test cases)

Test #3:

score: -100
Wrong Answer
time: 465ms
memory: 3592kb

input:

500000
1 2
dd
ba
1 2
cd
ba
1 2
bd
ba
1 2
ad
ba
1 2
dc
ba
1 2
cc
ba
1 2
bc
ba
1 2
ac
ba
1 2
db
ba
1 2
cb
ba
1 2
bb
ba
1 2
ab
ba
1 2
da
ba
1 2
ca
ba
1 2
ba
ba
1 2
aa
ba
1 2
dd
aa
1 2
cd
aa
1 2
bd
aa
1 2
ad
aa
1 2
dc
aa
1 2
cc
aa
1 2
bc
aa
1 2
ac
aa
1 2
db
aa
1 2
cb
aa
1 2
bb
aa
1 2
ab
aa
1 2
da
aa
1 2...

output:

-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
1 
1 
1 
1 
1 
1 
-1
-1
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
1 
1 
1 
1 
1 
1 
1 
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
1 
-1
-1
-1
-1
-1
1 
1 
1 
1 
1 
1 
1 
1 
-1
...

result:

wrong answer not cyclic isomorphism (test case 9)