QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#604464#8759. 小班课UESTC_OldEastWest#TL 95ms4196kbC++174.2kb2024-10-02 11:05:082024-10-02 11:05:09

Judging History

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

  • [2024-10-02 11:05:09]
  • 评测
  • 测评结果:TL
  • 用时:95ms
  • 内存:4196kb
  • [2024-10-02 11:05:08]
  • 提交

answer

#include <bits/stdc++.h>

namespace MCMF {
	#define INF INT_MAX

	int n, s, t, tot;
	std::vector<int> to, nxt, cap, cost, head;
	std::vector<int> dis;

	void AddEdge(int u, int v, int c, int w) {
		to[tot] = v, nxt[tot] = head[u], cap[tot] = c, cost[tot] = w, head[u] = tot++;
	}

	void Init(int _n, int _s, int _t, std::vector<std::array<int, 4> > edge) {
		n = _n, s = _s, t = _t, tot = 0;
		int m = edge.size();
		to = nxt = cap = cost = std::vector<int> (m << 1);
		head = dis = std::vector<int> (n, -1);
		for (auto [u, v, c, w] : edge) {
			AddEdge(u, v, c, w);
			AddEdge(v, u, 0, -w);
		}
	}

	std::pair<int, int> SPFA() {
		dis = std::vector<int> (n, INF);
		std::vector<int> vis(n), que;
		std::vector<std::pair<int, int> > pre(n, std::make_pair(-1, -1));
		que.emplace_back(s), dis[s] = 0, vis[s] = 1;
		for (int j = 0, u; j < que.size(); ++j) {
			u = que[j], vis[u] = 0;
			for (int i = head[u], v, c, w; ~i; i = nxt[i]) {
				v = to[i], c = cap[i], w = cost[i];
				int new_val = dis[u] + w;
				if (c && dis[v] > new_val) {
					dis[v] = new_val;
					pre[v] = std::make_pair(u, i);
					if (!vis[v]) que.emplace_back(v), vis[v] = 1;
				}
			}
		}
		if (dis[t] >= INT_MAX) return std::make_pair(0, 0);
		int min_cost = 0, max_flow = INF;
		int now = t;
		std::vector<int> all_e_idx;
		while (now != s) {
			auto [u, e_idx] = pre[now];
			now = u;
			max_flow = std::min(max_flow, cap[e_idx]);
			all_e_idx.emplace_back(e_idx);
		}
		for (int e_idx : all_e_idx) {
			min_cost += max_flow * cost[e_idx];
			cap[e_idx] -= max_flow, cap[e_idx ^ 1] += max_flow;
		}

		return std::make_pair(min_cost, max_flow);
	}

	std::pair<int, int> MCMF() {
		int min_cost = 0, max_flow = 0;
		while (true) {
			auto [new_cost, new_flow] = SPFA();
			if (!new_flow) break;
			else min_cost += new_cost, max_flow += new_flow;
		}
		return std::make_pair(min_cost, max_flow);
	}
}

void charming() {
  int n, m; std::cin >> n >> m;
  std::vector<int> b(m);
  for (int i = 0; i < m; ++i) std::cin >> b[i];

  int s = 0, t = 1, tot = 2;
  std::map<int, int> mp0, mp1, rev_mp1;
  std::vector<std::array<int, 4> > edge;
  std::vector<std::vector<int> > a(n);
  for (int i = 0; i < n; ++i) {
    mp0[i] = tot++;
    edge.emplace_back((std::array<int, 4>) {s, mp0[i], 1, 0});
  }
  for (int i = 0; i < m; ++i) {
    mp1[i] = tot;
    rev_mp1[tot++] = i;
    edge.emplace_back((std::array<int, 4>) {mp1[i], t, b[i], 0});
  }
  for (int i = 0, k; i < n; ++i) {
    std::cin >> k;
    a[i] = std::vector<int> (k);
    for (int j = 0; j < k; ++j) {
      std::cin >> a[i][j], --a[i][j];
      edge.emplace_back((std::array<int, 4>) {mp0[i], mp1[a[i][j]], 1, j});
    }
  }
  
  MCMF::Init(tot, s, t, edge);
  auto [_, ans] = MCMF::MCMF();

  std::vector<int> seq;
  {
    using namespace MCMF;

    std::vector<int> match(n, -1);
    for (int i = 0, u; i < n; ++i) {
      u = mp0[i];
      for (int j = head[u], v, c, w; ~j; j = nxt[j]) if (j & 1 ^ 1) {
        v = to[j], c = cap[j], w = cost[j];
        if (!c) {
          match[i] = w;
          break;
        }
      }
    }

    std::vector<int> vis(n);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) if (match[j] > -1 && !vis[j]) {
        bool ok = true;
        for (int k = 0; k < match[j]; ++k) {
          if (b[a[j][k]]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          vis[j] = 1;
          seq.emplace_back(j + 1);
          --b[a[j][match[j]]];
          break;
        }
      }
    }
    for (int i = 0; i < n; ++i) if (match[i] == -1) {
      seq.emplace_back(i + 1);
    }
  }
  
  // std::vector<int> vec(n);
  // std::iota(vec.begin(), vec.end(), 0);
  // sort(vec.begin(), vec.end(), [&](int x, int y) {
  //   if (match[x] == -1) return false;
  //   else if (match[y] == -1) return false;
  //   else {
  //     return match[x] < match[y];
  //   }
  // });

  std::cout << ans << '\n';
  for (int i = 0; i < n; ++i) std::cout << seq[i] << " \n"[i == n - 1];
}

