QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432572#8686. Zoo ManagementMine_KingWA 2ms14016kbC++141.9kb2024-06-07 12:38:462024-06-07 12:38:46

Judging History

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

  • [2024-06-07 12:38:46]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:14016kb
  • [2024-06-07 12:38:46]
  • 提交

answer

// Think twice, code once.
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eputchar(c) putc(c, stderr)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define eputs(str) fputs(str, stderr), putc('\n', stderr)
using namespace std;

const int base = 23333, mod = 991958579;

int n, m, a[400005], b[400005];
int vis[400005], cnt, p[400005], q[800005];
struct graph {
	int tot, hd[400005];
	int nxt[800005], to[800005];

	void add(int u, int v) {
		nxt[++tot] = hd[u];
		hd[u] = tot;
		to[tot] = v;
		return ;
	}
} g;
int hsh[800005], pw;

void dfs(int now) {
	vis[now] = 1;
	cnt++;
	p[cnt] = a[now], q[cnt] = b[now];
	for (int i = g.hd[now]; i; i = g.nxt[i])
		if (!vis[g.to[i]]) dfs(g.to[i]);
	return ;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
	if (n == 1) {puts(a[1] == b[1] ? "possible" : "impossible"); return 0;}
	if (n == 2) {puts(a[1] == b[1] && a[2] == b[2] ? "possible" : "impossible"); return 0;}
	for (int i = 1; i <= m; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		g.add(u, v), g.add(v, u);
	}
	dfs(1);
	for (int i = 1; i <= n; i++) q[n + i] = q[i];
	int H = 0;
	for (int i = 1; i <= n; i++)
		H = ((long long)H * base + p[i]) % mod;
	pw = 1;
	for (int i = 1; i <= n; i++) pw = (long long)pw * base % mod;
	for (int i = 1; i <= 2 * n; i++) hsh[i] = ((long long)hsh[i - 1] * base + q[i]) % mod;
	for (int i = 1; i <= n; i++)
		if ((hsh[i + n - 1] + mod - (long long)hsh[i - 1] * pw % mod) % mod == H) {
			puts("possible");
			return 0;
		}
	reverse(q + 1, q + 2 * n + 1);
	for (int i = 1; i <= 2 * n; i++) hsh[i] = ((long long)hsh[i - 1] * base + q[i]) % mod;
	for (int i = 1; i <= n; i++)
		if ((hsh[i + n - 1] + mod - (long long)hsh[i - 1] * pw % mod) % mod == H) {
			puts("possible");
			return 0;
		}
	puts("impossible");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 14016kb

input:

6 7
1 1
2 2
3 3
1 2
2 3
3 1
1 2
2 3
1 3
3 4
4 5
5 6
4 6

output:

impossible

result:

wrong answer 1st lines differ - expected: 'possible', found: 'impossible'