QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#337202#4513. Slide ParadeGrand_Elf35 ✓2293ms34188kbC++173.4kb2024-02-25 08:05:092024-02-25 08:05:10

Judging History

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

  • [2024-02-25 08:05:10]
  • 评测
  • 测评结果:35
  • 用时:2293ms
  • 内存:34188kb
  • [2024-02-25 08:05:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 205;
const int M = 6005;
const int L = 1e6 + 5;
const int inf = 1e9;

int n, m, top, tot, tot2, s, t, h[N * 2], h2[N], lv[N * 2], cur[N * 2], pre[N * 2], cnt[M * 2], ans[L];
bool vis[N];
queue<int> q;

struct Edge {
	int v, w, nxt;
} e[M * 2], e2[L];

void add(int u, int v, int w) {
	e[++tot].v = v;
	e[tot].w = w;
	e[tot].nxt = h[u];
	h[u] = tot;
}

void add2(int u, int v) {
	e2[++tot2].v = v;
	e2[tot2].nxt = h2[u];
	h2[u] = tot2;
}

bool bfs() {
	memset(lv, -1, sizeof(lv));
	lv[s] = 0;
	q.push(s);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = h[u]; i; i = e[i].nxt) {
			int v = e[i].v, w = e[i].w;
			if (lv[v] == -1 && w > 0) {
				lv[v] = lv[u] + 1;
				q.push(v);
			}
		}
	}
	return ~lv[t];
}

int dfs(int u, int mn) {
	if (u == t) {
		return mn;
	}
	int res = 0;
	for (int &i = cur[u]; i; i = e[i].nxt) {
		int v = e[i].v, w = e[i].w;
		if (lv[v] == lv[u] + 1 && w > 0) {
			int f = dfs(v, min(mn, w));
			e[i].w -= f;
			e[i ^ 1].w += f;
			mn -= f;
			res += f;
			if (!mn) {
				break;
			}
		}
	}
	if (!res) {
		lv[u] = -1;
	}
	return res;
}

int Dinic() {
	int res = 0;
	while (bfs()) {
		for (int i = 1; i <= t; i++) {
			cur[i] = h[i];
		}
		res += dfs(s, inf);
	}
 	return res;
}

void euler(int u) {
	for (int &i = h2[u]; i;) {
		int v = e2[i].v;
		i = e2[i].nxt;
		euler(v);
	}
	ans[++top] = u;
}

void work(int test_case) {
	cout << "Case #" << test_case << ": ";
	cin >> n >> m;
	tot = 1;
	tot2 = 0;
	top = 0;
	s = n * 2 + 1;
	t = n * 2 + 2;
	for (int i = 1; i <= t; i++) {
		h[i] = 0;
		h2[i] = 0;
	}
	for (int i = 2; i <= m * 2 + n * 4 + 1; i++) {
		cnt[i] = 0;
	}
	for (int u = 1; u <= n; u++) {
		add(s, u, 1);
		add(u, s, 0);
		add(u + n, t, 1);
		add(t, u + n, 0);
	}
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		add(u, v + n, 1);
		add(v + n, u, 0);
	}
	if (Dinic() < n) {
		cout << "IMPOSSIBLE\n";
		return;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = h[i]; j; j = e[j].nxt) {
			int v = e[j].v;
			if (v > n && v < s && !e[j].w) {
				cnt[j] += m - n + 1;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= t; j++) {
			pre[j] = 0;
		}
		pre[i + n] = -1;
		q.push(i + n);
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			for (int j = h[u]; j; j = e[j].nxt) {
				int v = e[j].v, w = e[j].w;
				if (!w || v >= s) {
					continue;
				}
				if (pre[v]) {
					if (v == i + n) {
						cnt[j]++;
						for (int x = u; x != i + n; x = e[pre[x] ^ 1].v) {
							if (x > n) {
								cnt[pre[x]]++;
							}
							else {
								cnt[pre[x] ^ 1]--;
							}
						}
					}
					continue;
				}
				pre[v] = j;
				q.push(v);
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = h[i]; j; j = e[j].nxt) {
			int v = e[j].v;
			if (v > n && v < s && !cnt[j]) {
				cout << "IMPOSSIBLE\n";
				return;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = h[i]; j; j = e[j].nxt) {
			while (cnt[j]--) {
				add2(i, e[j].v - n);
			}
		}
	}
	euler(1);
	for (int i = 1; i <= n; i++) {
		if (h2[i]) {
			cout << "IMPOSSIBLE\n";
			return;
		}
	}
	cout << top << '\n';
	while (top) {
		cout << ans[top--] << ' ';
	}
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T; cin >> T; for (int i = 1; i <= T; i++) work(i);

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 11
Accepted
time: 0ms
memory: 5904kb

input:

100
5 8
1 2
1 3
1 4
1 5
2 1
3 1
4 1
5 1
5 10
1 3
1 4
2 3
2 5
3 1
3 4
3 5
4 2
5 1
5 3
5 10
1 4
2 3
2 5
3 1
3 5
4 2
4 3
4 5
5 1
5 2
3 6
1 2
1 3
2 1
2 3
3 1
3 2
5 10
1 2
1 5
2 3
2 4
3 1
4 3
4 5
5 2
5 3
5 4
4 10
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 4
4 2
4 3
5 10
1 2
1 3
2 1
2 4
3 1
3 5
4 2
4 5
5 3
5 4
5 10
1 ...

output:

Case #1: IMPOSSIBLE
Case #2: 31
1 3 1 3 4 2 3 4 2 3 5 1 4 2 3 5 1 4 2 5 1 4 2 5 1 4 2 5 3 5 1 
Case #3: 31
1 4 2 3 1 4 2 3 1 4 2 3 1 4 3 5 1 4 3 5 1 4 5 2 3 5 2 5 2 5 1 
Case #4: 13
1 2 1 2 3 1 2 3 1 3 2 3 1 
Case #5: 31
1 2 3 1 2 4 3 1 2 4 3 1 5 2 4 3 1 5 2 4 5 2 4 5 3 1 5 4 5 3 1 
Case #6: 29
1 2 ...

result:

ok  (100 test cases)

Test #2:

score: 24
Accepted
time: 2293ms
memory: 34188kb

input:

100
199 4980
1 95
1 96
1 105
1 124
1 130
1 131
1 135
1 137
1 139
1 140
1 141
1 147
1 155
1 172
1 174
1 179
1 183
1 188
1 193
1 196
1 197
2 3
2 5
2 13
2 14
2 17
2 20
2 24
2 26
2 30
2 41
2 44
2 45
2 52
2 56
2 67
2 70
2 71
2 74
2 78
2 84
2 85
2 90
2 92
2 93
2 97
2 107
2 111
2 113
2 122
2 124
2 128
2 13...

output:

Case #1: IMPOSSIBLE
Case #2: IMPOSSIBLE
Case #3: 960201
1 18 13 9 5 3 7 4 17 7 4 17 11 1 18 25 1 18 29 1 18 29 12 5 3 7 5 3 7 6 1 18 29 12 5 21 17 11 4 17 11 4 17 11 4 17 11 4 34 2 5 21 19 10 7 6 1 18 29 12 15 2 5 21 26 18 29 25 1 27 15 2 5 27 15 3 7 6 2 25 23 4 34 22 4 34 22 10 13 10 14 4 34 22 15 ...

result:

ok  (100 test cases)