QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#490959#8759. 小班课Yansuan_HClCompile Error//C++145.2kb2024-07-25 16:51:262024-07-25 16:51:26

Judging History

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

  • [2024-07-25 16:51:26]
  • 评测
  • [2024-07-25 16:51:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define U(i,l,r) for (int i(l),END##i(r); i<=END##i; ++i)
#define D(i,l,r) for (int i(l),END##i(r); i>=END##i; --i)
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline))
#define vc vector
#define ar array
#define pb push_back
#define eb emplace_back
#define el '\n'
using ll = long long;

const int N = 505;
int n, m, b[N];


namespace dinic {
	const int N = 1005, M = 800005, INF = 0x3f3f3f3f;
  struct edge { int v, f, pre; } e[M]; int tail[N], ptr = 1;
  void single(int u, int v, int f) {
    e[++ptr] = {v, f, tail[u]};
    tail[u] = ptr;
  }
  void add(int u, int v, int f) {
    single(u, v, f);
    single(v, u, 0);
  }
  int layer[N], que[N];
  bool bfs(int s, int t) {
    ms(layer, 0); int l = 0, r = 1; que[0] = s; layer[s] = 1;
    while (l < r) {
      int u = que[l++];
      for (int p = tail[u]; p; p = e[p].pre) {
        auto [v, f, _] = e[p];
        if (!layer[v] && f)
          layer[que[r++] = v] = layer[u] + 1;
      }
    }
    return layer[t];
  }
  int dfs(int u, int in, int t) {
    if (u == t || !in) return in;
    int out = 0;
    for (int &p = tail[u]; p; p = e[p].pre) {
      auto &[v, f, _] = e[p];
      if (!f || layer[v] != layer[u] + 1) continue;
      
      int c = dfs(v, min(f, in), t);
      f -= c; e[p ^ 1].f += c;
      in -= c; out += c;
      if (!in) break;
    }
    return out;
  }
  int flow(int s, int t) {
    int orig[N], res = 0;
    while (bfs(s, t)) {
      memcpy(orig, tail, sizeof(tail));
      int cur = dfs(s, INF, t);
      res += cur;
      // clog << cur << endl;
      memcpy(tail, orig, sizeof(tail));
    }
    return res;
  }

	void clear() { ms(tail, 0); ptr = 1; }
}
#define de dinic::

void solve() {
	#define Assert(x) if (!(x)) { cout << "FAIL@" << __LINE__ << endl; print(); exit(0); }

	int n, m; cin >> n >> m;
	int S = de N - 1, T = S - 1;
	int sum[N] {};
	U (i, 1, m) {
		cin >> b[i]; sum[i] = sum[i - 1] + b[i];
		de add(n + i, T, b[i]);
	}

	vc<int> a[N], eg[N]; int beg[N] {};
	U (i, 1, n) {
		de add(S, i, 1);

		int k; cin >> k;
		a[i].resize(k); eg[i].resize(k); int j = 0;
		for (int &u : a[i]) {
			cin >> u;
			de add(i, n + u, 1);
			eg[i][j++] = de ptr;
		}
	}

	int cnt[N] {}, mat[N] {}, ans[N] {}; memcpy(cnt, b, sizeof(b));
	int fl = de flow(S, T);
	int trash = n;
	U (i, 1, n) {
		mat[i] = -1;
		U (j, 0, int(a[i].size()) - 1) if (de e[eg[i][j]].f) {
			mat[i] = j;
			break;
		}
		if (mat[i] == -1)
			ans[i] = trash--;
	}

	// clog << fl << "*";
	// U (i, 1, n)
	// 	clog << a[i][mat[i]] << ' ';
	// clog << endl;

	auto shrink = [&ans, &n, &cnt, &a, &beg, &mat]() {
		U (i, 1, n) if (!ans[i]) {
			while (!cnt[a[i][beg[i]]]) {
				++beg[i];
				Assert(beg[i] <= mat[i]);
				if (beg[i] == a[i].size())
					Assert(0);
			}
		}
	};
	auto done = [&ans, &cnt, &beg, &mat, &a, &n, &shrink](int j, int t) {
		// clog << j << '@' << a[j][mat[j]] << endl;
		// clog << "ans_" << j << "=" << t << endl;
		ans[j] = t;
		--cnt[a[j][mat[j]]];
		shrink();
	};

	auto print = [&]() {
		cout << "**" << endl;
		cout << n << ' ' << m << endl;
		U (i, 1, m) cout << b[i] << ' '; cout << endl;
		U (i, 1, n) {
			cout << a[i].size();
			for (int u : a[i])
				cout << ' ' << u;
			cout << endl;
		}
	}
	shrink();
	
	U (_, 1, trash) { // 依次确定前 i 个
		int f = 0;
		U (j, 1, n) if (!ans[j] && mat[j] == beg[j]) {
			f = j;
			break;
		}
		if (f) {
			done(f, _);
			continue;
		}
		int pre[N] {}; memcpy(pre, b, sizeof(b));
		U (j, 1, n) if (mat[j] != -1)
			--pre[a[j][mat[j]]];
		U (j, 1, n) if (!ans[j] && pre[a[j][beg[j]]]) {
			mat[j] = beg[j];
			f = j; break;
		}
		if (f) {
			done(f, _);
			break;
		}

		int label[N][2] {}, sc[N] {}, s2[N] {}, mp[N] {}, tot, lim[N] {};
		U (i, 1, n) if (!ans[i]) {
			if (beg[i] > mat[i])
				Assert(0);
			++sc[a[i][beg[i]]];
			++s2[a[i][mat[i]]];
		}
		U (i, 1, m) {
			Assert(s2[i] == sc[i]);
			sc[i] += sc[i - 1];
			lim[i] = sc[i - 1];
			U (j, sc[i - 1] + 1, sc[i])
				mp[j] = i;
		}
		tot = sc[m];
		memcpy(s2, sc, sizeof(sc));
		int to[N] {};
		U (i, 1, n) if (!ans[i]) {
			label[i][0] = sc[a[i][beg[i]]]--;
			label[i][1] = s2[a[i][mat[i]]]--;
			Assert(sc[a[i][beg[i]]] >= lim[a[i][beg[i]]]);
			Assert(s2[a[i][mat[i]]] >= lim[a[i][mat[i]]]);
			to[label[i][1]] = label[i][0];
		}
		int stk[N] {}, sp = 0, vis[N] {}, tag[N] {};
		for (int u = 1; ; u = to[u]) {
			if (vis[u]) {
				do {
					tag[stk[sp]] = 1;
				} while (stk[sp--] != u);
				break;
			} else {
				vis[u] = 1;
				stk[++sp] = u;
			}
		}

		U (i, 1, n) if (!ans[i]) {
			if (tag[i]) {
				mat[i] = beg[i];
				Assert(mp[label[i][0]] == a[i][beg[i]]);
			} else {
				Assert(mp[label[i][1]] == a[i][mat[i]]);
			}
		}

		--_;
	}

	cout << fl << endl;
	int inv[N] {};
	U (i, 1, n) 
		inv[ans[i]] = i;
	U (i, 1, n) {
		cout << inv[i] << ' ';
		Assert(inv[i]);
	}
	cout << endl;
}

void clear() {
	de clear();
	ms(b, 0);
}

int main() {
	// freopen("ava.in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);

	int t; cin >> t;
	while (t-- && !cin.eof()) {
		solve();
		clear();
	}
}

Details

answer.code: In function ‘bool dinic::bfs(int, int)’:
answer.code:35:14: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   35 |         auto [v, f, _] = e[p];
      |              ^
answer.code: In function ‘int dinic::dfs(int, int, int)’:
answer.code:46:13: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   46 |       auto &[v, f, _] = e[p];
      |             ^
answer.code: In lambda function:
answer.code:73:76: error: ‘print’ was not declared in this scope; did you mean ‘rint’?
   73 |         #define Assert(x) if (!(x)) { cout << "FAIL@" << __LINE__ << endl; print(); exit(0); }
      |                                                                            ^~~~~
answer.code:118:33: note: in expansion of macro ‘Assert’
  118 |                                 Assert(beg[i] <= mat[i]);
      |                                 ^~~~~~
answer.code:73:76: error: ‘print’ was not declared in this scope; did you mean ‘rint’?
   73 |         #define Assert(x) if (!(x)) { cout << "FAIL@" << __LINE__ << endl; print(); exit(0); }
      |                                                                            ^~~~~
answer.code:120:41: note: in expansion of macro ‘Assert’
  120 |                                         Assert(0);
      |                                         ^~~~~~
answer.code: In function ‘void solve()’:
answer.code:143:9: error: expected ‘,’ or ‘;’ before ‘shrink’
  143 |         shrink();
      |         ^~~~~~