signed main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  int t; std::cin >> t;
  while (t--) charming();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3820kb

input:

3
5 5
1 1 1 1 1
4 1 3 2 4
1 5
4 3 4 2 1
2 3 5
1 1
5 3
1 2 2
2 1 2
2 1 2
2 1 3
2 1 3
2 1 3
5 5
1 1 1 1 1
2 1 2
2 5 4
2 3 2
2 4 3
2 5 1

output:

5
2 4 3 5 1
5
5 1 2 3 4
5
2 3 4 5 1

result:

ok Correct!

Test #2:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

250
2 1
2
1 1
1 1
1 1
1
0
2 2
1 1
1 1
2 2 1
2 2
0 2
2 1 2
1 2
1 1
1
1 1
1 2
1 0
0
1 2
1 0
0
2 1
2
1 1
0
1 2
1 0
0
2 1
2
1 1
1 1
1 1
1
1 1
1 2
1 0
1 2
2 2
2 0
1 1
1 2
1 1
1
0
1 1
1
0
1 2
0 1
1 1
2 2
1 1
1 1
2 1 2
2 2
1 1
2 2 1
2 2 1
1 2
0 1
1 2
2 1
2
1 1
0
2 2
2 0
1 1
1 2
1 1
1
1 1
2 1
2
0
1 1
1 1
1
...

output:

2
1 2
0
1
2
1 2
2
1 2
1
1
0
1
0
1
1
1 2
0
1
2
1 2
1
1
0
1
1
1 2
0
1
0
1
0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3
1 2 4 3 5
3
1 3 2 4 5
2
1 3 2 4 5
3
1 4 5 2 3
1
1 2 3 4 5
3
2 3 5 1 4
1
4 1 2 3 5
2
3 4 1 2 5
2
1 4 2 3 5
2
3 4 1 2 5
3
2 4 5 1 3
...

result:

ok Correct!

Test #6:

score: 0
Accepted
time: 0ms
memory: 3680kb

input:

10
45 47
3 0 2 0 1 1 1 0 2 0 1 0 0 3 0 0 0 4 0 1 0 0 1 2 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 2 4 1 2 1 2 3
7 1 37 21 3 13 43 22
0
10 23 46 22 40 12 19 47 27 16 42
4 29 19 45 35
10 6 26 2 43 41 7 9 16 42 44
5 39 40 34 46 14
3 34 3 38
8 10 5 38 23 19 37 9 34
0
5 31 29 15 13 35
3 40 4 28
1 7
6 29 12 9 35 2...

output:

33
1 7 11 6 12 14 15 16 17 19 21 25 29 5 30 31 37 10 4 13 18 24 35 38 39 8 3 34 40 42 43 36 44 2 9 20 22 23 26 27 28 32 33 41 45
39
3 10 12 14 15 16 17 18 19 7 20 25 24 28 29 30 31 32 33 35 38 11 34 40 9 21 5 1 26 6 23 39 41 42 43 36 44 2 45 4 8 13 22 27 37
36
1 3 4 8 10 16 17 20 21 18 23 2 25 27 5 ...

result:

ok Correct!

Test #7:

score: 0
Accepted
time: 95ms
memory: 4196kb

input:

1
499 497
1 2 0 2 0 1 0 0 0 2 1 2 0 3 1 2 0 0 0 1 0 1 0 2 1 0 1 0 1 1 1 2 0 1 0 1 0 2 2 3 1 1 2 1 0 0 1 0 2 3 0 1 0 0 2 0 1 2 1 0 0 1 2 0 0 2 0 2 0 1 0 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 2 3 0 0 0 4 2 2 1 2 2 0 1 0 1 0 2 0 1 0 2 0 0 1 1 1 3 2 0 2 2 2 0 1 1 1 1 1 0 1 0 1 1 1 1 1 2 0 0 1 0 2 1 2 1 2 1 0 1 ...

output:

482
3 4 5 6 8 10 12 16 17 18 22 27 19 28 30 31 33 34 35 14 38 39 40 41 42 43 47 48 49 51 53 56 58 59 60 29 61 63 64 66 69 46 50 70 73 75 76 80 81 82 86 87 88 89 91 92 95 96 101 102 104 106 97 108 110 111 112 113 117 118 119 120 121 122 123 124 125 126 134 135 136 138 139 141 142 143 144 145 148 149 ...

result:

ok Correct!

Test #8:

score: -100
Time Limit Exceeded

input:

1
498 499
0 1 1 0 1 0 1 0 0 0 0 2 0 3 1 2 4 0 1 0 1 1 0 0 0 1 1 0 0 2 2 0 1 1 1 0 4 1 1 2 1 0 0 1 2 0 1 2 1 0 1 2 0 2 1 2 2 0 2 2 0 1 0 2 0 0 3 0 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 2 1 1 0 1 0 1 0 0 0 1 1 2 0 1 0 2 1 1 2 2 0 0 0 0 2 0 2 1 0 1 0 2 0 1 3 1 1 1 0 1 3 0 1 0 1 0 0 1 3 2 3 2 1 1 0 2 ...

output:


result: