QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#98663#6329. Colorful GraphCSU2023WA 342ms19156kbC++144.3kb2023-04-19 19:33:172023-04-19 19:33:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-19 19:33:19]
  • 评测
  • 测评结果:WA
  • 用时:342ms
  • 内存:19156kb
  • [2023-04-19 19:33:17]
  • 提交

answer

#include <bits/stdc++.h>

template <class T>
inline void read(T &res)
{
	char ch; bool flag = false; res = 0;
	while (ch = getchar(), !isdigit(ch) && ch != '-');
	ch == '-' ? flag = true : res = ch ^ 48;
	while (ch = getchar(), isdigit(ch))
		res = res * 10 + ch - 48;
	flag ? res = -res : 0;
}

template <class T>
inline void nonnegative_put(T x)
{
	if (x > 9)
		nonnegative_put(x / 10);
	putchar(x % 10 + 48);
}

template <class T>
inline void put(T x)
{
	if (x < 0)
		x = -x, putchar('-');
	nonnegative_put(x);
}

template <class T>
inline void CkMin(T &x, T y) {x > y ? x = y : 0;}
template <class T>
inline void CkMax(T &x, T y) {x < y ? x = y : 0;}
template <class T>
inline T Min(T x, T y) {return x < y ? x : y;}
template <class T>
inline T Max(T x, T y) {return x > y ? x : y;}
template <class T>
inline T Abs(T x) {return x < 0 ? -x : x;}
template <class T>
inline T Sqr(T x) {return x * x;} 
//call Sqr((ll)x) when the type of returned value is "long long".

using std::map;
using std::set;
using std::pair;
using std::bitset;
using std::string;
using std::vector;
using std::complex;
using std::multiset;
using std::priority_queue;

typedef long long ll;
typedef long double ld;
typedef complex<ld> com;
typedef pair<int, int> pir;
const ld pi = acos(-1.0);
const ld eps = 1e-8;
const int Maxn = 1e9;
const int Minn = -1e9;
const int mod = 998244353; 
const int N = 14e3 + 5;

vector<int> e[N], re[N];
int ans[N], ind[N], dfn[N], low[N], stk[N], col[N]; 
bool ins[N];
bitset<N> vis[N];
int T_data, n, m, C, top, tis;

const int M = 7e4 + 5;
int nxt[M], to[M], cap[M], adj[N], que[N], cur[N], lev[N];
int src, des, qr, T = 1;

inline void linkArc(int x, int y, int w)
{
	nxt[++T] = adj[x]; adj[x] = T; to[T] = y; cap[T] = w;
	nxt[++T] = adj[y]; adj[y] = T; to[T] = x; cap[T] = 0;	
}

inline bool Bfs()
{
	for (int x = 1; x <= des; ++x)	
		cur[x] = adj[x], lev[x] = -1;
	// 初始化具体的范围视建图而定,这里点的范围为 [1,n]
	que[qr = 1] = src;
	lev[src] = 0;
	for (int i = 1; i <= qr; ++i)
	{
		int x = que[i], y;
		for (int e = adj[x]; e; e = nxt[e])
			if (cap[e] > 0 && lev[y = to[e]] == -1)
			{
				lev[y] = lev[x] + 1;
				que[++qr] = y;
				if (y == des)
					return true;
			}
	}
	return false;
} 

inline int Dinic(int x, int flow)
{
	if (x == des)
		return flow;
	int y, delta, res = 0;	
	for (int &e = cur[x]; e; e = nxt[e]) 
		if (cap[e] > 0 && lev[y = to[e]] > lev[x])
		{
			delta = Dinic(y, Min(flow - res, cap[e]));
			if (delta)
			{
				cap[e] -= delta;
				cap[e ^ 1] += delta;
				res += delta;
				if (res == flow)
					break ; 
				//此时 break 保证下次 cur[x] 仍有机会增广 
			}
		} 
	if (res != flow)
		lev[x] = -1;
	return res; 
}

inline int maxFlow()
{
	int res = 0;
	while (Bfs())
		res += Dinic(src, Maxn);
	return res;
}

inline void Tarjan(int x)
{
	dfn[x] = low[x] = ++tis;
	stk[++top] = x;
	ins[x] = true; 
	for (int y : e[x])
		if (!dfn[y])
		{
			Tarjan(y);
			CkMin(low[x], low[y]);
		}
		else if (ins[y])
			CkMin(low[x], dfn[y]);
	if (dfn[x] == low[x])
	{
		int y;
		++C;
		do
		{
			y = stk[top--];
			ins[y] = false;
			col[y] = C;
		}while (y != x);
	}
}

inline void dfs(int x)
{
	ans[x] = tis;
	for (int y : re[x])
		if (ind[y])
		{
			--ind[y];
			dfs(y);
			return ;
		}			
}

int main()
{
	read(n); read(m);
	for (int i = 1, x, y; i <= m; ++i)
	{
		read(x); read(y);
		e[x].emplace_back(y);
	}
	for (int i = 1; i <= n; ++i)
		if (!dfn[i])
			Tarjan(i);
	for (int x = 1; x <= n; ++x)
	{
		for (int y : e[x])
			if (col[x] != col[y])
				vis[col[x]][col[y]] = 1;
		e[x].clear();
	}
	src = (C << 1) | 1, des = src + 1;
	for (int x = 1; x <= C; ++x)
	{
		linkArc(src, x, 1);
		linkArc(x + C, des, 1);
		linkArc(x + C, x, Maxn);
		for (int y = 1; y <= C; ++y)
			if (vis[x][y])
				linkArc(x, y + C, Maxn);
	}
	int _ans = maxFlow();
	for (int x = 1; x <= C; ++x)
		for (int e = adj[x]; e; e = nxt[e])
			if (!(e & 1) && to[e] > C && to[e] < src && to[e] - C != x && cap[e] < Maxn)
			{
				re[x].emplace_back(to[e] - C);
				++ind[to[e] - C];
			}
	tis = 0;
	for (int x = 1; x <= C; ++x)
		if (!ind[x])
		{
			++tis;
			dfs(x);
		}
	for (int i = 1; i <= n; ++i)
		put(ans[col[i]]), putchar(' ');
	putchar('\n');
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2 2 2 1 2 

result:

ok AC

Test #2:

score: 0
Accepted
time: 2ms
memory: 5568kb

input:

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

output:

1 1 2 2 2 

result:

ok AC

Test #3:

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

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 342ms
memory: 19156kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

1 0 0 0 4 2 3 0 0 0 2 2 4 2 0 5 0 6 0 0 0 0 3 0 0 7 8 11 0 9 0 0 10 11 0 0 0 12 3 13 0 0 14 0 15 0 16 3 17 18 0 19 0 4 0 0 2 20 0 21 0 4 4 3 0 22 3 3 0 23 24 0 25 0 26 0 11 0 0 3 3 3 0 0 2 0 0 0 0 3 0 11 246 0 3 0 0 0 27 0 4 5 0 0 0 28 0 4 0 29 4 0 30 0 0 2 0 31 4 3 4 32 33 5 0 0 34 0 0 35 0 0 36 2 ...

result:

wrong answer Integer 0 violates the range [1, 1750]