QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#491186#8759. 小班课Rong7TL 2ms15840kbC++144.8kb2024-07-25 17:28:502024-07-25 17:28:50

Judging History

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

  • [2024-07-25 17:28:50]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:15840kb
  • [2024-07-25 17:28:50]
  • 提交

answer

// Not afraid to dark.

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

#define inline __inline__ __attribute__ ((always_inline))

namespace io {
	int pos;
	inline int read (int &p = pos){
		static int v; static char c;
		v = 1, c = getchar (), p = 0;
		while (! isdigit (c)){
			if (c == '-')
				v = - 1;
			c = getchar ();
		}
		while (isdigit (c)){
			p = (p << 1) + (p << 3) + (c - 48);
			c = getchar ();
		}
		return p *= v;
	}
	inline void write (int x){
		if (x < 0)
			putchar ('-'), x = - x;
		static int sta[65], top;
		top = 0;
		do {
			sta[++ top] = x % 10;
			x /= 10;
		} while (x);
		while (top)
			putchar (sta[top --] + 48);
	}
	inline char next_char (){
		static char c;
		c = getchar ();
		while (c == '\n' || c == ' ' || c == '\r')
			c = getchar ();
		return c;
	}
	inline void putss (const string s){
		for (int i = 0;i < (int) s.size ();++ i)
			putchar (s[i]);
	}
}

const int N = 500;
int n, m;
int restb, b[N + 5], a[N + 5][N + 5];

namespace F {
	const int cV = N * 2 + 5, cE = N * N * 6 + 5, inf = 0x3f3f3f3f;
	int s, t;
	int firs[cV], nex[cE], to[cE], w[cE], tot;
	int cur[cE], dep[cE], Ansflow;
	bool inque[cE];
	queue < int > q;
	inline void init (){
		tot = 1;
		for (int i = 1;i <= t;++ i) firs[i] = 0;
	}
	inline void Add (int u, int v, int k){
		nex[++ tot] = firs[u]; firs[u] = tot; to[tot] = v; w[tot] = k;
		nex[++ tot] = firs[v]; firs[v] = tot; to[tot] = u; w[tot] = 0;
	}
	inline bool Bfs (){
		for (int i = 1;i <= t;++ i)
			dep[i] = inf, inque[i] = false, cur[i] = firs[i];
		while (! q.empty ()) q.pop ();
		dep[s] = 0; inque[s] = true; q.push (s);
		while (! q.empty ()){
			int u = q.front ();
			q.pop ();
			inque[u] = false;
			for (int e = firs[u], v;e;e = nex[e]){
				v = to[e];
				if (dep[v] > dep[u] + 1 && w[e] != 0){
					dep[v] = dep[u] + 1;
					if (inque[v] == false){
						q.push (v);
						inque[v] = true;
					}
				}
			}
		}
		return dep[t] != inf;
	}
	int Dfs (int u, int flow){
		if (u == t) return flow;
		int used = 0, rlow;
		for (int &e = cur[u], v;e;e = nex[e]){
			v = to[e];
			if (w[e] != 0 && dep[v] == dep[u] + 1){
				rlow = Dfs (v, min (flow - used, w[e]));
				if (rlow != 0){
					used += rlow;
					w[e] -= rlow;
					w[e ^ 1] += rlow;
					if (used == flow)
						break;
				}
			}
		}
		return used;
	}
	inline int FLOW (){
		static int Ansflow; Ansflow = 0;
		while (Bfs ())
			Ansflow += Dfs (s, inf);
		return Ansflow;
	}
} using namespace F;

int rep[N + 5], te[N + 5][N + 5], fr[N + 5];
int us[N + 5], fi[N + 5];
bool vis[N + 5], ustag[N + 5];

int res;
vector < int > ans;

inline void PUSHANS (int x){
	for (int i = 1;i <= a[x][0];++ i)
		if (b[a[x][i]]){ -- b[a[x][i]]; restb -= ! b[a[x][i]]; ++ res; break; }
	ans.push_back (x);
	vis[x] = true;
}

bool inD[N + 5];
int deg[N + 5];
vector < int > gt[N + 5];

inline void ARKNIGHTS (){
	ans.clear (), res = 0;
	io::read (n), io::read (m);
	restb = 0;
	for (int i = 1;i <= m;++ i) restb += (bool) io::read (b[i]);
	for (int i = 1;i <= n;++ i){
		vis[i] = false;
		io::read (a[i][0]);
		for (int j = 1;j <= a[i][0];++ j) io::read (a[i][j]);
	}
	queue < int > Q;
	while (restb && (int) ans.size () < n){
		int L = n - (int) ans.size (), R = restb;
		s = L + R + 1, t = s + 1;
		init ();
		for (int i = 1, cnt = 0;i <= m;++ i)
			if (! b[i]) rep[i] = 0;
			else rep[i] = ++ cnt, Add (rep[i] + L, t, b[i]);
		for (int i = 1, cnt = 0;i <= n;++ i)
			if (! vis[i]){
				fr[i] = tot + 1;
				Add (s, ++ cnt, 1), fi[i] = 0;
				for (int j = a[i][0];j >= 1;-- j)
					if (rep[a[i][j]]){
						te[i][j] = tot + 1, fi[i] = a[i][j];
						Add (cnt, rep[a[i][j]] + L, 1);
					}
			} else
				fi[i] = 0;
		FLOW ();
		for (int i = 1;i <= m;++ i) ustag[i] = inD[i] = false, deg[i] = 0, gt[i].clear ();
		for (int i = 1;i <= n;++ i)
			if (! vis[i] && ! w[fr[i]]){
				for (int j = 1;j <= a[i][0];++ j)
					if (! w[te[i][j]]){ ustag[us[i] = a[i][j]] = true; break; }
			} else us[i] = 0;
		for (int i = 1;i <= n;++ i)
			if (fi[i] && (! ustag[fi[i]] || fi[i] == us[i])){
				PUSHANS (i);
				goto next_case;
			}
		for (int i = 1;i <= n;++ i)
			if (us[i])
				gt[us[i]].push_back (fi[i]), ++ deg[fi[i]];
		for (int i = 1;i <= m;++ i) if (! deg[i]) Q.push (i);
		while (! Q.empty ()){
			int u = Q.front (); Q.pop ();
			inD[u] = true;
			for (int v : gt[u])
				if (! (-- deg[v]))
					Q.push (v);
		}
		for (int i = 1;i <= n;++ i)
			if (us[i] && ! inD[us[i]]){
				PUSHANS (i);
				break;
			}

		next_case : ;
	}
	for (int i = 1;i <= n;++ i)
		if (! vis[i])
			PUSHANS (i);
	io::write (res), putchar ('\n');
	for (int i = 0;i < n;++ i)
		io::write (ans[i]), putchar (' ');
	putchar ('\n');
}

signed main (){
	for (int T = io::read ();T --;)	ARKNIGHTS ();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 15840kb

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: -100
Time Limit Exceeded

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:


result: